Centralise the program name into the main header file. I'm probably
[sgt/agedu] / trie.c
diff --git a/trie.c b/trie.c
index f0b21df..7168a9e 100644 (file)
--- a/trie.c
+++ b/trie.c
@@ -11,6 +11,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include "agedu.h"
 #include "malloc.h"
 #include "trie.h"
 
@@ -23,9 +24,9 @@
  */
 static int trieccmp(unsigned char a, unsigned char b)
 {
-    a = (a == '\0' ? '\0' : a == '/' ? '\1' : a+1);
-    b = (b == '\0' ? '\0' : b == '/' ? '\1' : b+1);
-    return a - b;
+    a = (a == '\0' ? '\0' : a == pathsep ? '\1' : a+1);
+    b = (b == '\0' ? '\0' : b == pathsep ? '\1' : b+1);
+    return (int)a - (int)b;
 }
 
 static int triencmp(const char *a, size_t alen,
@@ -108,6 +109,7 @@ struct trie_header {
     off_t root, indexroot;
     int count;
     size_t maxpathlen;
+    int pathsep;
 };
 
 /* Union only used for computing alignment */
@@ -154,7 +156,7 @@ static void tb_seek(triebuild *tb, off_t off)
 {
     tb->offset = off;
     if (lseek(tb->fd, off, SEEK_SET) < 0) {
-       fprintf(stderr, "agedu: lseek: %s\n", strerror(errno));
+       fprintf(stderr, PNAME ": lseek: %s\n", strerror(errno));
        exit(1);
     }
 }
@@ -165,7 +167,7 @@ static void tb_write(triebuild *tb, const void *buf, size_t len)
     while (len > 0) {
        int ret = write(tb->fd, buf, len);
        if (ret < 0) {
-           fprintf(stderr, "agedu: write: %s\n", strerror(errno));
+           fprintf(stderr, PNAME ": write: %s\n", strerror(errno));
            exit(1);
        }
        len -= ret;
@@ -198,6 +200,7 @@ triebuild *triebuild_new(int fd)
     th.root = th.count = 0;
     th.indexroot = 0;
     th.maxpathlen = 0;
+    th.pathsep = (unsigned char)pathsep;
 
     tb_seek(tb, 0);
     tb_write(tb, &th, sizeof(th));
@@ -373,6 +376,7 @@ int triebuild_finish(triebuild *tb)
     th.root = triebuild_unwind(tb, 0, &th.count);
     th.indexroot = 0;
     th.maxpathlen = tb->maxpathlen;
+    th.pathsep = (unsigned char)pathsep;
 
     tb_seek(tb, 0);
     tb_write(tb, &th, sizeof(th));
@@ -494,6 +498,12 @@ unsigned long trie_count(const void *t)
     return hdr->count;
 }
 
+char trie_pathsep(const void *t)
+{
+    const struct trie_header *hdr = NODE(t, 0, trie_header);
+    return (char)hdr->pathsep;
+}
+
 struct triewalk_switch {
     const struct trie_switch *sw;
     int pos, depth, count;
@@ -606,3 +616,12 @@ off_t trie_get_index_offset(const void *t)
 {
     return ((const struct trie_header *)t)->indexroot;
 }
+
+void make_successor(char *pathbuf)
+{
+    int len = strlen(pathbuf);
+    if (len > 0 && pathbuf[len-1] == pathsep)
+       len--;
+    pathbuf[len] = '\001';
+    pathbuf[len+1] = '\0';
+}