/*
* 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"
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;
}
-StringId stringstore_alloc(const char *str)
+StringId stringstore_find(const char *str)
+{
+ return stringstore_find_n(str, strlen(str));
+}
+
+
+StringId stringstore_alloc_n(const char *str, uint l)
{
- Rb_node node=(Rb_node)stringstore_find(str);
+ Rb_node node=(Rb_node)stringstore_find_n(str, l);
char *s;
if(node!=NULL){
return STRINGID_NONE;
}
- s=scopy(str);
+ s=scopyn(str, l);
if(s==NULL)
return STRINGID_NONE;
}
+StringId stringstore_alloc(const char *str)
+{
+ if(str==NULL)
+ return STRINGID_NONE;
+
+ return stringstore_alloc_n(str, strlen(str));
+}
+
+
void stringstore_free(StringId id)
{
Rb_node node=(Rb_node)id;
- if(node==NULL){
- warn("Attempt to free un-allocated string from stringstore.");
+ if(node==NULL)
return;
- }
if(node->v.ival<=0){
warn("Stringstore reference count corrupted.");
}
}
+
+void stringstore_ref(StringId id)
+{
+ Rb_node node=(Rb_node)id;
+
+ if(node!=NULL)
+ node->v.ival++;
+}
+