Merge branch 'master' of https://secure.softndesign.org/git/compress
[compress.git] / code.c
diff --git a/code.c b/code.c
index 67220e05b02dd97a390f25a7d49409da3872d840..b8c8e78c7067cca3498dacda767185af1ed4fbe7 100644 (file)
--- a/code.c
+++ b/code.c
@@ -1,11 +1,12 @@
-#include <stddef.h>
-#include <unistd.h>
-
 #include "debug.h"
-#include "fprintf.h"
+#include "fdprintf.h"
 
 #include "code.h"
 
+/* constants */
+
+#define NB_LEAFS ((NB_BYTES + 1) * NB_BYTES / 2)
+
 /* concatenate code */
 
 int codcat (char *dst, int n, char *src)
@@ -14,7 +15,7 @@ int codcat (char *dst, int n, char *src)
         n--;
         dst++;
     }
-    
+
     return (n > 0) ? codcpy (dst, n, src) : -1;
 }
 
@@ -22,14 +23,12 @@ int codcat (char *dst, int n, char *src)
 
 int codcmp (char *cod1, char *cod2)
 {
-    int i = -1;
-
-    do {
-        i++;
-        if (cod1[i] != cod2[i]) {
-            return (cod1[i] < cod2[i]) ? -1 : 1;
+    while ((*cod1 != 0) || (*cod2 != 0)) {
+        int test = *cod1++ - *cod2++;
+        if (test != 0) {
+            return (test < 0) ? -1 : 1;
         }
-    } while (cod1[i] != 0);
+    }
 
     return 0;
 }
@@ -46,7 +45,7 @@ int codcpy (char *dst, int n, char *src)
             return i;
         }
     }
-    VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "Buffer too short\n"));
+    VERBOSE (ERROR, PRINTERR ("Buffer too short\n"));
 
     return -1;
 }
@@ -64,4 +63,22 @@ int codlen (char *code)
     return i;
 }
 
-/* vim: set ts=4 sw=4 et */
+/* get leaf(s) */
+
+leaf_t *getleaf (int n)
+{
+    static leaf_t leafs[NB_LEAFS] = {0};
+    static int pos = 0;
+    leaf_t *pt = (void *)0;
+
+    if (pos + n < NB_LEAFS) {
+        pt = leafs + pos;
+        pos += n;
+    } else {
+        VERBOSE (ERROR, PRINTERR ("No more leaf avaliable\n"));
+    }
+
+    return pt;
+}
+
+/* vim: set ts=4 sw=4 et: */