diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2009-12-21 15:57:21 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2009-12-21 15:57:57 +0200 |
commit | 43ba0c3da9ff8913f0286a01a82875fa59601dc0 (patch) | |
tree | fac6765cc2fb90ae39f5eb47d2c41abcaf93fb6d /src/dictionary.c | |
parent | 30e1c54062fe7098b9c71601370df1ce27f873d3 (diff) | |
download | wydawca-43ba0c3da9ff8913f0286a01a82875fa59601dc0.tar.gz wydawca-43ba0c3da9ff8913f0286a01a82875fa59601dc0.tar.bz2 |
Namespace cleanup.
Rename "access method" to "dictionary".
All sources affected.
* src/method.c: renamed to...
* src/dictionary.c: ... this.
Diffstat (limited to 'src/dictionary.c')
-rw-r--r-- | src/dictionary.c | 228 |
1 files changed, 228 insertions, 0 deletions
diff --git a/src/dictionary.c b/src/dictionary.c new file mode 100644 index 0000000..c31baf8 --- /dev/null +++ b/src/dictionary.c | |||
@@ -0,0 +1,228 @@ | |||
1 | /* wydawca - automatic release submission daemon | ||
2 | Copyright (C) 2007 Sergey Poznyakoff | ||
3 | |||
4 | Wydawca is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU General Public License as published by the | ||
6 | Free Software Foundation; either version 3 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | Wydawca is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License along | ||
15 | with wydawca. If not, see <http://www.gnu.org/licenses/>. */ | ||
16 | |||
17 | #include "wydawca.h" | ||
18 | #include "sql.h" | ||
19 | #include "builtin.h" | ||
20 | |||
21 | struct dictionary_descr | ||
22 | { | ||
23 | const char *name; | ||
24 | |||
25 | int (*init) (struct dictionary *); | ||
26 | int (*done) (struct dictionary *); | ||
27 | int (*free) (struct dictionary *, void *); | ||
28 | |||
29 | void *(*open) (struct dictionary *); | ||
30 | int (*close) (struct dictionary *, void *); | ||
31 | |||
32 | int (*get) (struct dictionary *, void *, unsigned, unsigned); | ||
33 | int (*lookup) (struct dictionary *, void *, const char *); | ||
34 | int (*quote) (struct dictionary *, void *, const char *, char **, size_t *); | ||
35 | }; | ||
36 | |||
37 | static struct dictionary_descr dictionary_tab[] = { | ||
38 | { "none", NULL, NULL, NULL, NULL, NULL, NULL, NULL }, | ||
39 | { "sql", sql_init_dictionary, sql_done_dictionary, sql_free_result, | ||
40 | sql_open, NULL, sql_get_dictionary, sql_lookup_dictionary, sql_quote }, | ||
41 | { "builtin", builtin_init, builtin_done, builtin_free_result, | ||
42 | builtin_open, NULL, | ||
43 | builtin_get, | ||
44 | builtin_lookup }, | ||
45 | { "external", NULL, NULL, NULL, NULL, NULL, NULL, NULL } | ||
46 | }; | ||
47 | |||
48 | struct dictionary * | ||
49 | dictionary_new (enum dictionary_id id, enum dictionary_type type) | ||
50 | { | ||
51 | struct dictionary *mp = xmalloc (sizeof mp[0]); | ||
52 | memset (mp, 0, sizeof mp[0]); | ||
53 | mp->id = id; | ||
54 | mp->type = type; | ||
55 | return mp; | ||
56 | } | ||
57 | |||
58 | int | ||
59 | dictionary_init (struct dictionary *dict) | ||
60 | { | ||
61 | struct dictionary_descr *mp = dictionary_tab + dict->type; | ||
62 | int rc = 0; | ||
63 | |||
64 | if (dict->init_passed++) | ||
65 | return 0; | ||
66 | if (debug_level > 1) | ||
67 | { | ||
68 | int i; | ||
69 | logmsg (LOG_DEBUG, _("initializing dictionary: %s \"%s\""), | ||
70 | mp->name, SP (dict->query)); | ||
71 | for (i = 0; i < dict->parmc; i++) | ||
72 | logmsg (LOG_DEBUG, " parmv[%d]=%s", i, dict->parmv[i]); | ||
73 | } | ||
74 | if (mp->init) | ||
75 | rc = mp->init (dict); | ||
76 | if (rc == 0) | ||
77 | dict->init_passed = 1; | ||
78 | return rc; | ||
79 | } | ||
80 | |||
81 | void * | ||
82 | dictionary_open (struct dictionary *dict) | ||
83 | { | ||
84 | struct dictionary_descr *mp = dictionary_tab + dict->type; | ||
85 | |||
86 | if (!mp->open) | ||
87 | return NULL; | ||
88 | return mp->open (dict); | ||
89 | } | ||
90 | |||
91 | int | ||
92 | dictionary_close (struct dictionary *dict, void *handle) | ||
93 | { | ||
94 | struct dictionary_descr *mp = dictionary_tab + dict->type; | ||
95 | if (!mp->close) | ||
96 | return 0; | ||
97 | return mp->close (dict, handle); | ||
98 | } | ||
99 | |||
100 | int | ||
101 | dictionary_done (struct dictionary *dict) | ||
102 | { | ||
103 | struct dictionary_descr *mp = dictionary_tab + dict->type; | ||
104 | int rc = 0; | ||
105 | |||
106 | if (dict->init_passed == 0) | ||
107 | return 0; | ||
108 | if (--dict->init_passed) | ||
109 | return 0; | ||
110 | if (debug_level > 1) | ||
111 | { | ||
112 | int i; | ||
113 | logmsg (LOG_DEBUG, _("closing dictionary: %s \"%s\""), | ||
114 | mp->name, SP (dict->query)); | ||
115 | for (i = 0; i < dict->parmc; i++) | ||
116 | logmsg (LOG_DEBUG, " parmv[%d]=%s", i, dict->parmv[i]); | ||
117 | } | ||
118 | if (mp->done) | ||
119 | rc = mp->done (dict); | ||
120 | free (dict->result); | ||
121 | dict->result = NULL; | ||
122 | dict->result_size = 0; | ||
123 | return rc; | ||
124 | } | ||
125 | |||
126 | int | ||
127 | dictionary_lookup (struct dictionary *dict, void *handle, const char *cmd) | ||
128 | { | ||
129 | struct dictionary_descr *mp = dictionary_tab + dict->type; | ||
130 | |||
131 | if (debug_level > 1) | ||
132 | { | ||
133 | if (cmd) | ||
134 | logmsg (LOG_DEBUG, _("dictionary lookup: %s \"%s\""), | ||
135 | mp->name, cmd); | ||
136 | else | ||
137 | logmsg (LOG_DEBUG, _("dictionary lookup: %s"), mp->name); | ||
138 | } | ||
139 | |||
140 | if (!dict->init_passed) | ||
141 | { | ||
142 | logmsg (LOG_CRIT, | ||
143 | _("INTERNAL ERROR: dictionary %s \"%s\" not initialized"), | ||
144 | mp->name, SP (dict->query)); | ||
145 | return 1; | ||
146 | } | ||
147 | if (!mp->lookup) | ||
148 | { | ||
149 | logmsg (LOG_CRIT, | ||
150 | _("INTERNAL ERROR: no lookup function for dictionary %s \"%s\""), | ||
151 | mp->name, SP (dict->query)); | ||
152 | return 1; | ||
153 | } | ||
154 | if (mp->free) | ||
155 | mp->free (dict, handle); | ||
156 | return mp->lookup (dict, handle, cmd); | ||
157 | } | ||
158 | |||
159 | unsigned | ||
160 | dictionary_num_rows (struct dictionary *dict) | ||
161 | { | ||
162 | return dict->nrow; | ||
163 | } | ||
164 | |||
165 | unsigned | ||
166 | dictionary_num_cols (struct dictionary *dict) | ||
167 | { | ||
168 | return dict->ncol; | ||
169 | } | ||
170 | |||
171 | const char * | ||
172 | dictionary_result (struct dictionary *dict, void *handle, | ||
173 | unsigned nrow, unsigned ncol) | ||
174 | { | ||
175 | struct dictionary_descr *mp = dictionary_tab + dict->type; | ||
176 | |||
177 | if (nrow >= dict->nrow || ncol >= dict->ncol | ||
178 | || mp->get (dict, handle, nrow, ncol)) | ||
179 | return NULL; | ||
180 | return dict->result; | ||
181 | } | ||
182 | |||
183 | void | ||
184 | dictionary_copy_result (struct dictionary *dict, const char *res, size_t size) | ||
185 | { | ||
186 | if (dict->result_size < size + 1) | ||
187 | { | ||
188 | dict->result_size = size + 1; | ||
189 | dict->result = x2realloc (dict->result, &dict->result_size); | ||
190 | } | ||
191 | memcpy (dict->result, res, size); | ||
192 | dict->result[size] = 0; | ||
193 | } | ||
194 | |||
195 | /* Quote non-printable characters in INPUT. Point *OUTPUT to the malloc'ed | ||
196 | quoted string. Return its length. */ | ||
197 | int | ||
198 | dictionary_quote_string (struct dictionary *dict, void *handle, | ||
199 | const char *input, | ||
200 | char **poutput, size_t *psize) | ||
201 | { | ||
202 | struct dictionary_descr *mp = dictionary_tab + dict->type; | ||
203 | size_t size; | ||
204 | int quote; | ||
205 | char *output; | ||
206 | |||
207 | if (!input) | ||
208 | { | ||
209 | *poutput = xmalloc (1); | ||
210 | (*poutput)[0] = 0; | ||
211 | *psize = 1; | ||
212 | return 0; | ||
213 | } | ||
214 | |||
215 | if (mp->quote) | ||
216 | return mp->quote (dict, handle, input, poutput, psize); | ||
217 | |||
218 | size = wordsplit_quoted_length (input, 0, "e); | ||
219 | output = xmalloc (size + 1); | ||
220 | wordsplit_quote_copy (output, input, 0); | ||
221 | output[size] = 0; | ||
222 | |||
223 | *poutput = output; | ||
224 | if (psize) | ||
225 | *psize = size; | ||
226 | return 0; | ||
227 | } | ||
228 | |||