X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=libtu%2Fstringstore.c;h=3c5b7a1ec238d615fb8e71f8786b71c9bf765a8b;hb=ae4260bb64817c11f9a7140324cd3e3ba113e297;hp=6f54387b86280bb677d924f7c4d59706dc93c5ed;hpb=8366314611bf30a0f31d25bf5f5023186fa87692;p=ion3.git diff --git a/libtu/stringstore.c b/libtu/stringstore.c index 6f54387..3c5b7a1 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) +{ + 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){ @@ -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,14 +103,21 @@ StringId stringstore_alloc(const char *str) } +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."); @@ -96,3 +133,12 @@ void stringstore_free(StringId id) } } + +void stringstore_ref(StringId id) +{ + Rb_node node=(Rb_node)id; + + if(node!=NULL) + node->v.ival++; +} +