1 #include <idzebra/api_swig.h>
4 #define DEFAULT_APPROX_LIMIT 2000000000
6 /* == Internal structures ================================================== */
8 struct idzebra_swig_service {
13 struct idzebra_swig_session {
18 /* == API errors, debug ==================================================== */
19 static Res api_error = 0;
20 const char* api_error_context = 0;
21 void api_add_error(const char *fmt, ...);
22 void api_clear_error(void);
23 void free_array(const char **args);
26 /* == API init, destroy =================================================== */
27 void idzebra_api_init(void)
29 yaz_log_init_prefix("API_SWIG");
30 yaz_log(YLOG_LOG, "IDZebra API initialized");
33 /* == Service start/stop =================================================== */
35 IDZebraService idzebra_start (RES_LIST)
37 IDZebraService srv = xmalloc(sizeof(*srv));
42 ARGS_PARSE ("configName",
54 srv->zs = zebra_start_res(res_get(func_res,"configName"), NULL, srv->res);
62 IDZEBRA_RES idzebra_stop(IDZebraService srv)
64 ZEBRA_RES rv = zebra_stop(srv->zs);
70 /* == Session open/close =================================================== */
71 IDZebraSession idzebra_open (IDZebraService srv, RES_LIST)
73 IDZebraSession sess = xmalloc(sizeof(*sess));
78 ARGS_PARSE ("tempfiles",
86 sess->zh = zebra_open(srv->zs, sess->res);
88 /* Function resources are kept for session (zh->res->over_res); */
95 IDZEBRA_RES idzebra_close(IDZebraSession sess)
97 ZEBRA_RES rv = zebra_close (sess->zh);
98 res_close (sess->res);
102 /* == Sample function == =================================================== */
103 IDZEBRA_RES idzebra_samplefunc(IDZebraSession sess, RES_LIST)
107 ARGS_PARSE("strucc");
108 ARGS_PROCESS(ARG_MODE_OPTIONAL,"encoding");
111 res_dump (sess->res,0);
116 res_remove_over(temp_res);
117 used = (const char **) res_get_array(local, "_used"); args_use(sess, sess->res, 0, ARG_MODE_FORCE, used);
126 -------------------------------------------------------------------------------
127 Utility functions for argument handling
128 -------------------------------------------------------------------------------
130 void arg_parse_res (Res r,
131 const char **valid_args,
133 char *name, char *value)
135 if ((name) && (value)) {
138 while (valid_args[i]) {
139 if (!strcmp(name, valid_args[i])) {
140 res_set(r, name, value);
147 yaz_log (YLOG_DEBUG, "skip: %s=%s",name, value);
148 res_add (skip, name, value);
152 void args_parse_res (Res r,
153 const char **valid_args,
161 while (args[i] && args[i+1]) {
163 argvalue = args[i++];
164 arg_parse_res (r, valid_args, skip, argname, argvalue);
169 /* == special resource handlers ============================================ */
171 void idzebra_res_encoding (ZebraHandle zh, const char *value)
174 zebra_octet_term_encoding(zh, value);
176 zebra_octet_term_encoding(zh, "ISO-8859-1");
180 void idzebra_res_estimatehits (ZebraHandle zh, const char *value)
184 if (! (sscanf(value, "%d", &val) == 1))
185 api_add_error( "Expected integer value for 'estimatehits'");
187 zebra_set_approx_limit(zh, val);
191 void idzebra_res_staticrank (ZebraHandle zh, const char *value)
195 if (! (sscanf(value, "%d", &val) == 1))
196 api_add_error( "Expected integer value for 'estimatehits'");
198 sess->zh->m_staticrank = val;
202 /* == applying and revoking call-scope resources =========================== */
204 void arg_use (ZebraHandle zh,
211 const char *value = res_get(r, name);
214 /* in FORCE mode resource is used with default value also */
215 if ((value) || (mode & ARG_MODE_FORCE)) {
218 if (!strcmp(name,"encoding")) {
219 idzebra_res_encoding(zh, value);
224 else if (!strcmp(name,"estimatehits")) {
225 idzebra_res_estimatehits(zh, value);
231 else if (!strcmp(name,"staticrank")) {
232 idzebra_res_staticrank(zh, value);
237 /* collects provided arguments in order to revoke them
238 at the end of the function */
240 if (! (mode & ARG_MODE_FORCE)) res_add(r, "_used", name);
241 if ( (rr) && (name) && (value) ) res_add(rr, name, value);
245 if (mode & ARG_MODE_MANDATORY)
246 api_add_error( "Missing mandatory argument '%s'", name);
251 void args_use (IDZebraSession sess,
260 arg_use (sess->zh, r, rr, mode, args[i++]);
265 /* == API errors =========================================================== */
267 void api_add_error(const char *fmt, ...)
276 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
280 vsnprintf(buf, sizeof(buf), fmt, ap);
282 vsprintf(buf, fmt, ap);
289 api_error = res_open(0,0);
291 if (api_error_context)
292 context = api_error_context;
294 context = "<unknown>";
296 res_add(api_error, context, buf);
299 char **api_errors(void)
301 static char **res = 0;
303 res = res_2_array(api_error);
305 res = xmalloc(sizeof(char *));
313 int api_check_error(void)
321 void api_clear_error(void)
324 res_close(api_error);
328 void free_array(const char **args)
333 xfree((char *) args[i]);