make saved position reusable
[tedtools.git] / sfxstr.c
index 0697ba3..c107e60 100644 (file)
--- a/sfxstr.c
+++ b/sfxstr.c
@@ -29,6 +29,7 @@
 
 #include <errno.h>
 #include <string.h>
+#include <stdint.h>
 #include <sys/types.h>
 #include <sys/file.h>
 #include <sys/uio.h>
@@ -46,7 +47,7 @@
 
 #define SFSTREE_VERSION                0x0100
 
-typedef unsigned long Opaque;  /* XXX sizeof(Opaque) == sizeof(void *) */
+typedef uintptr_t Opaque;  /* XXX sizeof(Opaque) == sizeof(void *) */
 
 #define CHECK_MEMORY(tree)     ( ( (tree)->plainmemory ) ? \
        tlog(TL_CRIT|TL_EXIT, "Tree in plain memory - read only access") : (void)0 ) 
@@ -139,20 +140,48 @@ SFSInit_c(SFSTree *info, char **in) {
        return info;
 }
 
+#define ISEND(p,w,l)   ( (l>0) ? ( ((char*)(p))-(w) >= (l) ) : ( *(p) == '\0' ) )
+
+void*
+SFSFindData(SFSTree *info, char *word, int len) {
+       SFSDataIO       in;
+
+       in.key = word;
+       in.keylen = len;
+
+       return SFSFindDataOrSave(info, &in, NULL);
+}
+
 void*
-SFSFindData(SFSTree *info, char *word) {
+SFSFindDataOrSave(SFSTree *info, SFSDataIO *in, SFSTreePosition *position) {
        SFSNode *node = info->node;
+       SFSNode **pnode = &(info->node);
        SFSNodeData *StopLow, *StopHigh, *StopMiddle;
-       u_int8_t *ptr =(u_int8_t*)word;
+       u_int8_t *ptr =(u_int8_t*)in->key;
+
+       if ( position ) {
+               position->nodeptr = NULL;
+               position->node = NULL;
+               position->level = 0;
+       }
+
+       while( node && !ISEND(ptr, in->key, in->keylen) ) {
+               if ( position ) {
+                       position->nodeptr = pnode;
+                       position->node = node;
+                       position->level =  ((char*)ptr) - in->key;
+               }
 
-       while( node && *ptr ) {
                if ( node->isskip ) {
-                       if ( STRNCMP(ptr, ((char*)node)+node->dataptr, node->nchar) ) {
+                       if ( in->keylen>0 &&  in->keylen - (((char*)ptr) - in->key) > node->nchar )
+                               return NULL;
+                       else if ( STRNCMP(ptr, ((char*)node)+node->dataptr, node->nchar) ) {
                                ptr+=node->nchar;
-                               if ( *ptr=='\0' && node->isword) {
+                               if ( ISEND(ptr, in->key, in->keylen) && node->isword) {
                                        return (void*) ( ((char*)(node->data)) + ((node->haschild) ? sizeof(SFSNode*) : 0) );
                                } else if ( node->haschild ) {
                                        node = getSkipChildPointer(info, node);
+                                       pnode = (SFSNode**)( (char*)(node->data) );
                                } else {
                                        return NULL;
                                }
@@ -165,10 +194,11 @@ SFSFindData(SFSTree *info, char *word) {
                                StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
                                if ( StopMiddle->val == *ptr ) {
                                        ptr++;
-                                       if ( *ptr=='\0' && StopMiddle->isword ) {
+                                       if ( ISEND(ptr, in->key, in->keylen) && StopMiddle->isword ) {
                                                return (void*)( ((char*)node) + node->dataptr + info->datasize * StopMiddle->data );
                                        } else if ( StopMiddle->haschild ) {
                                                node = getChildPointer(info, StopMiddle);
+                                               pnode = (SFSNode**)(((char*)StopMiddle) + StopMiddle->child);
                                        } else {
                                                return NULL;
                                        }
@@ -186,6 +216,18 @@ SFSFindData(SFSTree *info, char *word) {
        return NULL;
 }
 
+void
+SFSAddSaved(SFSTree *info, SFSDataIO *in, SFSTreePosition *position) {
+       CHECK_MEMORY(info);
+
+       if ( !(position->nodeptr && position->node) ) {
+               SFSAdd(info, in);
+               return;
+       }
+
+       position->node = *(position->nodeptr) = addRecord(info, position->node, in, position->level);
+}
+
 static void
 freeFSFNode(SFSTree *info, SFSNode *node, void (*freefunc)(void*)) {
        u_int32_t i;
@@ -1021,7 +1063,7 @@ SFSWriteDump(SFSTree *info, char *filename, void *extradata, u_int32_t extrasize
        SFSTreeDumpHeader       dh;
 
        if ( (fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0 )
-               tlog(TL_CRIT|TL_EXIT, "Can not open file '%s': %s", strerror(errno));
+               tlog(TL_CRIT|TL_EXIT, "Can not open file '%s': %s", filename, strerror(errno));
 
        if ( flock(fd, LOCK_EX) < 0 )
                tlog(TL_CRIT|TL_EXIT, "flock failed: %s", strerror(errno));
@@ -1194,8 +1236,8 @@ SFSInitFromDump(SFSTree *info, void *pointer, u_int64_t size, void **extradata,
                tlog(TL_CRIT|TL_EXIT, "sizeof(Opaque) mismatch");
        if ( dh->headersize != SFSTDHSZ )
                tlog(TL_CRIT|TL_EXIT, "Tree's header size mismatch (should be %d but %d bytes)", SFSTDHSZ, dh->headersize);
-       if ( size && size != dh->totalen + SFSTDHSZ )
-               tlog(TL_CRIT|TL_EXIT, "Memory size mismatch (should be %d but %d bytes)", dh->totalen + SFSTDHSZ, size);
+       if ( size && size != dh->totalen + SFSTDHSZ  + MAXALIGN(dh->extrasize) )
+               tlog(TL_CRIT|TL_EXIT, "Memory size mismatch (should be %d but %d bytes)", dh->totalen + SFSTDHSZ + dh->extrasize , size);
 
        info->totalen = dh->totalen;
        info->nnodes = dh->nnodes;