-/* $Id: zebramap.c,v 1.60 2007-10-30 19:17:15 adam Exp $
+/* $Id: zebramap.c,v 1.63 2007-11-05 11:36:23 adam Exp $
Copyright (C) 1995-2007
Index Data ApS
#define ZEBRA_REPLACE_ANY 300
struct zebra_map {
- unsigned reg_id;
+ const char *id;
int completeness;
int positioned;
int alwaysmatches;
int type;
union {
struct {
- int dummy;
- } index;
- struct {
int entry_size;
} sort;
} u;
NMEM nmem;
char temp_map_str[2];
const char *temp_map_ptr[2];
- struct zebra_map **lookup_array;
WRBUF wrbuf_1;
- int no_maps;
+ int no_files_read;
zebra_map_t map_list;
- zebra_map_t *last_map;
+ zebra_map_t last_map;
};
void zebra_maps_close(zebra_maps_t zms)
zebra_map_t zm = (zebra_map_t) nmem_malloc(zms->nmem, sizeof(*zm));
zm->zebra_maps = zms;
- zm->reg_id = index_type[0];
+ zm->id = nmem_strdup(zms->nmem, index_type);
zm->maptab_name = 0;
zm->maptab = 0;
zm->type = map_type;
zm->alwaysmatches = 0;
zm->first_in_field = 0;
+ if (zms->last_map)
+ zms->last_map->next = zm;
+ else
+ zms->map_list = zm;
+ zms->last_map = zm;
zm->next = 0;
- *zms->last_map = zm;
- zms->last_map = &zm->next;
-
- zms->no_maps++;
return zm;
}
+static int parse_command(zebra_maps_t zms, int argc, char **argv,
+ const char *fname, int lineno)
+{
+ zebra_map_t zm = zms->last_map;
+ if (argc == 1)
+ {
+ yaz_log(YLOG_WARN, "%s:%d: Missing arguments for '%s'",
+ fname, lineno, argv[0]);
+ return -1;
+ }
+ if (argc > 2)
+ {
+ yaz_log(YLOG_WARN, "%s:%d: Too many arguments for '%s'",
+ fname, lineno, argv[0]);
+ return -1;
+ }
+ if (!yaz_matchstr(argv[0], "index"))
+ {
+ zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_INDEX);
+ zm->positioned = 1;
+ }
+ else if (!yaz_matchstr(argv[0], "sort"))
+ {
+ zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_SORT);
+ zm->u.sort.entry_size = 80;
+ }
+ else if (!yaz_matchstr(argv[0], "staticrank"))
+ {
+ zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_STATICRANK);
+ zm->completeness = 1;
+ }
+ else if (!zm)
+ {
+ yaz_log(YLOG_WARN, "%s:%d: Missing sort/index before '%s'",
+ fname, lineno, argv[0]);
+ return -1;
+ }
+ else if (!yaz_matchstr(argv[0], "charmap") && argc == 2)
+ {
+ if (zm->type != ZEBRA_MAP_TYPE_STATICRANK)
+ zm->maptab_name = nmem_strdup(zms->nmem, argv[1]);
+ else
+ {
+ yaz_log(YLOG_WARN|YLOG_FATAL, "%s:%d: charmap for "
+ "staticrank is invalid", fname, lineno);
+ yaz_log(YLOG_LOG, "Type is %d", zm->type);
+ return -1;
+ }
+ }
+ else if (!yaz_matchstr(argv[0], "completeness") && argc == 2)
+ {
+ zm->completeness = atoi(argv[1]);
+ }
+ else if (!yaz_matchstr(argv[0], "position") && argc == 2)
+ {
+ zm->positioned = atoi(argv[1]);
+ }
+ else if (!yaz_matchstr(argv[0], "alwaysmatches") && argc == 2)
+ {
+ if (zm->type != ZEBRA_MAP_TYPE_STATICRANK)
+ zm->alwaysmatches = atoi(argv[1]);
+ else
+ {
+ yaz_log(YLOG_WARN|YLOG_FATAL, "%s:%d: alwaysmatches for "
+ "staticrank is invalid", fname, lineno);
+ return -1;
+ }
+ }
+ else if (!yaz_matchstr(argv[0], "firstinfield") && argc == 2)
+ {
+ zm->first_in_field = atoi(argv[1]);
+ }
+ else if (!yaz_matchstr(argv[0], "entrysize") && argc == 2)
+ {
+ if (zm->type == ZEBRA_MAP_TYPE_SORT)
+ zm->u.sort.entry_size = atoi(argv[1]);
+ else
+ {
+ yaz_log(YLOG_WARN,
+ "%s:%d: entrysize only valid in sort section",
+ fname, lineno);
+ return -1;
+ }
+ }
+ else
+ {
+ yaz_log(YLOG_WARN, "%s:%d: Unrecognized directive '%s'",
+ fname, lineno, argv[0]);
+ return -1;
+ }
+ return 0;
+}
+
ZEBRA_RES zebra_maps_read_file(zebra_maps_t zms, const char *fname)
{
FILE *f;
int argc;
int lineno = 0;
int failures = 0;
- zebra_map_t zm = 0;
if (!(f = yaz_fopen(zms->tabpath, fname, "r", zms->tabroot)))
{
}
while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
{
- if (argc == 1)
- {
- yaz_log(YLOG_WARN, "%s:%d: Missing arguments for '%s'",
- fname, lineno, argv[0]);
- failures++;
- break;
- }
- if (argc > 2)
- {
- yaz_log(YLOG_WARN, "%s:%d: Too many arguments for '%s'",
- fname, lineno, argv[0]);
- failures++;
- break;
- }
- if (!yaz_matchstr(argv[0], "index"))
- {
- zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_INDEX);
- zm->positioned = 1;
- }
- else if (!yaz_matchstr(argv[0], "sort"))
- {
- zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_SORT);
- zm->u.sort.entry_size = 80;
- }
- else if (!yaz_matchstr(argv[0], "staticrank"))
- {
- zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_STATICRANK);
- zm->completeness = 1;
- }
- else if (!zm)
- {
- yaz_log(YLOG_WARN, "%s:%d: Missing sort/index before '%s'",
- fname, lineno, argv[0]);
- failures++;
- }
- else if (!yaz_matchstr(argv[0], "charmap") && argc == 2)
- {
- if (zm->type != ZEBRA_MAP_TYPE_STATICRANK)
- zm->maptab_name = nmem_strdup(zms->nmem, argv[1]);
- else
- {
- yaz_log(YLOG_WARN|YLOG_FATAL, "%s:%d: charmap for "
- "staticrank is invalid", fname, lineno);
- yaz_log(YLOG_LOG, "Type is %d", zm->type);
- failures++;
- }
- }
- else if (!yaz_matchstr(argv[0], "completeness") && argc == 2)
- {
- zm->completeness = atoi(argv[1]);
- }
- else if (!yaz_matchstr(argv[0], "position") && argc == 2)
- {
- zm->positioned = atoi(argv[1]);
- }
- else if (!yaz_matchstr(argv[0], "alwaysmatches") && argc == 2)
- {
- if (zm->type != ZEBRA_MAP_TYPE_STATICRANK)
- zm->alwaysmatches = atoi(argv[1]);
- else
- {
- yaz_log(YLOG_WARN|YLOG_FATAL, "%s:%d: alwaysmatches for "
- "staticrank is invalid", fname, lineno);
- failures++;
- }
- }
- else if (!yaz_matchstr(argv[0], "firstinfield") && argc == 2)
- {
- zm->first_in_field = atoi(argv[1]);
- }
- else if (!yaz_matchstr(argv[0], "entrysize") && argc == 2)
- {
- if (zm->type == ZEBRA_MAP_TYPE_SORT)
- zm->u.sort.entry_size = atoi(argv[1]);
- }
- else
- {
- yaz_log(YLOG_WARN, "%s:%d: Unrecognized directive '%s'",
- fname, lineno, argv[0]);
+ int r = parse_command(zms, argc, argv, fname, lineno);
+ if (r)
failures++;
- }
}
yaz_fclose(f);
- for (zm = zms->map_list; zm; zm = zm->next)
- zms->lookup_array[zm->reg_id] = zm;
-
if (failures)
return ZEBRA_FAIL;
+
+ (zms->no_files_read)++;
return ZEBRA_OK;
}
zebra_maps_t zebra_maps_open(Res res, const char *base_path,
- const char *profile_path)
+ const char *profile_path)
{
zebra_maps_t zms = (zebra_maps_t) xmalloc(sizeof(*zms));
- int i;
zms->nmem = nmem_create();
- zms->no_maps = 0;
zms->tabpath = profile_path ? nmem_strdup(zms->nmem, profile_path) : 0;
zms->tabroot = 0;
if (base_path)
zms->tabroot = nmem_strdup(zms->nmem, base_path);
zms->map_list = 0;
- zms->last_map = &zms->map_list;
+ zms->last_map = 0;
zms->temp_map_str[0] = '\0';
zms->temp_map_str[1] = '\0';
zms->temp_map_ptr[0] = zms->temp_map_str;
zms->temp_map_ptr[1] = NULL;
- zms->lookup_array = (zebra_map_t *)
- nmem_malloc(zms->nmem, sizeof(*zms->lookup_array)*256);
zms->wrbuf_1 = wrbuf_alloc();
- for (i = 0; i<256; i++)
- zms->lookup_array[i] = 0;
+ zms->no_files_read = 0;
return zms;
}
-zebra_map_t zebra_map_get(zebra_maps_t zms, unsigned reg_id)
+zebra_map_t zebra_map_get(zebra_maps_t zms, const char *id)
{
- assert(reg_id >= 0 && reg_id <= 255);
- return zms->lookup_array[reg_id];
+ zebra_map_t zm;
+ for (zm = zms->map_list; zm; zm = zm->next)
+ if (!strcmp(zm->id, id))
+ break;
+ return zm;
}
-zebra_map_t zebra_map_get_or_add(zebra_maps_t zms, unsigned reg_id)
+zebra_map_t zebra_map_get_or_add(zebra_maps_t zms, const char *id)
{
- struct zebra_map *zm = zebra_map_get(zms, reg_id);
+ struct zebra_map *zm = zebra_map_get(zms, id);
if (!zm)
{
- char name[2];
- name[0] = reg_id;
- name[1] = '\0';
-
- zm = zebra_add_map(zms, name, ZEBRA_MAP_TYPE_INDEX);
+ zm = zebra_add_map(zms, id, ZEBRA_MAP_TYPE_INDEX);
- /* no reason to warn if no maps are installed at ALL
- Note that zebra_add_maps increments no_maps ..
- */
- if (zms->no_maps > 1)
- yaz_log(YLOG_WARN, "Unknown register type: %c", reg_id);
- else
- zms->no_maps = 0;
+ /* no reason to warn if no maps are read from file */
+ if (zms->no_files_read)
+ yaz_log(YLOG_WARN, "Unknown register type: %s", id);
zm->maptab_name = nmem_strdup(zms->nmem, "@");
zm->completeness = 0;
zm->positioned = 1;
- zm->next = zms->map_list;
- zms->map_list = zm->next;
-
- zms->lookup_array[zm->reg_id & 255] = zm;
}
return zm;
}