X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=util%2Fzebramap.c;h=9538f8eb7e7fea6bafb73e8cb7e61957f73bc3a0;hb=45dcbe04562559c1af90b5049fa1871311907072;hp=3d75a4160ea8b2a51c9e2a0fb26ccc8459f7ed9f;hpb=ef696645cc3b7e0f4027008d1dc589c0f0f90c1f;p=idzebra-moved-to-github.git diff --git a/util/zebramap.c b/util/zebramap.c index 3d75a41..9538f8e 100644 --- a/util/zebramap.c +++ b/util/zebramap.c @@ -4,7 +4,19 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: zebramap.c,v $ - * Revision 1.15 1999-05-26 07:49:14 adam + * Revision 1.19 1999-11-30 13:48:04 adam + * Improved installation. Updated for inclusion of YAZ header files. + * + * Revision 1.18 1999/10/15 08:27:46 adam + * Fixed replace handler. 8-bit fix. + * + * Revision 1.17 1999/09/08 12:13:21 adam + * Fixed minor bug "replace"-mappings. Removed some logging messages. + * + * Revision 1.16 1999/09/07 07:19:21 adam + * Work on character mapping. Implemented replace rules. + * + * Revision 1.15 1999/05/26 07:49:14 adam * C++ compilation. * * Revision 1.14 1999/02/19 10:37:40 adam @@ -60,13 +72,22 @@ #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; @@ -83,6 +104,7 @@ struct zebra_map { chrmaptab maptab; const char *maptab_name; struct zebra_map *next; + struct zm_token *replace_tokens; }; struct zebra_maps { @@ -92,6 +114,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; }; void zebra_maps_close (ZebraMaps zms) @@ -103,6 +126,8 @@ void zebra_maps_close (ZebraMaps zms) chrmaptab_destroy (zm->maptab); zm = zm->next; } + wrbuf_free (zms->wrbuf_1, 1); + wrbuf_free (zms->wrbuf_2, 1); nmem_destroy (zms->nmem); xfree (zms); } @@ -136,6 +161,7 @@ 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) { @@ -151,6 +177,7 @@ 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) { @@ -169,6 +196,58 @@ 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; @@ -191,7 +270,8 @@ ZebraMaps zebra_maps_open (Res res) int i; zms->nmem = nmem_create (); - zms->tabpath = nmem_strdup (zms->nmem, res_get_def (res, "profilePath", ".")); + zms->tabpath = nmem_strdup (zms->nmem, + res_get_def (res, "profilePath", ".")); zms->map_list = NULL; zms->temp_map_str[0] = '\0'; @@ -206,6 +286,9 @@ ZebraMaps zebra_maps_open (Res res) zms->lookup_array[i] = 0; if (!res || !res_trav (res, "index", zms, zms_map_handle)) zebra_map_read (zms, "default.idx"); + + zms->wrbuf_1 = wrbuf_alloc(); + zms->wrbuf_2 = wrbuf_alloc(); return zms; } @@ -261,19 +344,47 @@ const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id, return zms->temp_map_ptr; } +#if 0 +int zebra_maps_input_tokens (ZebraMaps zms, unsigned reg_id, + const char *input_str, int input_len, + WRBUF wrbuf) +{ + chrmaptab maptab = zebra_charmap_get (zms, reg_id); + int len[4]; + char *str[3]; + int input_i = 0; + int first = 1; + const char **out; + + if (!maptab) + { + wrbuf_write (wrbuf, input_str, input_len); + return -1; + } + str[0] = " "; + len[0] = 1; + str[1] = input_str; + len[1] = input_len; + str[2] = " "; + len[2] = 1; + len[3] = -1; + + out = chr_map_input (maptab, str, len); + while (len[1] > 0) + { + while (out && *out && **out == *CHR_SPACE) + out = chr_map_input (maptab, str, len); + } +} +#endif + const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id, const char **from) { - chrmaptab maptab; - unsigned char i = (unsigned char) **from; - static char buf[2] = {0,0}; - - maptab = zebra_charmap_get (zms, reg_id); - if (maptab) - return chr_map_output (maptab, from, 1); - (*from)++; - buf[0] = i; - return buf; + chrmaptab maptab = zebra_charmap_get (zms, reg_id); + if (!maptab) + return 0; + return chr_map_output (maptab, from, 1); } @@ -466,3 +577,109 @@ 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->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; +} + +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; + 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; +}