stringstore.c

Mon, 17 Jan 2005 22:02:09 +0100

author
tuomov
date
Mon, 17 Jan 2005 22:02:09 +0100
changeset 85
9f94b2e96e3b
parent 74
60cb620a0341
child 104
abbac137bdac
permissions
-rw-r--r--

trunk: changeset 1934
Fixed everything that requires locale stuff to check CF_NO_LOCALE.

73
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
1 /*
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
2 * libtu/stringstore.c
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
3 *
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
4 * Copyright (c) Tuomo Valkonen 2004.
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
5 *
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
6 * You may distribute and modify this library under the terms of either
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
7 * the Clarified Artistic License or the GNU LGPL, version 2.1 or later.
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
8 */
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
9
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
10 #include <stdlib.h>
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
11
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
12 #include "misc.h"
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
13 #include "output.h"
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
14 #include "rb.h"
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
15 #include "stringstore.h"
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
16
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
17
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
18 static Rb_node stringstore=NULL;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
19
74
60cb620a0341 trunk: changeset 1575
tuomov
parents: 73
diff changeset
20
60cb620a0341 trunk: changeset 1575
tuomov
parents: 73
diff changeset
21 const char *stringstore_get(StringId id)
60cb620a0341 trunk: changeset 1575
tuomov
parents: 73
diff changeset
22 {
60cb620a0341 trunk: changeset 1575
tuomov
parents: 73
diff changeset
23 return (const char*)(((Rb_node)id)->k.key);
60cb620a0341 trunk: changeset 1575
tuomov
parents: 73
diff changeset
24 }
60cb620a0341 trunk: changeset 1575
tuomov
parents: 73
diff changeset
25
73
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
26
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
27 StringId stringstore_find(const char *str)
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
28 {
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
29 Rb_node node;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
30 int found=0;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
31
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
32 if(stringstore==NULL)
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
33 return STRINGID_NONE;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
34
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
35 node=rb_find_key_n(stringstore, str, &found);
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
36
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
37 if(!found)
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
38 return STRINGID_NONE;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
39
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
40 return (StringId)node;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
41 }
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
42
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
43
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
44 StringId stringstore_alloc(const char *str)
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
45 {
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
46 Rb_node node=(Rb_node)stringstore_find(str);
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
47 char *s;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
48
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
49 if(node!=NULL){
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
50 node->v.ival++;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
51 return node;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
52 }
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
53
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
54 if(stringstore==NULL){
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
55 stringstore=make_rb();
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
56 if(stringstore==NULL)
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
57 return STRINGID_NONE;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
58 }
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
59
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
60 s=scopy(str);
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
61
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
62 if(s==NULL)
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
63 return STRINGID_NONE;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
64
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
65 node=rb_insert(stringstore, s, NULL);
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
66
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
67 if(node==NULL)
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
68 return STRINGID_NONE;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
69
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
70 node->v.ival=1;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
71
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
72 return (StringId)node;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
73 }
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
74
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
75
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
76 void stringstore_free(StringId id)
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
77 {
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
78 Rb_node node=(Rb_node)id;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
79
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
80 if(node==NULL){
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
81 warn("Attempt to free un-allocated string from stringstore.");
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
82 return;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
83 }
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
84
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
85 if(node->v.ival<=0){
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
86 warn("Stringstore reference count corrupted.");
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
87 return;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
88 }
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
89
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
90 node->v.ival--;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
91
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
92 if(node->v.ival==0){
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
93 char *s=(char*)(node->k.key);
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
94 rb_delete_node(node);
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
95 free(s);
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
96 }
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
97 }
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
98

mercurial