]> git.decadent.org.uk Git - ion3.git/blobdiff - libtu/stringstore.c
[svn-upgrade] Integrating new upstream version, ion3 (20070203)
[ion3.git] / libtu / stringstore.c
index 6f54387b86280bb677d924f7c4d59706dc93c5ed..5529820666cd46d27f7d25b01d9ce35070ca54dd 100644 (file)
@@ -1,13 +1,14 @@
 /*
  * libtu/stringstore.c
  *
- * Copyright (c) Tuomo Valkonen 2004. 
+ * Copyright (c) Tuomo Valkonen 2004-2007
  *
  * You may distribute and modify this library under the terms of either
  * the Clarified Artistic License or the GNU LGPL, version 2.1 or later.
  */
 
 #include <stdlib.h>
+#include <string.h>
 
 #include "misc.h"
 #include "output.h"
@@ -20,19 +21,42 @@ static Rb_node stringstore=NULL;
 
 const char *stringstore_get(StringId id)
 {
-    return (const char*)(((Rb_node)id)->k.key);
+    return (id==STRINGID_NONE
+            ? NULL
+            : (const char*)(((Rb_node)id)->k.key));
 }
 
+
+typedef struct{
+    const char *key;
+    uint len;
+} D;
+
+static int cmp(const void *d_, const char *nodekey)
+{
+    D *d=(D*)d_;
     
-StringId stringstore_find(const char *str)
+    int res=strncmp(d->key, nodekey, d->len);
+    
+    return (res!=0 
+            ? res 
+            : (nodekey[d->len]=='\0' ? 0 : 1));
+}
+
+
+StringId stringstore_find_n(const char *str, uint l)
 {
     Rb_node node;
     int found=0;
+    D d;
     
     if(stringstore==NULL)
         return STRINGID_NONE;
     
-    node=rb_find_key_n(stringstore, str, &found);
+    d.key=str;
+    d.len=l;
+    
+    node=rb_find_gkey_n(stringstore, &d, (Rb_compfn*)cmp, &found);
     
     if(!found)
         return STRINGID_NONE;
@@ -41,9 +65,15 @@ StringId stringstore_find(const char *str)
 }
 
 
-StringId stringstore_alloc(const char *str)
+StringId stringstore_find(const char *str)
 {
-    Rb_node node=(Rb_node)stringstore_find(str);
+    return stringstore_find_n(str, strlen(str));
+}
+
+
+StringId stringstore_alloc_n(const char *str, uint l)
+{
+    Rb_node node=(Rb_node)stringstore_find_n(str, l);
     char *s;
     
     if(node!=NULL){
@@ -57,7 +87,7 @@ StringId stringstore_alloc(const char *str)
             return STRINGID_NONE;
     }
     
-    s=scopy(str);
+    s=scopyn(str, l);
     
     if(s==NULL)
         return STRINGID_NONE;
@@ -73,6 +103,12 @@ StringId stringstore_alloc(const char *str)
 }
 
 
+StringId stringstore_alloc(const char *str)
+{
+    return stringstore_alloc_n(str, strlen(str));
+}
+
+
 void stringstore_free(StringId id)
 {
     Rb_node node=(Rb_node)id;
@@ -96,3 +132,12 @@ void stringstore_free(StringId id)
     }
 }
 
+
+void stringstore_ref(StringId id)
+{
+    Rb_node node=(Rb_node)id;
+    
+    if(node!=NULL)
+        node->v.ival++;
+}
+