From 3d3d0cebd23fa527a7e9d5d728e3c744acb37580 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 13 Sep 2005 11:51:05 +0000 Subject: [PATCH] Added parameter for zebra_open. --- dict/dicttest.c | 6 +- include/idzebra/api.h | 4 +- include/idzebra/res.h | 29 +++---- index/apitest.c | 4 +- index/index.h | 3 +- index/main.c | 6 +- index/zebraapi.c | 73 +++++++++--------- index/zebrash.c | 4 +- index/zserver.c | 4 +- test/api/safari1.c | 4 +- test/api/t1.c | 4 +- test/api/t10.c | 6 +- test/api/t11.c | 4 +- test/api/t12.c | 8 +- test/api/t13.c | 4 +- test/api/t2.c | 4 +- test/api/t3.c | 4 +- test/api/t4.c | 6 +- test/api/t5.c | 4 +- test/api/t6.c | 8 +- test/api/t7.c | 4 +- test/api/t8.c | 4 +- test/api/t9.c | 4 +- test/api/testlib.c | 6 +- test/api/testlib.h | 4 +- test/charmap/charmap1.c | 4 +- test/espec/t1.c | 4 +- test/filters/grs.marc.c | 4 +- test/filters/grs.xml.c | 4 +- test/filters/text.c | 4 +- test/marcxml/t1.c | 4 +- test/marcxml/t2.c | 4 +- test/mbox/mbox1.c | 4 +- test/rusmarc/t1.c | 4 +- test/sort/sort1.c | 4 +- test/sort2/t1.c | 4 +- test/xpath/xpath1.c | 4 +- test/xpath/xpath2.c | 4 +- test/xpath/xpath3.c | 4 +- test/xpath/xpath4.c | 4 +- test/xpath/xpath5.c | 4 +- test/xpath/xpath6.c | 4 +- test/xslt/xslt1.c | 4 +- test/xslt/xslt2.c | 4 +- test/xslt/xslt3.c | 4 +- test/xslt/xslt4.c | 4 +- util/res.c | 197 ++++++++++++++++++----------------------------- 47 files changed, 226 insertions(+), 266 deletions(-) diff --git a/dict/dicttest.c b/dict/dicttest.c index 053d586..331f5f5 100644 --- a/dict/dicttest.c +++ b/dict/dicttest.c @@ -1,4 +1,4 @@ -/* $Id: dicttest.c,v 1.34 2005-04-15 10:47:48 adam Exp $ +/* $Id: dicttest.c,v 1.35 2005-09-13 11:51:05 adam Exp $ Copyright (C) 1995-2005 Index Data ApS @@ -160,12 +160,14 @@ int main (int argc, char **argv) yaz_log (YLOG_FATAL, "no config and/or dictionary specified"); exit (1); } - my_resource = res_open (config, 0, 0); + my_resource = res_open(0, 0); if (!my_resource) { yaz_log (YLOG_FATAL, "cannot open resource `%s'", config); exit (1); } + res_read_file(my_resource, config); + bfs = bfs_create (res_get(my_resource, "register"), 0); if (!bfs) { diff --git a/include/idzebra/api.h b/include/idzebra/api.h index cf6c387..c505c90 100644 --- a/include/idzebra/api.h +++ b/include/idzebra/api.h @@ -1,4 +1,4 @@ -/* $Id: api.h,v 1.29 2005-08-09 10:01:03 adam Exp $ +/* $Id: api.h,v 1.30 2005-09-13 11:51:05 adam Exp $ Copyright (C) 1995-2005 Index Data ApS @@ -129,7 +129,7 @@ void zebra_filter_info(ZebraService zs, void *cd, one handle is sufficient */ YAZ_EXPORT -ZebraHandle zebra_open(ZebraService zs); +ZebraHandle zebra_open(ZebraService zs, Res res); /** \brief Destroys Zebra session handle. diff --git a/include/idzebra/res.h b/include/idzebra/res.h index 8c68715..68e2c90 100644 --- a/include/idzebra/res.h +++ b/include/idzebra/res.h @@ -1,4 +1,4 @@ -/* $Id: res.h,v 1.6 2005-08-17 21:28:07 adam Exp $ +/* $Id: res.h,v 1.7 2005-09-13 11:51:06 adam Exp $ Copyright (C) 1995-2005 Index Data ApS @@ -30,36 +30,39 @@ YAZ_BEGIN_CDECL typedef struct res_struct *Res; YAZ_EXPORT -Res res_open (const char *name, Res res_def, Res over_res); +Res res_open(Res res_def, Res over_res); YAZ_EXPORT -void res_close (Res r); +void res_close(Res r); YAZ_EXPORT -void res_clear (Res r); +ZEBRA_RES res_read_file(Res r, const char *fname); YAZ_EXPORT -const char *res_get (Res r, const char *name); +ZEBRA_RES res_write_file(Res r, const char *fname); YAZ_EXPORT -const char *res_get_def (Res r, const char *name, const char *def); +void res_clear(Res r); YAZ_EXPORT -int res_get_match (Res r, const char *name, const char *value, const char *s); +const char *res_get(Res r, const char *name); YAZ_EXPORT -void res_set (Res r, const char *name, const char *value); +const char *res_get_def(Res r, const char *name, const char *def); YAZ_EXPORT -int res_trav (Res r, const char *prefix, void *p, - void (*f)(void *p, const char *name, const char *value)); +int res_get_match(Res r, const char *name, const char *value, const char *s); + +YAZ_EXPORT +void res_set(Res r, const char *name, const char *value); YAZ_EXPORT -int res_write (Res r); +int res_trav(Res r, const char *prefix, void *p, + void (*f)(void *p, const char *name, const char *value)); YAZ_EXPORT -const char *res_get_prefix (Res r, const char *name, const char *prefix, - const char *def); +const char *res_get_prefix(Res r, const char *name, const char *prefix, + const char *def); YAZ_EXPORT ZEBRA_RES res_get_int(Res r, const char *name, int *val); diff --git a/index/apitest.c b/index/apitest.c index cd88853..0248f72 100644 --- a/index/apitest.c +++ b/index/apitest.c @@ -1,4 +1,4 @@ -/* $Id: apitest.c,v 1.22 2005-03-09 12:14:42 adam Exp $ +/* $Id: apitest.c,v 1.23 2005-09-13 11:51:06 adam Exp $ Copyright (C) 1995-2005 Index Data ApS @@ -146,7 +146,7 @@ int main (int argc, char **argv) exit (1); } /* open Zebra */ - zh = zebra_open (zs); + zh = zebra_open (zs, 0); if (!zh) { printf ("zebras_open failed\n"); diff --git a/index/index.h b/index/index.h index e9c8f50..b1f5c48 100644 --- a/index/index.h +++ b/index/index.h @@ -1,4 +1,4 @@ -/* $Id: index.h,v 1.149 2005-08-19 11:04:23 adam Exp $ +/* $Id: index.h,v 1.150 2005-09-13 11:51:06 adam Exp $ Copyright (C) 1995-2005 Index Data ApS @@ -282,6 +282,7 @@ struct zebra_session { int destroyed; ZebraSet sets; Res res; + Res session_res; char *user_perm; int errCode; zint hits; diff --git a/index/main.c b/index/main.c index c1709bc..1ced237 100644 --- a/index/main.c +++ b/index/main.c @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.126 2005-08-26 10:11:49 adam Exp $ +/* $Id: main.c,v 1.127 2005-09-13 11:51:06 adam Exp $ Copyright (C) 1995-2005 Index Data ApS @@ -63,7 +63,7 @@ int main (int argc, char **argv) int nsections = 0; int enable_commit = 1; char *database = 0; - Res res = res_open(0, 0, 0); + Res res = res_open(0, 0); int trans_started=0; #if HAVE_SYS_TIMES_H @@ -135,7 +135,7 @@ int main (int argc, char **argv) yaz_log (YLOG_FATAL, "Cannot read config %s", config); exit (1); } - zh = zebra_open (zs); + zh = zebra_open (zs, 0); zebra_shadow_enable (zh, enable_commit); } diff --git a/index/zebraapi.c b/index/zebraapi.c index 40aa1cc..2ffaed2 100644 --- a/index/zebraapi.c +++ b/index/zebraapi.c @@ -1,4 +1,4 @@ -/* $Id: zebraapi.c,v 1.184 2005-08-22 09:04:18 adam Exp $ +/* $Id: zebraapi.c,v 1.185 2005-09-13 11:51:06 adam Exp $ Copyright (C) 1995-2005 Index Data ApS @@ -49,8 +49,8 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA static int log_level = 0; static int log_level_initialized = 0; -static Res zebra_open_res (ZebraHandle zh); -static void zebra_close_res (ZebraHandle zh); +static void zebra_open_res(ZebraHandle zh); +static void zebra_close_res(ZebraHandle zh); static void zebra_chdir (ZebraService zs) { @@ -79,14 +79,14 @@ static void zebra_flush_reg (ZebraHandle zh) zebra_index_merge (zh ); } -static struct zebra_register *zebra_register_open (ZebraService zs, - const char *name, - int rw, int useshadow, - Res res, - const char *reg_path); +static struct zebra_register *zebra_register_open(ZebraService zs, + const char *name, + int rw, int useshadow, + Res res, + const char *reg_path); static void zebra_register_close (ZebraService zs, struct zebra_register *reg); -ZebraHandle zebra_open (ZebraService zs) +ZebraHandle zebra_open(ZebraService zs, Res res) { ZebraHandle zh; const char *default_encoding; @@ -111,6 +111,7 @@ ZebraHandle zebra_open (ZebraService zs) zh->errCode = 0; zh->errString = 0; zh->res = 0; + zh->session_res = res_open(zs->global_res, res); zh->user_perm = 0; zh->reg_name = xstrdup (""); @@ -128,7 +129,7 @@ ZebraHandle zebra_open (ZebraService zs) zh->shadow_enable = 1; zh->m_staticrank = 0; - default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1"); + default_encoding = res_get_def(zh->session_res, "encoding", "ISO-8859-1"); zh->iconv_to_utf8 = yaz_iconv_open ("UTF-8", default_encoding); @@ -177,12 +178,15 @@ ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res) yaz_log(YLOG_LOG, "zebra_start %s %s", ZEBRAVER, configName ? configName : ""); - if ((res = res_open (configName, def_res, over_res))) + if ((res = res_open(def_res, over_res))) { const char *passwd_plain = 0; const char *passwd_encrypt = 0; ZebraService zh = xmalloc(sizeof(*zh)); + if (configName) + res_read_file(res, configName); + zh->global_res = res; zh->sessions = 0; @@ -246,9 +250,9 @@ Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw, } static -struct zebra_register *zebra_register_open (ZebraService zs, const char *name, - int rw, int useshadow, Res res, - const char *reg_path) +struct zebra_register *zebra_register_open(ZebraService zs, const char *name, + int rw, int useshadow, Res res, + const char *reg_path) { struct zebra_register *reg; int record_compression = REC_COMPRESS_NONE; @@ -533,6 +537,7 @@ ZEBRA_RES zebra_close (ZebraHandle zh) if (zh->reg) zebra_register_close (zh->service, zh->reg); zebra_close_res (zh); + res_close(zh->session_res); xfree(zh->record_encoding); @@ -583,38 +588,34 @@ struct map_baseinfo { int new_num_max; }; -static Res zebra_open_res (ZebraHandle zh) +static void zebra_open_res(ZebraHandle zh) { - Res res = 0; char fname[512]; ASSERTZH; zh->errCode = 0; if (zh->path_reg) { - sprintf (fname, "%.200s/zebra.cfg", zh->path_reg); - res = res_open (fname, zh->service->global_res, 0); - if (!res) - res = zh->service->global_res; + sprintf(fname, "%.200s/zebra.cfg", zh->path_reg); + zh->res = res_open(zh->session_res, 0); + res_read_file(zh->res, fname); } else if (*zh->reg_name == 0) { - res = zh->service->global_res; + zh->res = res_open(zh->session_res, 0); } else { yaz_log (YLOG_WARN, "no register root specified"); - return 0; /* no path for register - fail! */ + zh->res = 0; /* no path for register - fail! */ } - return res; } static void zebra_close_res (ZebraHandle zh) { ASSERTZH; zh->errCode = 0; - if (zh->res != zh->service->global_res) - res_close (zh->res); + res_close (zh->res); zh->res = 0; } @@ -655,7 +656,7 @@ static void zebra_select_register (ZebraHandle zh, const char *new_reg) strcat (zh->path_reg, zh->reg_name); } } - zh->res = zebra_open_res (zh); + zebra_open_res(zh); if (zh->lock_normal) zebra_lock_destroy (zh->lock_normal); @@ -668,7 +669,7 @@ static void zebra_select_register (ZebraHandle zh, const char *new_reg) if (zh->res) { char fname[512]; - const char *lock_area =res_get (zh->res, "lockDir"); + const char *lock_area = res_get (zh->res, "lockDir"); if (!lock_area && zh->path_reg) res_set (zh->res, "lockDir", zh->path_reg); @@ -749,8 +750,8 @@ int zebra_select_default_database(ZebraHandle zh) /* no database has been selected - so we select based on resource setting (including group) */ - const char *group = res_get(zh->service->global_res, "group"); - const char *v = res_get_prefix(zh->service->global_res, + const char *group = res_get(zh->session_res, "group"); + const char *v = res_get_prefix(zh->session_res, "database", group, "Default"); return zebra_select_database(zh, v); } @@ -777,7 +778,7 @@ void map_basenames (ZebraHandle zh, ODR stream, odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max); info.mem = stream->mem; - res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func); + res_trav (zh->session_res, "mapdb", &info, map_basenames_func); for (i = 0; inum_bases; i++) if (p->basenames[i] && p->new_num_bases < p->new_num_max) @@ -1623,9 +1624,9 @@ ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw) } zebra_set_state (zh, 'd', seqno); - zh->reg = zebra_register_open (zh->service, zh->reg_name, - 1, rval ? 1 : 0, zh->res, - zh->path_reg); + zh->reg = zebra_register_open(zh->service, zh->reg_name, + 1, rval ? 1 : 0, zh->res, + zh->path_reg); if (zh->reg) zh->reg->seqno = seqno; else @@ -1704,9 +1705,9 @@ ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw) resultSetInvalidate (zh); zebra_register_close (zh->service, zh->reg); } - zh->reg = zebra_register_open (zh->service, zh->reg_name, - 0, val == 'c' ? 1 : 0, - zh->res, zh->path_reg); + zh->reg = zebra_register_open(zh->service, zh->reg_name, + 0, val == 'c' ? 1 : 0, + zh->res, zh->path_reg); if (!zh->reg) { zebra_unlock (zh->lock_normal); diff --git a/index/zebrash.c b/index/zebrash.c index c9bbda4..b11a63e 100644 --- a/index/zebrash.c +++ b/index/zebrash.c @@ -1,4 +1,4 @@ -/* $Id: zebrash.c,v 1.34 2005-06-14 20:28:54 adam Exp $ +/* $Id: zebrash.c,v 1.35 2005-09-13 11:51:06 adam Exp $ Copyright (C) 1995-2005 Index Data ApS @@ -193,7 +193,7 @@ static int cmd_zebra_open( char *args[], WRBUF outbuff) if (!zs) wrbuf_puts(outbuff,"zebra seems not to have been started, " "trying anyway\n"); - zh=zebra_open(zs); + zh = zebra_open(zs, 0); return 0; /* ok */ } diff --git a/index/zserver.c b/index/zserver.c index 118887a..f1c92c4 100644 --- a/index/zserver.c +++ b/index/zserver.c @@ -1,4 +1,4 @@ -/* $Id: zserver.c,v 1.139 2005-08-22 09:04:18 adam Exp $ +/* $Id: zserver.c,v 1.140 2005-09-13 11:51:06 adam Exp $ Copyright (C) 1995-2005 Index Data ApS @@ -76,7 +76,7 @@ bend_initresult *bend_init (bend_initrequest *q) yaz_log (YLOG_DEBUG, "bend_init"); sob = statserv_getcontrol (); - if (!(zh = zebra_open (sob->handle))) + if (!(zh = zebra_open (sob->handle, 0))) { yaz_log (YLOG_WARN, "Failed to read config `%s'", sob->configname); r->errcode = YAZ_BIB1_PERMANENT_SYSTEM_ERROR; diff --git a/test/api/safari1.c b/test/api/safari1.c index 3f3c6da..02dfdfa 100644 --- a/test/api/safari1.c +++ b/test/api/safari1.c @@ -1,4 +1,4 @@ -/* $Id: safari1.c,v 1.5 2005-05-03 09:11:37 adam Exp $ +/* $Id: safari1.c,v 1.6 2005-09-13 11:51:07 adam Exp $ Copyright (C) 1995-2005 Index Data ApS @@ -60,7 +60,7 @@ int main(int argc, char **argv) zint limits[3]; ZebraService zs = start_up("safari.cfg", argc, argv); - ZebraHandle zh = zebra_open(zs); + ZebraHandle zh = zebra_open(zs, 0); init_data(zh, myrec); do_query(__LINE__, zh, "@attr 1=any the", 3); diff --git a/test/api/t1.c b/test/api/t1.c index 3f6a7ca..731c390 100644 --- a/test/api/t1.c +++ b/test/api/t1.c @@ -1,4 +1,4 @@ -/* $Id: t1.c,v 1.12 2005-01-15 19:38:35 adam Exp $ +/* $Id: t1.c,v 1.13 2005-09-13 11:51:07 adam Exp $ Copyright (C) 1995-2005 Index Data ApS @@ -28,7 +28,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA int main(int argc, char **argv) { ZebraService zs = start_up(0, argc, argv); - ZebraHandle zh = zebra_open(zs); + ZebraHandle zh = zebra_open(zs, 0); return close_down(zh, zs, 0); } diff --git a/test/api/t10.c b/test/api/t10.c index 56b68a2..97b3c67 100644 --- a/test/api/t10.c +++ b/test/api/t10.c @@ -1,4 +1,4 @@ -/* $Id: t10.c,v 1.8 2005-05-24 11:35:43 adam Exp $ +/* $Id: t10.c,v 1.9 2005-09-13 11:51:07 adam Exp $ Copyright (C) 1995-2005 Index Data ApS @@ -74,13 +74,13 @@ int main(int argc, char **argv) { int i; ZebraService zs = start_up("zebrazv.cfg", argc, argv); - ZebraHandle zh = zebra_open (zs); + ZebraHandle zh = zebra_open(zs, 0); init_data(zh, recs); zebra_close(zh); for (i = 0; tests[i].schema; i++) { - zh = zebra_open (zs); + zh = zebra_open(zs, 0); zebra_select_database(zh, "Default"); zebra_set_resource(zh, "zvrank.weighting-scheme", tests[i].schema); yaz_log(YLOG_LOG,"============%d: %s ===========", i, tests[i].schema); diff --git a/test/api/t11.c b/test/api/t11.c index ab59f20..8748ec0 100644 --- a/test/api/t11.c +++ b/test/api/t11.c @@ -1,4 +1,4 @@ -/* $Id: t11.c,v 1.2 2005-05-02 09:25:12 adam Exp $ +/* $Id: t11.c,v 1.3 2005-09-13 11:51:07 adam Exp $ Copyright (C) 1995-2005 Index Data ApS @@ -32,7 +32,7 @@ const char *myrec[] = { int main(int argc, char **argv) { ZebraService zs = start_up(0, argc, argv); - ZebraHandle zh = zebra_open(zs); + ZebraHandle zh = zebra_open(zs, 0); init_data(zh, myrec); diff --git a/test/api/t12.c b/test/api/t12.c index f20264a..ede4022 100644 --- a/test/api/t12.c +++ b/test/api/t12.c @@ -1,4 +1,4 @@ -/* $Id: t12.c,v 1.1 2005-05-09 13:22:44 adam Exp $ +/* $Id: t12.c,v 1.2 2005-09-13 11:51:07 adam Exp $ Copyright (C) 1995-2005 Index Data ApS @@ -29,13 +29,13 @@ int main(int argc, char **argv) int i; int no_db = 140; ZebraService zs = start_up(0, argc, argv); - ZebraHandle zh = zebra_open(zs); + ZebraHandle zh = zebra_open(zs, 0); zebra_select_database(zh, "Default"); zebra_init(zh); zebra_close(zh); - zh = zebra_open (zs); + zh = zebra_open(zs, 0); zebra_begin_trans (zh, 1); for (i = 0; ifirst) resp = r->last = r->first = - (struct res_entry *) xmalloc (sizeof(*resp)); + (struct res_entry *) xmalloc(sizeof(*resp)); else { - resp = (struct res_entry *) xmalloc (sizeof(*resp)); + resp = (struct res_entry *) xmalloc(sizeof(*resp)); r->last->next = resp; r->last = resp; } @@ -134,7 +132,7 @@ static char *xstrdup_env(const char *src) return dst; } -static void reread (Res r) +ZEBRA_RES res_read_file(Res r, const char *fname) { struct res_entry *resp; char *line; @@ -143,38 +141,21 @@ static void reread (Res r) char fr_buf[1024]; FILE *fr; - assert (r); - r->init = 1; - - if (!r->name) - return; + assert(r); - fr = fopen (r->name, "r"); + fr = fopen(fname, "r"); if (!fr) { - yaz_log (YLOG_WARN|YLOG_ERRNO, "Cannot open `%s'", r->name); - return ; + yaz_log(YLOG_WARN|YLOG_ERRNO, "Cannot open `%s'", fname); + return ZEBRA_FAIL; } - val_buf = (char*) xmalloc (val_max); + val_buf = (char*) xmalloc(val_max); while (1) { - line = fgets (fr_buf, sizeof(fr_buf)-1, fr); + line = fgets(fr_buf, sizeof(fr_buf)-1, fr); if (!line) break; - if (*line == '#') - { - int no = 0; - - while (fr_buf[no] && fr_buf[no] != '\n') - no++; - fr_buf[no] = '\0'; - - resp = add_entry (r); - resp->name = (char*) xmalloc (no+1); - resp->value = NULL; - strcpy (resp->name, fr_buf); - } - else + if (*line != '#') { int no = 0; while (1) @@ -184,18 +165,18 @@ static void reread (Res r) no = 0; break; } - if (strchr (": \t", fr_buf[no])) + if (strchr(": \t", fr_buf[no])) break; no++; } if (!no) continue; fr_buf[no++] = '\0'; - resp = add_entry (r); - resp->name = (char*) xmalloc (no); - strcpy (resp->name, fr_buf); + resp = add_entry(r); + resp->name = (char*) xmalloc(no); + strcpy(resp->name, fr_buf); - while (strchr (" \t", fr_buf[no])) + while (strchr(" \t", fr_buf[no])) no++; val_size = 0; while (1) @@ -208,13 +189,13 @@ static void reread (Res r) val_size--; val_buf[val_size] = '\0'; resp->value = xstrdup_env(val_buf); - yaz_log (YLOG_DEBUG, "(name=%s,value=%s)", + yaz_log(YLOG_DEBUG, "(name=%s,value=%s)", resp->name, resp->value); break; } - else if (fr_buf[no] == '\\' && strchr ("\n\r\f", fr_buf[no+1])) + else if (fr_buf[no] == '\\' && strchr("\n\r\f", fr_buf[no+1])) { - line = fgets (fr_buf, sizeof(fr_buf)-1, fr); + line = fgets(fr_buf, sizeof(fr_buf)-1, fr); if (!line) { val_buf[val_size] = '\0'; @@ -230,77 +211,56 @@ static void reread (Res r) { char *nb; - nb = (char*) xmalloc (val_max+=1024); - memcpy (nb, val_buf, val_size); - xfree (val_buf); + nb = (char*) xmalloc(val_max+=1024); + memcpy(nb, val_buf, val_size); + xfree(val_buf); val_buf = nb; } } } } } - xfree (val_buf); - fclose (fr); + xfree(val_buf); + fclose(fr); + return ZEBRA_OK; } -Res res_open (const char *name, Res def_res, Res over_res) +Res res_open(Res def_res, Res over_res) { Res r; - if (name) - { -#ifdef WIN32 - if (access (name, 4)) -#else - if (access (name, R_OK)) -#endif - { - yaz_log (YLOG_WARN|YLOG_ERRNO, "Cannot open `%s'", name); - return 0; - } - } - r = (Res) xmalloc (sizeof(*r)); - r->init = 0; + r = (Res) xmalloc(sizeof(*r)); r->first = r->last = NULL; - if (name) - r->name = xstrdup (name); - else - r->name=0; r->def_res = def_res; r->over_res = over_res; return r; } -void res_clear (Res r) +void res_clear(Res r) { - if (r->init) + struct res_entry *re, *re1; + for (re = r->first; re; re=re1) { - struct res_entry *re, *re1; - for (re = r->first; re; re=re1) - { - if (re->name) - xfree (re->name); - if (re->value) - xfree (re->value); - re1 = re->next; - xfree (re); - } + if (re->name) + xfree(re->name); + if (re->value) + xfree(re->value); + re1 = re->next; + xfree(re); } - r->init = 0; r->first = r->last = NULL; } -void res_close (Res r) +void res_close(Res r) { if (!r) return; res_clear(r); - xfree (r->name); - xfree (r); + xfree(r); } -const char *res_get_prefix (Res r, const char *name, const char *prefix, +const char *res_get_prefix(Res r, const char *name, const char *prefix, const char *def) { const char *v = 0;; @@ -322,7 +282,7 @@ const char *res_get_prefix (Res r, const char *name, const char *prefix, return v; } -const char *res_get (Res r, const char *name) +const char *res_get(Res r, const char *name) { struct res_entry *re; const char *v; @@ -334,59 +294,55 @@ const char *res_get (Res r, const char *name) if (v) return v; - if (!r->init) - reread (r); for (re = r->first; re; re=re->next) - if (re->value && !yaz_matchstr (re->name, name)) + if (re->value && !yaz_matchstr(re->name, name)) return re->value; - return res_get (r->def_res, name); + return res_get(r->def_res, name); } -const char *res_get_def (Res r, const char *name, const char *def) +const char *res_get_def(Res r, const char *name, const char *def) { const char *t; - if (!(t = res_get (r, name))) + if (!(t = res_get(r, name))) { - yaz_log (YLOG_DEBUG, "CAUTION: Using default resource %s:%s", name, def); + yaz_log(YLOG_DEBUG, "CAUTION: Using default resource %s:%s", name, def); return def; } else return t; } -int res_get_match (Res r, const char *name, const char *value, const char *s) +int res_get_match(Res r, const char *name, const char *value, const char *s) { - const char *cn = res_get (r, name); + const char *cn = res_get(r, name); if (!cn) cn = s; - if (cn && !yaz_matchstr (cn, value)) + if (cn && !yaz_matchstr(cn, value)) return 1; return 0; } -void res_set (Res r, const char *name, const char *value) +void res_set(Res r, const char *name, const char *value) { struct res_entry *re; - assert (r); - if (!r->init) - reread (r); + assert(r); for (re = r->first; re; re=re->next) - if (re->value && !yaz_matchstr (re->name, name)) + if (re->value && !yaz_matchstr(re->name, name)) { - xfree (re->value); - re->value = xstrdup_env (value); + xfree(re->value); + re->value = xstrdup_env(value); return; } - re = add_entry (r); - re->name = xstrdup (name); - re->value = xstrdup_env (value); + re = add_entry(r); + re->name = xstrdup(name); + re->value = xstrdup_env(value); } -int res_trav (Res r, const char *prefix, void *p, +int res_trav(Res r, const char *prefix, void *p, void (*f)(void *p, const char *name, const char *value)) { struct res_entry *re; @@ -395,38 +351,35 @@ int res_trav (Res r, const char *prefix, void *p, if (!r) return 0; + no = res_trav(r->over_res, prefix, p, f); + if (no) + return no; if (prefix) l = strlen(prefix); - if (!r->init) - reread (r); for (re = r->first; re; re=re->next) if (re->value) - if (l==0 || !memcmp (re->name, prefix, l)) + if (l==0 || !memcmp(re->name, prefix, l)) { (*f)(p, re->name, re->value); no++; } if (!no) - return res_trav (r->def_res, prefix, p, f); + return res_trav(r->def_res, prefix, p, f); return no; } -int res_write (Res r) +ZEBRA_RES res_write_file(Res r, const char *fname) { struct res_entry *re; FILE *fr; - assert (r); - if (!r->init) - reread (r); - if (!r->name) - return 0; /* ok, this was not from a file */ - fr = fopen (r->name, "w"); + assert(r); + fr = fopen(fname, "w"); if (!fr) { - yaz_log (YLOG_FATAL|YLOG_ERRNO, "Cannot create `%s'", r->name); - exit (1); + yaz_log(YLOG_FATAL|YLOG_ERRNO, "Cannot create `%s'", fname); + exit(1); } for (re = r->first; re; re=re->next) @@ -435,10 +388,10 @@ int res_write (Res r) int lefts = strlen(re->name)+2; if (!re->value) - fprintf (fr, "%s\n", re->name); + fprintf(fr, "%s\n", re->name); else { - fprintf (fr, "%s: ", re->name); + fprintf(fr, "%s: ", re->name); while (lefts + strlen(re->value+no) > 78) { int i = 20; @@ -452,16 +405,16 @@ int res_write (Res r) if (i<0) ind = no + 78 - lefts; for (i = no; i != ind; i++) - putc (re->value[i], fr); - fprintf (fr, "\\\n"); + putc(re->value[i], fr); + fprintf(fr, "\\\n"); no=ind; lefts = 0; } - fprintf (fr, "%s\n", re->value+no); + fprintf(fr, "%s\n", re->value+no); } } - fclose (fr); - return 0; + fclose(fr); + return ZEBRA_OK; } ZEBRA_RES res_get_int(Res r, const char *name, int *val) -- 1.7.10.4