diff options
Diffstat (limited to 'src/dictionary.c')
-rw-r--r-- | src/dictionary.c | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/src/dictionary.c b/src/dictionary.c index b7baf05..2b995d4 100644 --- a/src/dictionary.c +++ b/src/dictionary.c | |||
@@ -39,26 +39,25 @@ static struct dictionary_descr dictionary_tab[] = { | |||
39 | { "sql", sql_init_dictionary, sql_done_dictionary, sql_free_result, | 39 | { "sql", sql_init_dictionary, sql_done_dictionary, sql_free_result, |
40 | sql_open, NULL, sql_get_dictionary, sql_lookup_dictionary, sql_quote }, | 40 | sql_open, NULL, sql_get_dictionary, sql_lookup_dictionary, sql_quote }, |
41 | { "builtin", builtin_init, builtin_done, builtin_free_result, | 41 | { "builtin", builtin_init, builtin_done, builtin_free_result, |
42 | builtin_open, NULL, | 42 | builtin_open, NULL, |
43 | builtin_get, | 43 | builtin_get, |
44 | builtin_lookup }, | 44 | builtin_lookup }, |
45 | { "external", NULL, NULL, NULL, NULL, NULL, NULL, NULL } | 45 | { "external", NULL, NULL, NULL, NULL, NULL, NULL, NULL } |
46 | }; | 46 | }; |
47 | 47 | ||
48 | struct dictionary * | 48 | struct dictionary * |
49 | dictionary_new (enum dictionary_id id, enum dictionary_type type) | 49 | dictionary_new (enum dictionary_id id, enum dictionary_type type) |
50 | { | 50 | { |
51 | struct dictionary *mp = xmalloc (sizeof mp[0]); | 51 | struct dictionary *mp = grecs_zalloc (sizeof mp[0]); |
52 | memset (mp, 0, sizeof mp[0]); | ||
53 | mp->id = id; | 52 | mp->id = id; |
54 | mp->type = type; | 53 | mp->type = type; |
55 | return mp; | 54 | return mp; |
56 | } | 55 | } |
57 | 56 | ||
58 | int | 57 | int |
59 | dictionary_init (struct dictionary *dict) | 58 | dictionary_init (struct dictionary *dict) |
60 | { | 59 | { |
61 | struct dictionary_descr *mp = dictionary_tab + dict->type; | 60 | struct dictionary_descr *mp = dictionary_tab + dict->type; |
62 | int rc = 0; | 61 | int rc = 0; |
63 | 62 | ||
64 | if (dict->init_passed++) | 63 | if (dict->init_passed++) |
@@ -83,24 +82,26 @@ dictionary_open (struct dictionary *dict) | |||
83 | { | 82 | { |
84 | struct dictionary_descr *mp = dictionary_tab + dict->type; | 83 | struct dictionary_descr *mp = dictionary_tab + dict->type; |
85 | 84 | ||
86 | if (!mp->open) | 85 | if (!mp->open) |
87 | return NULL; | 86 | return NULL; |
88 | return mp->open (dict); | 87 | return mp->open (dict); |
89 | } | 88 | } |
90 | 89 | ||
91 | int | 90 | int |
92 | dictionary_close (struct dictionary *dict, void *handle) | 91 | dictionary_close (struct dictionary *dict, void *handle) |
93 | { | 92 | { |
94 | struct dictionary_descr *mp = dictionary_tab + dict->type; | 93 | struct dictionary_descr *mp = dictionary_tab + dict->type; |
94 | if (mp->free) | ||
95 | mp->free (dict, handle); | ||
95 | if (!mp->close) | 96 | if (!mp->close) |
96 | return 0; | 97 | return 0; |
97 | return mp->close (dict, handle); | 98 | return mp->close (dict, handle); |
98 | } | 99 | } |
99 | 100 | ||
100 | int | 101 | int |
101 | dictionary_done (struct dictionary *dict) | 102 | dictionary_done (struct dictionary *dict) |
102 | { | 103 | { |
103 | struct dictionary_descr *mp = dictionary_tab + dict->type; | 104 | struct dictionary_descr *mp = dictionary_tab + dict->type; |
104 | int rc = 0; | 105 | int rc = 0; |
105 | 106 | ||
106 | if (dict->init_passed == 0) | 107 | if (dict->init_passed == 0) |
@@ -177,52 +178,52 @@ dictionary_result (struct dictionary *dict, void *handle, | |||
177 | if (nrow >= dict->nrow || ncol >= dict->ncol | 178 | if (nrow >= dict->nrow || ncol >= dict->ncol |
178 | || mp->get (dict, handle, nrow, ncol)) | 179 | || mp->get (dict, handle, nrow, ncol)) |
179 | return NULL; | 180 | return NULL; |
180 | return dict->result; | 181 | return dict->result; |
181 | } | 182 | } |
182 | 183 | ||
183 | void | 184 | void |
184 | dictionary_copy_result (struct dictionary *dict, const char *res, size_t size) | 185 | dictionary_copy_result (struct dictionary *dict, const char *res, size_t size) |
185 | { | 186 | { |
186 | if (dict->result_size < size + 1) | 187 | if (dict->result_size < size + 1) |
187 | { | 188 | { |
188 | dict->result_size = size + 1; | 189 | dict->result_size = size + 1; |
189 | dict->result = x2realloc (dict->result, &dict->result_size); | 190 | dict->result = grecs_realloc (dict->result, dict->result_size); |
190 | } | 191 | } |
191 | memcpy (dict->result, res, size); | 192 | memcpy (dict->result, res, size); |
192 | dict->result[size] = 0; | 193 | dict->result[size] = 0; |
193 | } | 194 | } |
194 | 195 | ||
195 | /* Quote non-printable characters in INPUT. Point *OUTPUT to the malloc'ed | 196 | /* Quote non-printable characters in INPUT. Point *OUTPUT to the malloc'ed |
196 | quoted string. Return its length. */ | 197 | quoted string. Return its length. */ |
197 | int | 198 | int |
198 | dictionary_quote_string (struct dictionary *dict, void *handle, | 199 | dictionary_quote_string (struct dictionary *dict, void *handle, |
199 | const char *input, | 200 | const char *input, |
200 | char **poutput, size_t *psize) | 201 | char **poutput, size_t *psize) |
201 | { | 202 | { |
202 | struct dictionary_descr *mp = dictionary_tab + dict->type; | 203 | struct dictionary_descr *mp = dictionary_tab + dict->type; |
203 | size_t size; | 204 | size_t size; |
204 | int quote; | 205 | int quote; |
205 | char *output; | 206 | char *output; |
206 | 207 | ||
207 | if (!input) | 208 | if (!input) |
208 | { | 209 | { |
209 | *poutput = xmalloc (1); | 210 | *poutput = grecs_malloc (1); |
210 | (*poutput)[0] = 0; | 211 | (*poutput)[0] = 0; |
211 | *psize = 1; | 212 | *psize = 1; |
212 | return 0; | 213 | return 0; |
213 | } | 214 | } |
214 | 215 | ||
215 | if (mp->quote) | 216 | if (mp->quote) |
216 | return mp->quote (dict, handle, input, poutput, psize); | 217 | return mp->quote (dict, handle, input, poutput, psize); |
217 | 218 | ||
218 | size = wordsplit_c_quoted_length (input, 0, "e); | 219 | size = wordsplit_c_quoted_length (input, 0, "e); |
219 | output = xmalloc (size + 1); | 220 | output = grecs_malloc (size + 1); |
220 | wordsplit_c_quote_copy (output, input, 0); | 221 | wordsplit_c_quote_copy (output, input, 0); |
221 | output[size] = 0; | 222 | output[size] = 0; |
222 | 223 | ||
223 | *poutput = output; | 224 | *poutput = output; |
224 | if (psize) | 225 | if (psize) |
225 | *psize = size; | 226 | *psize = size; |
226 | return 0; | 227 | return 0; |
227 | } | 228 | } |
228 | 229 | ||