# HG changeset patch # User tuomov # Date 1087596283 -7200 # Node ID 401322031c7ee748726e7b2186c4b3b6c439a5aa # Parent 7e69e5f7a0138c5a495acfbcec05d25f457adcc9 trunk: changeset 1574 Added routines for storing commonly used strings and referring to them by keys. diff -r 7e69e5f7a013 -r 401322031c7e Makefile --- a/Makefile Sun May 02 17:14:06 2004 +0200 +++ b/Makefile Sat Jun 19 00:04:43 2004 +0200 @@ -11,7 +11,8 @@ CFLAGS += $(C89_SOURCE) $(POSIX_SOURCE) SOURCES=misc.c output.c util.c optparser.c parser.c tokenizer.c \ - map.c obj.c objlist.c errorlog.c symlist.c rb.c + map.c obj.c objlist.c errorlog.c symlist.c rb.c \ + stringstore.c ifdef LIBTU_NO_ERRMSG DEFINES += -DLIBTU_NO_ERRMSG diff -r 7e69e5f7a013 -r 401322031c7e rb.h --- a/rb.h Sun May 02 17:14:06 2004 +0200 +++ b/rb.h Sat Jun 19 00:04:43 2004 +0200 @@ -66,6 +66,7 @@ struct rb_node *lext; } k; union { + int ival; void *val; struct rb_node *rext; } v; diff -r 7e69e5f7a013 -r 401322031c7e stringstore.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stringstore.c Sat Jun 19 00:04:43 2004 +0200 @@ -0,0 +1,92 @@ +/* + * libtu/stringstore.c + * + * Copyright (c) Tuomo Valkonen 2004. + * + * 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 "misc.h" +#include "output.h" +#include "rb.h" +#include "stringstore.h" + + +static Rb_node stringstore=NULL; + + +StringId stringstore_find(const char *str) +{ + Rb_node node; + int found=0; + + if(stringstore==NULL) + return STRINGID_NONE; + + node=rb_find_key_n(stringstore, str, &found); + + if(!found) + return STRINGID_NONE; + + return (StringId)node; +} + + +StringId stringstore_alloc(const char *str) +{ + Rb_node node=(Rb_node)stringstore_find(str); + char *s; + + if(node!=NULL){ + node->v.ival++; + return node; + } + + if(stringstore==NULL){ + stringstore=make_rb(); + if(stringstore==NULL) + return STRINGID_NONE; + } + + s=scopy(str); + + if(s==NULL) + return STRINGID_NONE; + + node=rb_insert(stringstore, s, NULL); + + if(node==NULL) + return STRINGID_NONE; + + node->v.ival=1; + + return (StringId)node; +} + + +void stringstore_free(StringId id) +{ + Rb_node node=(Rb_node)id; + + if(node==NULL){ + warn("Attempt to free un-allocated string from stringstore."); + return; + } + + if(node->v.ival<=0){ + warn("Stringstore reference count corrupted."); + return; + } + + node->v.ival--; + + if(node->v.ival==0){ + char *s=(char*)(node->k.key); + rb_delete_node(node); + free(s); + } +} + diff -r 7e69e5f7a013 -r 401322031c7e stringstore.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stringstore.h Sat Jun 19 00:04:43 2004 +0200 @@ -0,0 +1,21 @@ +/* + * libtu/stringstore.h + * + * Copyright (c) Tuomo Valkonen 2004. + * + * 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. + */ + +#ifndef LIBTU_STRINGSTORE_H +#define LIBTU_STRINGSTORE_H + +typedef void* StringId; + +#define STRINGID_NONE ((StringId)0) + +extern StringId stringstore_find(const char *str); +extern StringId stringstore_alloc(const char *str); +extern void stringstore_free(StringId id); + +#endif /* LIBTU_STRINGSTORE_H */