X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=util%2Fzebramap.c;h=9c03d0b24d00cec3d0185359495559336734c9d9;hb=6d3b83ae7e008f2d61326051b03f7f07d3cc2ef0;hp=4116e529847651d553ed435cbfed7e8dcc9a7831;hpb=7c3a0352f0492609a3b6b26b63a72b0b2d207aab;p=idzebra-moved-to-github.git diff --git a/util/zebramap.c b/util/zebramap.c index 4116e52..9c03d0b 100644 --- a/util/zebramap.c +++ b/util/zebramap.c @@ -1,6 +1,6 @@ -/* $Id: zebramap.c,v 1.33 2004-09-14 14:38:08 quinn Exp $ - Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004 - Index Data Aps +/* $Id: zebramap.c,v 1.44 2006-03-13 17:40:28 mike Exp $ + Copyright (C) 1995-2005 + Index Data ApS This file is part of the Zebra server. @@ -20,27 +20,20 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - - #include +#include #include -#include #include -#include +#include + +#include #define ZEBRA_MAP_TYPE_SORT 1 #define ZEBRA_MAP_TYPE_INDEX 2 #define ZEBRA_REPLACE_ANY 300 -struct zm_token { - int *token_from; - char *token_to; - int token_min; - struct zm_token *next; -}; - struct zebra_map { unsigned reg_id; int completeness; @@ -57,7 +50,6 @@ struct zebra_map { chrmaptab maptab; const char *maptab_name; struct zebra_map *next; - struct zm_token *replace_tokens; }; struct zebra_maps { @@ -68,7 +60,7 @@ struct zebra_maps { char temp_map_str[2]; const char *temp_map_ptr[2]; struct zebra_map **lookup_array; - WRBUF wrbuf_1, wrbuf_2; + WRBUF wrbuf_1; }; void zebra_maps_close (ZebraMaps zms) @@ -81,12 +73,12 @@ void zebra_maps_close (ZebraMaps zms) zm = zm->next; } wrbuf_free (zms->wrbuf_1, 1); - wrbuf_free (zms->wrbuf_2, 1); nmem_destroy (zms->nmem); xfree (zms); } -static void zebra_map_read (ZebraMaps zms, const char *name) +/* Returns 0 if all is well, -1 on fatal error */ +static int zebra_map_read (ZebraMaps zms, const char *name, int fail_fatal) { FILE *f; char line[512]; @@ -97,8 +89,9 @@ static void zebra_map_read (ZebraMaps zms, const char *name) if (!(f = yaz_fopen(zms->tabpath, name, "r", zms->tabroot))) { - logf(LOG_WARN|LOG_ERRNO, "%s", name); - return ; + int level = fail_fatal ? YLOG_FATAL : YLOG_WARN; + yaz_log(level|YLOG_ERRNO, "%s", name); + return -1; } while ((argc = readconf_line(f, &lineno, line, 512, argv, 10))) { @@ -115,7 +108,6 @@ static void zebra_map_read (ZebraMaps zms, const char *name) (*zm)->type = ZEBRA_MAP_TYPE_INDEX; (*zm)->completeness = 0; (*zm)->positioned = 1; - (*zm)->replace_tokens = 0; } else if (!yaz_matchstr (argv[0], "sort") && argc == 2) { @@ -131,7 +123,6 @@ static void zebra_map_read (ZebraMaps zms, const char *name) (*zm)->maptab = NULL; (*zm)->completeness = 0; (*zm)->positioned = 0; - (*zm)->replace_tokens = 0; } else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2) { @@ -150,58 +141,6 @@ static void zebra_map_read (ZebraMaps zms, const char *name) if ((*zm)->type == ZEBRA_MAP_TYPE_SORT) (*zm)->u.sort.entry_size = atoi (argv[1]); } - else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2) - { - struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token)); - token->next = (*zm)->replace_tokens; - (*zm)->replace_tokens = token; -#if 0 - logf (LOG_LOG, "replace %s", argv[1]); -#endif - token->token_from = 0; - if (argc >= 2) - { - char *cp = argv[1]; - int *dp = token->token_from = (int *) - nmem_malloc (zms->nmem, (1+strlen(cp))*sizeof(int)); - while (*cp) - if (*cp == '$') - { - *dp++ = ' '; - cp++; - } - else if (*cp == '.') - { - *dp++ = ZEBRA_REPLACE_ANY; - cp++; - } - else - { - *dp++ = zebra_prim(&cp); -#if 0 - logf (LOG_LOG, " char %2X %c", dp[-1], dp[-1]); -#endif - } - *dp = '\0'; - } - if (argc >= 3) - { - char *cp = argv[2]; - char *dp = token->token_to = - nmem_malloc (zms->nmem, strlen(cp)+1); - while (*cp) - if (*cp == '$') - { - *dp++ = ' '; - cp++; - } - else - *dp++ = zebra_prim(&cp); - *dp = '\0'; - } - else - token->token_to = 0; - } } if (zm) (*zm)->next = NULL; @@ -209,13 +148,15 @@ static void zebra_map_read (ZebraMaps zms, const char *name) for (zp = zms->map_list; zp; zp = zp->next) zms->lookup_array[zp->reg_id] = zp; + + return 0; } static void zms_map_handle (void *p, const char *name, const char *value) { ZebraMaps zms = (ZebraMaps) p; - zebra_map_read (zms, value); + (void) zebra_map_read (zms, value, 0); } ZebraMaps zebra_maps_open (Res res, const char *base) @@ -243,16 +184,17 @@ ZebraMaps zebra_maps_open (Res res, const char *base) for (i = 0; i<256; i++) zms->lookup_array[i] = 0; if (!res || !res_trav (res, "index", zms, zms_map_handle)) - zebra_map_read (zms, "default.idx"); + if (zebra_map_read (zms, "default.idx", 1) < 0) + return 0; zms->wrbuf_1 = wrbuf_alloc(); - zms->wrbuf_2 = wrbuf_alloc(); return zms; } struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id) { + assert(reg_id >= 0 && reg_id <= 255); return zms->lookup_array[reg_id]; } @@ -262,7 +204,7 @@ chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id) if (!zm) { zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm)); - logf (LOG_WARN, "Unknown register type: %c", reg_id); + yaz_log (YLOG_WARN, "Unknown register type: %c", reg_id); zm->reg_id = reg_id; zm->maptab_name = nmem_strdup (zms->nmem, "@"); @@ -270,7 +212,6 @@ chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id) zm->type = ZEBRA_MAP_TYPE_INDEX; zm->completeness = 0; zm->next = zms->map_list; - zm->replace_tokens = 0; zms->map_list = zm->next; zms->lookup_array[zm->reg_id & 255] = zm; @@ -280,12 +221,12 @@ chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id) if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@")) return NULL; if (!(zm->maptab = chrmaptab_create (zms->tabpath, - zm->maptab_name, 0, + zm->maptab_name, zms->tabroot))) - logf(LOG_WARN, "Failed to read character table %s", + yaz_log(YLOG_WARN, "Failed to read character table %s", zm->maptab_name); else - logf(LOG_DEBUG, "Read character table %s", zm->maptab_name); + yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name); } return zm->maptab; } @@ -305,6 +246,32 @@ const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id, return zms->temp_map_ptr; } +const char **zebra_maps_search(ZebraMaps zms, unsigned reg_id, + const char **from, int len, int *q_map_match) +{ + chrmaptab maptab; + + *q_map_match = 0; + maptab = zebra_charmap_get (zms, reg_id); + if (maptab) + { + const char **map; + map = chr_map_q_input(maptab, from, len, 0); + if (map && map[0]) + { + *q_map_match = 1; + return map; + } + map = chr_map_input(maptab, from, len, 0); + if (map) + return map; + } + zms->temp_map_str[0] = **from; + + (*from)++; + return zms->temp_map_ptr; +} + const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id, const char **from) { @@ -532,108 +499,11 @@ int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt, return 0; } -int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list, - const char *input_str, int input_len, WRBUF wrbuf); - WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list, const char *input_str, int input_len) { - struct zebra_map *zm = zebra_map_get (zms, reg_id); - wrbuf_rewind(zms->wrbuf_1); wrbuf_write(zms->wrbuf_1, input_str, input_len); - if (!zm || !zm->replace_tokens) - return zms->wrbuf_1; - -#if 0 - logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1), - wrbuf_buf(zms->wrbuf_1)); -#endif - for (;;) - { - if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1), - wrbuf_len(zms->wrbuf_1), zms->wrbuf_2)) - return zms->wrbuf_2; - if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2), - wrbuf_len(zms->wrbuf_2), zms->wrbuf_1)) - return zms->wrbuf_1; - } - return 0; + return zms->wrbuf_1; } -int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list, - const char *input_str, int input_len, WRBUF wrbuf) -{ - int i = -1; - int no_replaces = 0; - struct zebra_map *zm = zebra_map_get (zms, reg_id); - - wrbuf_rewind(wrbuf); - for (i = -1; i <= input_len; ) - { - struct zm_token *token; - char replace_string[128]; - int replace_out = 0; - int replace_in = 0; - - for (token = zm->replace_tokens; !replace_in && token; - token = token->next) - { - int j = 0; - int replace_done = 0; - replace_out = 0; - for (;; j++) - { - int c; - if (!token->token_from[j]) - { - replace_in = j; - break; - } - if (ex_list && strchr (ex_list, token->token_from[j])) - break; - if (i+j < 0 || j+i >= input_len) - c = ' '; - else - c = input_str[j+i] & 255; - if (token->token_from[j] == ZEBRA_REPLACE_ANY) - { - if (c == ' ') - break; - replace_string[replace_out++] = c; - } - else - { - if (c != token->token_from[j]) - { - break; - } - if (!replace_done) - { - const char *cp = token->token_to; - replace_done = 1; - for (; cp && *cp; cp++) - replace_string[replace_out++] = *cp; - } - } - } - } - if (!replace_in) - { - if (i >= 0 && i < input_len) - wrbuf_putc(wrbuf, input_str[i]); - i++; - } - else - { - no_replaces++; - if (replace_out) - wrbuf_write(wrbuf, replace_string, replace_out); - i += replace_in; - } - } -#if 0 - logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf)); -#endif - return no_replaces; -}