add .gitignore
[tedtools.git] / sfxstr.c
index 0dd5fa7..d65bf7c 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,23 +140,57 @@ SFSInit_c(SFSTree *info, char **in) {
        return info;
 }
 
-#define ISEND(p,w,l)   ( (l>0) ? ( ((char*)(p))-(w) < (l) ) : ( *(p) == '\0' ) )
+#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 SFSFindDataFromSavedOrSave(info, &in, NULL);
+}
+
+void*
+SFSFindDataOrSave(SFSTree *info, SFSDataIO *in, SFSTreePosition *position) {
+       if ( position )
+               memset(position, 0, sizeof(SFSTreePosition));
+
+       return SFSFindDataFromSavedOrSave(info, in, position); 
+}
+
+void*
+SFSFindDataFromSavedOrSave(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 && position->node && in->keylen > position->level ) {
+               node = position->node;
+               pnode = position->nodeptr;
+               ptr += position->level;
+       }
+
+       while( node && !ISEND(ptr, in->key, in->keylen) ) {
+               if ( position ) {
+                       position->nodeptr = pnode;
+                       position->node = node;
+                       position->level =  ((char*)ptr) - in->key;
+               }
 
-       while( node && !ISEND(ptr, word, len) ) {
                if ( node->isskip ) {
-                       if ( len>0 &&  len - (((char*)ptr) - word) > 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 ( ISEND(ptr, word, len) && node->isword) {
-                                       return (void*) ( ((char*)(node->data)) + ((node->haschild) ? sizeof(SFSNode*) : 0) );
+                               if ( ISEND(ptr, in->key, in->keylen) ) {
+                                       if (node->isword)
+                                               return (void*) ( ((char*)(node->data)) + ((node->haschild) ? sizeof(SFSNode*) : 0) );
+                                       return NULL;
                                } else if ( node->haschild ) {
+                                       pnode = (SFSNode**)( (char*)(node->data) );
                                        node = getSkipChildPointer(info, node);
                                } else {
                                        return NULL;
@@ -169,9 +204,12 @@ SFSFindData(SFSTree *info, char *word, int len) {
                                StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
                                if ( StopMiddle->val == *ptr ) {
                                        ptr++;
-                                       if ( ISEND(ptr, word, len) && StopMiddle->isword ) {
-                                               return (void*)( ((char*)node) + node->dataptr + info->datasize * StopMiddle->data );
+                                       if ( ISEND(ptr, in->key, in->keylen) ) {
+                                               if ( StopMiddle->isword )
+                                                       return (void*)( ((char*)node) + node->dataptr + info->datasize * StopMiddle->data );
+                                               return NULL;
                                        } else if ( StopMiddle->haschild ) {
+                                               pnode = (SFSNode**)(((char*)StopMiddle) + StopMiddle->child);
                                                node = getChildPointer(info, StopMiddle);
                                        } else {
                                                return NULL;
@@ -190,6 +228,18 @@ SFSFindData(SFSTree *info, char *word, int len) {
        return NULL;
 }
 
+void
+SFSAddSaved(SFSTree *info, SFSDataIO *in, SFSTreePosition *position) {
+       CHECK_MEMORY(info);
+
+       if ( !(position && 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;
@@ -588,11 +638,11 @@ addRecord(SFSTree *info, SFSNode* node, SFSDataIO *in, int level) {
                        else 
                                node = splitSkipNode(info, node, in, level); 
                } else {
-                       StopLow = node->data;   
+               StopLow = node->data;   
                        StopHigh = StopLow + node->nchar;
                        while (StopLow < StopHigh) {
                                StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
-                               if ( StopMiddle->val == *ptr ) {
+                       if ( StopMiddle->val == *ptr ) {
                                        if ( *(ptr+1)=='\0' ) {
                                                if ( StopMiddle->isword ) {
                                                        /* already exists */
@@ -753,8 +803,13 @@ SFSIterate(SFSTree *info, SFSDataIO *out) {
                return 1;
        }
 
-       if ( s == NULL || s->node == NULL)
+       if ( s == NULL )
                return 0;
+       if ( s->node == NULL ) {
+               info->stack = s->next;
+               tfree(s);
+               return SFSIterate(info, out);
+       }
                                                         
        while ( s->level + s->node->nchar + 1 >= info->tlen ) {
                info->tlen *= 2;
@@ -1071,8 +1126,9 @@ SFSWriteDump(SFSTree *info, char *filename, void *extradata, u_int32_t extrasize
 
                wp.info = info;
                wp.offset = 0;
-               
-               writeNode(&wp, fd, info->node, extrasize);
+       
+               if ( info->node )
+                       writeNode(&wp, fd, info->node, extrasize);
        }
        
        flock(fd, LOCK_UN);
@@ -1198,7 +1254,7 @@ 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  + dh->extrasize )
+       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;