1 : #define VSTR_C
2 : /*
3 : * Copyright (C) 2004 James Antill
4 : *
5 : * This library is free software; you can redistribute it and/or
6 : * modify it under the terms of the GNU Lesser General Public
7 : * License as published by the Free Software Foundation; either
8 : * version 2 of the License, or (at your option) any later version.
9 : *
10 : * This library is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : * Lesser General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public
16 : * License along with this library; if not, write to the Free Software
17 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 : *
19 : * email: james@and.org
20 : */
21 : /* functions for assigning constant data from the "user" to a configuration */
22 : #include "main.h"
23 :
24 : int vstr__data_conf_init(Vstr_conf *conf)
25 6272 : {
26 6272 : if (!(conf->data_usr_ents = VSTR__MK(sizeof(struct Vstr_data_usr))))
27 1 : return (FALSE);
28 :
29 6271 : conf->data_usr_sz = 1;
30 6271 : conf->data_usr_len = 0;
31 :
32 6271 : return (TRUE);
33 : }
34 :
35 : void vstr__data_conf_free(Vstr_conf *conf)
36 6057 : {
37 6057 : unsigned int pos = 0;
38 :
39 6222 : while (pos < conf->data_usr_len)
40 : {
41 165 : if (conf->data_usr_ents[pos].name)
42 165 : vstr_ref_del(conf->data_usr_ents[pos].data);
43 :
44 165 : ++pos;
45 : }
46 :
47 6057 : VSTR__F(conf->data_usr_ents);
48 : }
49 :
50 : /* add / srch / del / get / set */
51 : unsigned int vstr_data_srch(Vstr_conf *passed_conf, const char *name)
52 141 : {
53 141 : Vstr_conf *conf = passed_conf ? passed_conf : vstr__options.def;
54 141 : unsigned int pos = 0;
55 :
56 141 : ASSERT_RET(name, 0);
57 209 : ASSERT(conf->data_usr_len <= conf->data_usr_sz);
58 :
59 328 : while (pos < conf->data_usr_len)
60 : {
61 289 : if (conf->data_usr_ents[pos].name &&
62 : !strcmp(name, conf->data_usr_ents[pos++].name))
63 98 : return (pos);
64 : }
65 :
66 39 : return (0);
67 : }
68 :
69 : unsigned int vstr_data_add(Vstr_conf *passed_conf,
70 : const char *name, Vstr_ref *data)
71 195 : {
72 195 : Vstr_conf *conf = passed_conf ? passed_conf : vstr__options.def;
73 195 : unsigned int len = conf->data_usr_len;
74 195 : unsigned int sz = conf->data_usr_sz;
75 195 : struct Vstr_data_usr *ents = NULL;
76 :
77 195 : ASSERT_RET(name, 0);
78 :
79 191 : ASSERT(!vstr_data_srch(conf, name));
80 :
81 191 : ASSERT(len <= sz);
82 :
83 191 : if (len == sz)
84 91 : len = 0; /* look for deleted enteries */
85 :
86 337 : while ((len < conf->data_usr_len) && conf->data_usr_ents[len].name)
87 146 : ++len;
88 :
89 191 : if (len == sz)
90 : {
91 91 : sz *= 2;
92 :
93 91 : if (!VSTR__MV(conf->data_usr_ents, ents, sizeof(struct Vstr_data_usr) * sz))
94 : {
95 1 : conf->malloc_bad = TRUE;
96 1 : return (0);
97 : }
98 90 : conf->data_usr_sz = sz;
99 : }
100 :
101 190 : conf->data_usr_ents[len].name = name;
102 190 : conf->data_usr_ents[len].data = data ? vstr_ref_add(data) : NULL;
103 :
104 190 : ++len;
105 190 : if (conf->data_usr_len < len)
106 190 : conf->data_usr_len = len;
107 :
108 190 : ASSERT(vstr_data_srch(conf, name));
109 :
110 190 : return (len);
111 : }
112 :
113 : void vstr_data_del(Vstr_conf *passed_conf, unsigned int pos)
114 29 : {
115 29 : Vstr_conf *conf = passed_conf ? passed_conf : vstr__options.def;
116 :
117 29 : ASSERT_RET_VOID(pos && (pos <= conf->data_usr_len));
118 :
119 25 : vstr_ref_del(conf->data_usr_ents[pos - 1].data);
120 :
121 25 : conf->data_usr_ents[pos - 1].name = NULL;
122 25 : conf->data_usr_ents[pos - 1].data = NULL;
123 :
124 25 : if (pos == conf->data_usr_len)
125 : {
126 40 : while (pos && !conf->data_usr_ents[pos - 1].name)
127 25 : { --pos; }
128 :
129 15 : conf->data_usr_len = pos;
130 : }
131 : }
132 :
133 : void *vstr_extern_inline_data_get(unsigned int pos)
134 64 : { /* global isn't exported */
135 64 : Vstr_conf *conf = vstr__options.def;
136 64 : return (vstr_data_get(conf, pos));
137 : }
138 :
139 : void vstr_extern_inline_data_set(unsigned int pos, Vstr_ref *ref)
140 20 : { /* global isn't exported */
141 20 : Vstr_conf *conf = vstr__options.def;
142 20 : return (vstr_data_set(conf, pos, ref));
143 : }
|