1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2009 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 struct idzebra_swig_service {
25 struct idzebra_swig_session {
30 /* == API errors, debug ==================================================== */
31 static Res api_error = 0;
32 const char* api_error_context = 0;
33 void api_add_error(const char *fmt, ...);
34 void api_clear_error(void);
35 void free_array(const char **args);
38 /* == API init, destroy =================================================== */
39 void idzebra_api_init(void)
41 yaz_log_init_prefix("API_SWIG");
42 yaz_log(YLOG_LOG, "IDZebra API initialized");
45 /* == Service start/stop =================================================== */
47 IDZebraService idzebra_start (RES_LIST)
49 IDZebraService srv = xmalloc(sizeof(*srv));
54 ARGS_PARSE ("configName",
66 srv->zs = zebra_start_res(res_get(func_res,"configName"), NULL, srv->res);
74 IDZEBRA_RES idzebra_stop(IDZebraService srv)
76 ZEBRA_RES rv = zebra_stop(srv->zs);
82 /* == Session open/close =================================================== */
83 IDZebraSession idzebra_open (IDZebraService srv, RES_LIST)
85 IDZebraSession sess = xmalloc(sizeof(*sess));
90 ARGS_PARSE ("tempfiles",
98 sess->zh = zebra_open(srv->zs, sess->res);
100 /* Function resources are kept for session (zh->res->over_res); */
107 IDZEBRA_RES idzebra_close(IDZebraSession sess)
109 ZEBRA_RES rv = zebra_close (sess->zh);
110 res_close (sess->res);
114 /* == Sample function == =================================================== */
115 IDZEBRA_RES idzebra_samplefunc(IDZebraSession sess, RES_LIST)
119 ARGS_PARSE("strucc");
120 ARGS_PROCESS(ARG_MODE_OPTIONAL,"encoding");
123 res_dump (sess->res,0);
128 res_remove_over(temp_res);
129 used = (const char **) res_get_array(local, "_used"); args_use(sess, sess->res, 0, ARG_MODE_FORCE, used);
138 -------------------------------------------------------------------------------
139 Utility functions for argument handling
140 -------------------------------------------------------------------------------
142 void arg_parse_res (Res r,
143 const char **valid_args,
145 char *name, char *value)
147 if ((name) && (value)) {
150 while (valid_args[i]) {
151 if (!strcmp(name, valid_args[i])) {
152 res_set(r, name, value);
159 yaz_log (YLOG_DEBUG, "skip: %s=%s",name, value);
160 res_add (skip, name, value);
164 void args_parse_res (Res r,
165 const char **valid_args,
173 while (args[i] && args[i+1]) {
175 argvalue = args[i++];
176 arg_parse_res (r, valid_args, skip, argname, argvalue);
181 /* == special resource handlers ============================================ */
183 void idzebra_res_encoding (ZebraHandle zh, const char *value)
186 zebra_octet_term_encoding(zh, value);
188 zebra_octet_term_encoding(zh, "ISO-8859-1");
192 void idzebra_res_estimatehits (ZebraHandle zh, const char *value)
196 if (! (sscanf(value, "%d", &val) == 1))
197 api_add_error( "Expected integer value for 'estimatehits'");
199 zebra_set_approx_limit(zh, val);
203 void idzebra_res_staticrank (ZebraHandle zh, const char *value)
207 if (! (sscanf(value, "%d", &val) == 1))
208 api_add_error( "Expected integer value for 'estimatehits'");
210 sess->zh->m_staticrank = val;
214 /* == applying and revoking call-scope resources =========================== */
216 void arg_use (ZebraHandle zh,
223 const char *value = res_get(r, name);
226 /* in FORCE mode resource is used with default value also */
227 if ((value) || (mode & ARG_MODE_FORCE)) {
230 if (!strcmp(name,"encoding")) {
231 idzebra_res_encoding(zh, value);
236 else if (!strcmp(name,"estimatehits")) {
237 idzebra_res_estimatehits(zh, value);
243 else if (!strcmp(name,"staticrank")) {
244 idzebra_res_staticrank(zh, value);
249 /* collects provided arguments in order to revoke them
250 at the end of the function */
252 if (! (mode & ARG_MODE_FORCE)) res_add(r, "_used", name);
253 if ( (rr) && (name) && (value) ) res_add(rr, name, value);
257 if (mode & ARG_MODE_MANDATORY)
258 api_add_error( "Missing mandatory argument '%s'", name);
263 void args_use (IDZebraSession sess,
272 arg_use (sess->zh, r, rr, mode, args[i++]);
277 /* == API errors =========================================================== */
279 void api_add_error(const char *fmt, ...)
288 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
292 vsnprintf(buf, sizeof(buf), fmt, ap);
294 vsprintf(buf, fmt, ap);
301 api_error = res_open(0,0);
303 if (api_error_context)
304 context = api_error_context;
306 context = "<unknown>";
308 res_add(api_error, context, buf);
311 char **api_errors(void)
313 static char **res = 0;
315 res = res_2_array(api_error);
317 res = xmalloc(sizeof(char *));
325 int api_check_error(void)
333 void api_clear_error(void)
336 res_close(api_error);
340 void free_array(const char **args)
345 xfree((char *) args[i]);
354 * c-file-style: "Stroustrup"
355 * indent-tabs-mode: nil
357 * vim: shiftwidth=4 tabstop=8 expandtab