stringstore.c

Sat, 27 Jan 2007 00:28:27 +0100

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sat, 27 Jan 2007 00:28:27 +0100
changeset 106
f1bb821008fc
parent 105
de53cada1423
child 107
da2a985da6ee
permissions
-rw-r--r--

stringstore_get should work on STRINGID_NONE too.

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>
105
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
11 #include <string.h>
73
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
12
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
13 #include "misc.h"
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
14 #include "output.h"
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
15 #include "rb.h"
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
16 #include "stringstore.h"
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
17
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
18
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
19 static Rb_node stringstore=NULL;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
20
74
60cb620a0341 trunk: changeset 1575
tuomov
parents: 73
diff changeset
21
60cb620a0341 trunk: changeset 1575
tuomov
parents: 73
diff changeset
22 const char *stringstore_get(StringId id)
60cb620a0341 trunk: changeset 1575
tuomov
parents: 73
diff changeset
23 {
106
f1bb821008fc stringstore_get should work on STRINGID_NONE too.
Tuomo Valkonen <tuomov@iki.fi>
parents: 105
diff changeset
24 return (id==STRINGID_NONE
f1bb821008fc stringstore_get should work on STRINGID_NONE too.
Tuomo Valkonen <tuomov@iki.fi>
parents: 105
diff changeset
25 ? NULL
f1bb821008fc stringstore_get should work on STRINGID_NONE too.
Tuomo Valkonen <tuomov@iki.fi>
parents: 105
diff changeset
26 : (const char*)(((Rb_node)id)->k.key));
74
60cb620a0341 trunk: changeset 1575
tuomov
parents: 73
diff changeset
27 }
60cb620a0341 trunk: changeset 1575
tuomov
parents: 73
diff changeset
28
105
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
29
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
30 typedef struct{
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
31 const char *key;
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
32 uint len;
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
33 } D;
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
34
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
35 static int cmp(const void *d_, const char *nodekey)
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
36 {
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
37 D *d=(D*)d_;
73
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
38
105
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
39 int res=strncmp(d->key, nodekey, d->len);
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
40
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
41 return (res!=0
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
42 ? res
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
43 : (nodekey[d->len]=='\0' ? 0 : 1));
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
44 }
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
45
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
46
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
47 StringId stringstore_find_n(const char *str, uint l)
73
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
48 {
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
49 Rb_node node;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
50 int found=0;
105
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
51 D d;
73
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
52
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
53 if(stringstore==NULL)
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
54 return STRINGID_NONE;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
55
105
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
56 d.key=str;
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
57 d.len=l;
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
58
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
59 node=rb_find_gkey_n(stringstore, &d, (Rb_compfn*)cmp, &found);
73
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
60
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
61 if(!found)
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
62 return STRINGID_NONE;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
63
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
64 return (StringId)node;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
65 }
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
66
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
67
105
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
68 StringId stringstore_find(const char *str)
73
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
69 {
105
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
70 return stringstore_find_n(str, strlen(str));
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
71 }
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
72
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
73
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
74 StringId stringstore_alloc_n(const char *str, uint l)
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
75 {
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
76 Rb_node node=(Rb_node)stringstore_find_n(str, l);
73
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
77 char *s;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
78
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
79 if(node!=NULL){
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
80 node->v.ival++;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
81 return node;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
82 }
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
83
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
84 if(stringstore==NULL){
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
85 stringstore=make_rb();
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
86 if(stringstore==NULL)
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
87 return STRINGID_NONE;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
88 }
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
89
105
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
90 s=scopyn(str, l);
73
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
91
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
92 if(s==NULL)
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
93 return STRINGID_NONE;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
94
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
95 node=rb_insert(stringstore, s, NULL);
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
96
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
97 if(node==NULL)
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
98 return STRINGID_NONE;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
99
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
100 node->v.ival=1;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
101
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
102 return (StringId)node;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
103 }
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
104
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
105
105
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
106 StringId stringstore_alloc(const char *str)
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
107 {
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
108 return stringstore_alloc_n(str, strlen(str));
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
109 }
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
110
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
111
73
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
112 void stringstore_free(StringId id)
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
113 {
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
114 Rb_node node=(Rb_node)id;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
115
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
116 if(node==NULL){
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
117 warn("Attempt to free un-allocated string from stringstore.");
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
118 return;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
119 }
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
120
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
121 if(node->v.ival<=0){
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
122 warn("Stringstore reference count corrupted.");
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
123 return;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
124 }
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
125
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
126 node->v.ival--;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
127
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
128 if(node->v.ival==0){
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
129 char *s=(char*)(node->k.key);
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
130 rb_delete_node(node);
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
131 free(s);
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
132 }
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
133 }
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
134
104
abbac137bdac Added stringstore_ref
Tuomo Valkonen <tuomov@iki.fi>
parents: 74
diff changeset
135
abbac137bdac Added stringstore_ref
Tuomo Valkonen <tuomov@iki.fi>
parents: 74
diff changeset
136 void stringstore_ref(StringId id)
abbac137bdac Added stringstore_ref
Tuomo Valkonen <tuomov@iki.fi>
parents: 74
diff changeset
137 {
abbac137bdac Added stringstore_ref
Tuomo Valkonen <tuomov@iki.fi>
parents: 74
diff changeset
138 Rb_node node=(Rb_node)id;
abbac137bdac Added stringstore_ref
Tuomo Valkonen <tuomov@iki.fi>
parents: 74
diff changeset
139
abbac137bdac Added stringstore_ref
Tuomo Valkonen <tuomov@iki.fi>
parents: 74
diff changeset
140 if(node!=NULL)
abbac137bdac Added stringstore_ref
Tuomo Valkonen <tuomov@iki.fi>
parents: 74
diff changeset
141 node->v.ival++;
abbac137bdac Added stringstore_ref
Tuomo Valkonen <tuomov@iki.fi>
parents: 74
diff changeset
142 }
abbac137bdac Added stringstore_ref
Tuomo Valkonen <tuomov@iki.fi>
parents: 74
diff changeset
143

mercurial