stringstore.c

Mon, 20 Apr 2020 10:14:32 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 20 Apr 2020 10:14:32 -0500
changeset 119
87e3bb5086e8
parent 110
13134ea30227
permissions
-rw-r--r--

Convert README to markdown

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 *
107
da2a985da6ee Clean-up and pending updates
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
4 * Copyright (c) Tuomo Valkonen 2004-2007.
73
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
110
13134ea30227 Oops, fixed comparison function order.
Tuomo Valkonen <tuomov@iki.fi>
parents: 108
diff changeset
43 : (nodekey[d->len]=='\0' ? 0 : -1));
105
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 {
108
2b4dd5f948bc Better handling of STRINGID_NONE
Tuomo Valkonen <tuomov@iki.fi>
parents: 107
diff changeset
108 if(str==NULL)
2b4dd5f948bc Better handling of STRINGID_NONE
Tuomo Valkonen <tuomov@iki.fi>
parents: 107
diff changeset
109 return STRINGID_NONE;
2b4dd5f948bc Better handling of STRINGID_NONE
Tuomo Valkonen <tuomov@iki.fi>
parents: 107
diff changeset
110
105
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
111 return stringstore_alloc_n(str, strlen(str));
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
112 }
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
113
de53cada1423 Added stringstore_find/alloc_n
Tuomo Valkonen <tuomov@iki.fi>
parents: 104
diff changeset
114
73
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
115 void stringstore_free(StringId id)
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
116 {
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
117 Rb_node node=(Rb_node)id;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
118
108
2b4dd5f948bc Better handling of STRINGID_NONE
Tuomo Valkonen <tuomov@iki.fi>
parents: 107
diff changeset
119 if(node==NULL)
73
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
120 return;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
121
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
122 if(node->v.ival<=0){
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
123 warn("Stringstore reference count corrupted.");
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
124 return;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
125 }
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
126
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
127 node->v.ival--;
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
128
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
129 if(node->v.ival==0){
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
130 char *s=(char*)(node->k.key);
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
131 rb_delete_node(node);
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
132 free(s);
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
133 }
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
134 }
401322031c7e trunk: changeset 1574
tuomov
parents:
diff changeset
135
104
abbac137bdac Added stringstore_ref
Tuomo Valkonen <tuomov@iki.fi>
parents: 74
diff changeset
136
abbac137bdac Added stringstore_ref
Tuomo Valkonen <tuomov@iki.fi>
parents: 74
diff changeset
137 void stringstore_ref(StringId id)
abbac137bdac Added stringstore_ref
Tuomo Valkonen <tuomov@iki.fi>
parents: 74
diff changeset
138 {
abbac137bdac Added stringstore_ref
Tuomo Valkonen <tuomov@iki.fi>
parents: 74
diff changeset
139 Rb_node node=(Rb_node)id;
abbac137bdac Added stringstore_ref
Tuomo Valkonen <tuomov@iki.fi>
parents: 74
diff changeset
140
abbac137bdac Added stringstore_ref
Tuomo Valkonen <tuomov@iki.fi>
parents: 74
diff changeset
141 if(node!=NULL)
abbac137bdac Added stringstore_ref
Tuomo Valkonen <tuomov@iki.fi>
parents: 74
diff changeset
142 node->v.ival++;
abbac137bdac Added stringstore_ref
Tuomo Valkonen <tuomov@iki.fi>
parents: 74
diff changeset
143 }
abbac137bdac Added stringstore_ref
Tuomo Valkonen <tuomov@iki.fi>
parents: 74
diff changeset
144

mercurial