4 * Copyright (c) Tuomo Valkonen 2004-2007.
6 * You may distribute and modify this library under the terms of either
7 * the Clarified Artistic License or the GNU LGPL, version 2.1 or later.
16 #include "stringstore.h"
19 static Rb_node stringstore=NULL;
22 const char *stringstore_get(StringId id)
24 return (id==STRINGID_NONE
26 : (const char*)(((Rb_node)id)->k.key));
35 static int cmp(const void *d_, const char *nodekey)
39 int res=strncmp(d->key, nodekey, d->len);
43 : (nodekey[d->len]=='\0' ? 0 : -1));
47 StringId stringstore_find_n(const char *str, uint l)
59 node=rb_find_gkey_n(stringstore, &d, (Rb_compfn*)cmp, &found);
64 return (StringId)node;
68 StringId stringstore_find(const char *str)
70 return stringstore_find_n(str, strlen(str));
74 StringId stringstore_alloc_n(const char *str, uint l)
76 Rb_node node=(Rb_node)stringstore_find_n(str, l);
84 if(stringstore==NULL){
85 stringstore=make_rb();
95 node=rb_insert(stringstore, s, NULL);
102 return (StringId)node;
106 StringId stringstore_alloc(const char *str)
109 return STRINGID_NONE;
111 return stringstore_alloc_n(str, strlen(str));
115 void stringstore_free(StringId id)
117 Rb_node node=(Rb_node)id;
123 warn("Stringstore reference count corrupted.");
130 char *s=(char*)(node->k.key);
131 rb_delete_node(node);
137 void stringstore_ref(StringId id)
139 Rb_node node=(Rb_node)id;