1 #include <idzebra/api_swig.h>
2 #include <idzebra/res.h>
3 #include <idzebra/api.h>
7 #define DEFAULT_APPROX_LIMIT 2000000000
9 /* == API errors, debug ==================================================== */
10 static Res api_error = 0;
11 const char* api_error_context = 0;
12 void api_add_error(const char *fmt, ...);
13 void api_clear_error(void);
14 void free_array(const char **args);
15 ZebraHandle zebra_get_handle (ZebraService zs);
18 /* == API init, destroy =================================================== */
19 void idzebra_api_init(void)
21 yaz_log_init_prefix("API_SWIG");
22 yaz_log(YLOG_LOG, "IDZebra API initialized");
25 /* == Service start/stop =================================================== */
27 ZebraService idzebra_start(RES_LIST)
34 ARGS_PARSE ("configName",
45 zs = zebra_start_res(res_get(func_res,"configName"), NULL, func_res);
47 /* Function resources are kept for service (zs->global_res); */
54 IDZEBRA_RES idzebra_stop(ZebraService zs)
56 /* Global res have an over part here */
57 res_close_over(zs->global_res);
58 return (zebra_stop(zs));
61 /* == Session open/close =================================================== */
62 ZebraHandle idzebra_open (ZebraService zs, RES_LIST)
70 ARGS_PARSE ("tempfiles",
77 zh = zebra_open(zs, func_res);
79 /* Function resources are kept for session (zh->res->over_res); */
83 yaz_log (YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
88 IDZEBRA_RES idzebra_close(ZebraHandle zh)
90 res_close_over(zh->session_res);
91 return (zebra_close(zh));
93 /* == Sample function == =================================================== */
94 IDZEBRA_RES idzebra_samplefunc(ZebraHandle zh, RES_LIST)
99 ARGS_PROCESS(ARG_MODE_OPTIONAL,"encoding");
102 yaz_log (YLOG_DEBUG, "Got strucc:%s\n",res_get(zh->res,"strucc"));
103 res_dump (zh->res,0);
112 -------------------------------------------------------------------------------
113 Utility functions for argument handling
114 -------------------------------------------------------------------------------
116 void arg_parse_res (Res r,
117 const char **valid_args,
119 char *name, char *value)
121 if ((name) && (value)) {
124 while (valid_args[i]) {
125 if (!strcmp(name, valid_args[i])) {
126 res_set(r, name, value);
133 yaz_log (YLOG_DEBUG, "skip: %s=%s",name, value);
134 res_add (skip, name, value);
138 void args_parse_res (Res r,
139 const char **valid_args,
147 while (args[i] && args[i+1]) {
149 argvalue = args[i++];
150 arg_parse_res (r, valid_args, skip, argname, argvalue);
155 /* == special resource handlers ============================================ */
157 void idzebra_res_encoding (ZebraHandle zh, const char *value)
160 zebra_octet_term_encoding(zh, value);
162 zebra_octet_term_encoding(zh, "ISO-8859-1");
166 void idzebra_res_estimatehits (ZebraHandle zh, const char *value)
170 if (! (sscanf(value, "%d", &val) == 1))
171 api_add_error( "Expected integer value for 'estimatehits'");
173 zebra_set_approx_limit(zh, val);
176 void idzebra_res_staticrank (ZebraHandle zh, const char *value)
180 if (! (sscanf(value, "%d", &val) == 1))
181 api_add_error( "Expected integer value for 'estimatehits'");
183 zh->m_staticrank = val;
186 /* == applying and revoking call-scope resources =========================== */
188 void arg_use (ZebraHandle zh,
195 const char *value = res_get(r, name);
198 /* in FORCE mode resource is used with default value also */
199 if ((value) || (mode & ARG_MODE_FORCE)) {
202 if (!strcmp(name,"encoding")) {
203 idzebra_res_encoding(zh, value);
208 else if (!strcmp(name,"estimatehits")) {
209 idzebra_res_estimatehits(zh, value);
214 else if (!strcmp(name,"staticrank")) {
215 idzebra_res_staticrank(zh, value);
219 /* collects provided arguments in order to revoke them
220 at the end of the function */
222 if (! (mode & ARG_MODE_FORCE)) res_add(r, "_used", name);
223 if ( (rr) && (name) && (value) ) res_add(rr, name, value);
227 if (mode & ARG_MODE_MANDATORY)
228 api_add_error( "Missing mandatory argument '%s'", name);
233 void args_use (ZebraHandle zh,
242 arg_use (zh, r, rr, mode, args[i++]);
247 /* == API errors =========================================================== */
249 void api_add_error(const char *fmt, ...)
258 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
262 vsnprintf(buf, sizeof(buf), fmt, ap);
264 vsprintf(buf, fmt, ap);
271 api_error = res_open(0,0);
273 if (api_error_context)
274 context = api_error_context;
276 context = "<unknown>";
278 res_add(api_error, context, buf);
281 char **api_errors(void)
283 static char **res = 0;
285 res = res_2_array(api_error);
287 res = xmalloc(sizeof(char *));
295 int api_check_error(void)
303 void api_clear_error(void)
306 res_close(api_error);
310 void free_array(const char **args)
315 xfree((char *) args[i]);