+static ISAMD_PP get_new_main_block( ISAMD_PP firstpp, ISAMD_PP pp)
+{ /* allocates a new block for the main data, and links it in */
+ int newblock;
+ if (0 == firstpp->next)
+ { /* special case, pp not yet allocated. */
+ /*Started as largest size, that's fine */
+ pp->pos = isamd_alloc_block(pp->is,pp->cat);
+ firstpp->next = isamd_addr(pp->pos,pp->cat);
+ if (pp->is->method->debug >3)
+ logf(LOG_LOG,"isamd_build: Alloc 1. dblock p=%d=%d:%d",
+ isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos);
+ }
+ newblock=isamd_alloc_block(pp->is,pp->cat);
+ pp->next=isamd_addr(newblock,pp->cat);
+ isamd_buildlaterblock(pp);
+ isamd_write_block(pp->is,pp->cat,pp->pos,pp->buf);
+ if (pp->is->method->debug >3)
+ logf(LOG_LOG,"isamd_build: Alloc nxt %d=%d:%d -> %d=%d:%d",
+ isamd_addr(pp->pos,pp->cat), pp->cat, pp->pos,
+ isamd_addr(newblock,pp->cat), pp->cat, newblock);
+ pp->next=0;
+ pp->pos=newblock;
+ pp->size=pp->offset=ISAMD_BLOCK_OFFSET_N;
+ return pp;
+} /* get_new_main_block */
+
+
+static ISAMD_PP append_main_item(ISAMD_PP firstpp,
+ ISAMD_PP pp,
+ struct it_key *i_key)
+{ /* appends one item in the main data block, allocates new if needed */
+ char *i_item= (char *) i_key; /* same as char */
+ char *i_ptr=i_item;
+ char codebuff[128];
+ char *c_ptr = codebuff;
+ int codelen;
+ char hexbuff[64];
+
+ int maxsize = pp->is->method->filecat[pp->is->max_cat].bsize;
+
+ c_ptr=codebuff;
+ i_ptr=i_item;
+ (*pp->is->method->code_item)(ISAMD_ENCODE, pp->decodeClientData,
+ &c_ptr, &i_ptr);
+ codelen = c_ptr - codebuff;
+ assert ( (codelen<128) && (codelen>0));
+ if (pp->is->method->debug >7)
+ logf(LOG_LOG,"isamd:build: coded %s nk=%d,ofs=%d-%d",
+ hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1,
+ pp->offset, pp->offset+codelen);
+
+ if (pp->offset + codelen > maxsize )
+ { /* oops, block full - get a new one */
+ pp = get_new_main_block( firstpp, pp );
+ /* reset encoging and code again */
+ (*pp->is->method->code_reset)(pp->decodeClientData);
+ c_ptr=codebuff;
+ i_ptr=i_item;
+ (*pp->is->method->code_item)(ISAMD_ENCODE, pp->decodeClientData,
+ &c_ptr, &i_ptr);
+ codelen = c_ptr - codebuff;
+ assert ( (codelen<128) && (codelen>0));
+ if (pp->is->method->debug >7)
+ logf(LOG_LOG,"isamd:build: recoded into %s (nk=%d)",
+ hexdump(codebuff, c_ptr-codebuff,hexbuff), firstpp->numKeys+1);
+ } /* block full */
+
+ assert (pp->offset + codelen <= maxsize );
+
+ /* write the data into pp, now we must have room */
+ memcpy(&(pp->buf[pp->offset]),codebuff,codelen);
+ pp->offset += codelen;
+ pp->size += codelen;
+ firstpp->numKeys++;
+ /* clear the next 4 bytes in block, to avoid confusions with diff lens */
+ /* dirty, it should not be done here, but something slips somewhere, and */
+ /* I hope this fixes it... - Heikki */
+ codelen = pp->offset;
+ while ( (codelen < maxsize ) && (codelen <= pp->offset+4) )
+ pp->buf[codelen++] = '\0';
+ return pp;
+} /* append_main_item */