X-Git-Url: https://git.decadent.org.uk/gitweb/?p=ion3.git;a=blobdiff_plain;f=libtu%2Fstringstore.c;h=5529820666cd46d27f7d25b01d9ce35070ca54dd;hp=6f54387b86280bb677d924f7c4d59706dc93c5ed;hb=20070203;hpb=8366314611bf30a0f31d25bf5f5023186fa87692 diff --git a/libtu/stringstore.c b/libtu/stringstore.c index 6f54387..5529820 100644 --- a/libtu/stringstore.c +++ b/libtu/stringstore.c @@ -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 +#include #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++; +} +