X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=util%2Fzebramap.c;h=238c0343acb075d73619e3a274b9121c5f869f18;hb=eca0079ec91514049034728df2b91a26566a5d89;hp=df53aa8561f76f39ad3f7381df3f91571a9f19c4;hpb=4eb3b54bb2ca9af74f39f000d3d40dba99ded887;p=idzebra-moved-to-github.git diff --git a/util/zebramap.c b/util/zebramap.c index df53aa8..238c034 100644 --- a/util/zebramap.c +++ b/util/zebramap.c @@ -1,6 +1,6 @@ -/* $Id: zebramap.c,v 1.37 2004-12-13 20:51:34 adam Exp $ - Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004 - Index Data Aps +/* $Id: zebramap.c,v 1.43 2005-08-30 12:23:02 adam Exp $ + Copyright (C) 1995-2005 + Index Data ApS This file is part of the Zebra server. @@ -20,9 +20,8 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - - #include +#include #include #include @@ -35,13 +34,6 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #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; @@ -58,7 +50,6 @@ struct zebra_map { chrmaptab maptab; const char *maptab_name; struct zebra_map *next; - struct zm_token *replace_tokens; }; struct zebra_maps { @@ -69,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) @@ -82,7 +73,6 @@ 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); } @@ -116,7 +106,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) { @@ -132,7 +121,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) { @@ -151,58 +139,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 - yaz_log (YLOG_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 - yaz_log (YLOG_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; @@ -247,13 +183,13 @@ ZebraMaps zebra_maps_open (Res res, const char *base) zebra_map_read (zms, "default.idx"); 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]; } @@ -271,7 +207,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; @@ -281,7 +216,7 @@ 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))) yaz_log(YLOG_WARN, "Failed to read character table %s", zm->maptab_name); @@ -306,6 +241,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) { @@ -533,108 +494,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 - yaz_log (YLOG_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 - yaz_log (YLOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf)); -#endif - return no_replaces; -}