- if (*k) /* first time for new key */
- {
- bp = i->buf;
- while ((*bp++ = *k++))
- ;
- i->keylen= bp - i->buf -1;
- assert(i->keylen+1+sizeof(struct it_key) < ENCODE_BUFLEN);
- }
- else
- {
- bp=i->buf + i->keylen;
- *bp++=0;
- k++;
- }
-
- memcpy (&key, k+1, sizeof(struct it_key));
- if (0==i->prevsys) /* no previous filter, fill up */
- {
- i->prevsys=key.sysno;
- i->prevseq=key.seqno;
- i->prevcmd=*k;
- }
- else if ( (i->prevsys==key.sysno) &&
- (i->prevseq==key.seqno) &&
- (i->prevcmd!=*k) )
- { /* same numbers, diff cmd, they cancel out */
- i->prevsys=0;
- }
- else
- { /* different stuff, write previous, move buf */
- bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
- if (i->sysno != i->prevsys)
- {
- i->sysno = i->prevsys;
- i->seqno = 0;
- }
- else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
- {
- return; /* ??? Filters some sort of duplicates away */
- /* ??? Can this ever happen -H 15oct02 */
- }
- bp = encode_key_int (i->prevseq - i->seqno, bp);
- i->seqno = i->prevseq;
- i->cmd = i->prevcmd;
- if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
- {
- yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
- exit (1);
- }
- i->keylen=0; /* ok, it's written, forget it */
- i->prevsys=key.sysno;
- i->prevseq=key.seqno;
- i->prevcmd=*k;
- }
-}
-
-void encode_key_flush (struct encode_info *i, FILE *outf)
-{ /* flush the last key from i */
- char *bp =i->buf + i->keylen;
- if (0==i->prevsys)
- {
- return; /* nothing to flush */
- }
- *bp++=0;
- bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
- if (i->sysno != i->prevsys)
- {
- i->sysno = i->prevsys;
- i->seqno = 0;
- }
- else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
- {
- return; /* ??? Filters some sort of duplicates away */
- /* ??? Can this ever happen -H 15oct02 */
- }
- bp = encode_key_int (i->prevseq - i->seqno, bp);
- i->seqno = i->prevseq;
- i->cmd = i->prevcmd;
- if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
- {
- yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
- exit (1);
- }
- i->keylen=0; /* ok, it's written, forget it */
- i->prevsys=0; /* forget the values too */
- i->prevseq=0;
-}
-#endif