-/* $Id: charmap.c,v 1.28 2004-03-09 15:12:15 adam Exp $
+/* $Id: charmap.c,v 1.30 2004-09-14 14:38:08 quinn Exp $
Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
Index Data Aps
#define CHR_MAXSTR 1024
#define CHR_MAXEQUIV 32
+const unsigned char CHR_FIELD_BEGIN = '^';
+
const char *CHR_UNKNOWN = "\001";
const char *CHR_SPACE = "\002";
-const char *CHR_BASE = "\003";
+const char *CHR_CUT = "\003";
+const char *CHR_BASE = "\005";
struct chrmaptab_info
{
return t->target ? t : 0;
}
-static chr_t_entry *find_entry_x(chr_t_entry *t, const char **from, int *len)
+static chr_t_entry *find_entry_x(chr_t_entry *t, const char **from, int *len, int first)
{
chr_t_entry *res;
from++;
len++;
}
- if (*len > 0 && t->children && t->children[(unsigned char) **from])
+ if (*len > 0 && t->children)
{
const char *old_from = *from;
int old_len = *len;
+
+ res = 0;
+
+ if (first && t->children[CHR_FIELD_BEGIN])
+ {
+ if ((res = find_entry_x(t->children[CHR_FIELD_BEGIN], from, len, 0)) && res != t->children[CHR_FIELD_BEGIN])
+ return res;
+ else
+ res = 0;
+ /* otherwhise there was no match on beginning of field, move on */
+ }
- (*len)--;
- (*from)++;
- if ((res = find_entry_x(t->children[(unsigned char) *old_from],
- from, len)))
- return res;
- /* no match */
- *len = old_len;
- *from = old_from;
+ if (!res && t->children[(unsigned char) **from])
+ {
+ (*len)--;
+ (*from)++;
+ if ((res = find_entry_x(t->children[(unsigned char) *old_from],
+ from, len, 0)))
+ return res;
+ /* no match */
+ *len = old_len;
+ *from = old_from;
+ }
}
/* no children match. use ourselves, if we have a target */
return t->target ? t : 0;
}
-const char **chr_map_input_x(chrmaptab maptab, const char **from, int *len)
+const char **chr_map_input_x(chrmaptab maptab, const char **from, int *len, int first)
{
chr_t_entry *t = maptab->input;
chr_t_entry *res;
- if (!(res = find_entry_x(t, from, len)))
+ if (!(res = find_entry_x(t, from, len, first)))
abort();
return (const char **) (res->target);
}
-const char **chr_map_input(chrmaptab maptab, const char **from, int len)
+const char **chr_map_input(chrmaptab maptab, const char **from, int len, int first)
{
chr_t_entry *t = maptab->input;
chr_t_entry *res;
len_tmp[0] = len;
len_tmp[1] = -1;
- if (!(res = find_entry_x(t, from, len_tmp)))
+ if (!(res = find_entry_x(t, from, len_tmp, first)))
abort();
return (const char **) (res->target);
}
ucs4_t i = 0;
char fmtstr[8];
- yaz_log (LOG_DEBUG, "prim %.3s", (char *) *s);
+ yaz_log (LOG_DEBUG, "prim_w %.3s", (char *) *s);
if (**s == '\\')
{
(*s)++;
(char*) CHR_SPACE, 0);
}
+/*
+ * Callback function.
+ * Add a space-entry to the value space.
+ */
+static void fun_addcut(const char *s, void *data, int num)
+{
+ chrmaptab tab = (chrmaptab) data;
+ tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
+ (char*) CHR_CUT, 0);
+}
+
/*
* Create a string containing the mapped characters provided.
*/
chrwork *arg = (chrwork *) data;
const char **res, *p = s;
- res = chr_map_input(arg->map, &s, strlen(s));
+ res = chr_map_input(arg->map, &s, strlen(s), 0);
if (*res == (char*) CHR_UNKNOWN)
logf(LOG_WARN, "Map: '%s' has no mapping", p);
strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
char str[1024];
ucs4_t arg[512];
+ ucs4_t arg_prim[512];
ucs4_t *s0, *s = arg;
ucs4_t c, begin, end;
size_t i;
case '[': s++; abort(); break;
case '(':
++s;
- s0 = s;
- while (*s != ')' || s[-1] == '\\')
- s++;
- *s = 0;
- if (scan_to_utf8 (t_utf8, s0, s - s0, str, sizeof(str)-1))
+ s0 = s; i = 0;
+ while (*s != ')' || s[-1] == '\\')
+ arg_prim[i++] = zebra_prim_w(&s);
+ arg_prim[i] = 0;
+ if (scan_to_utf8 (t_utf8, arg_prim, zebra_ucs4_strlen(arg_prim), str, sizeof(str)-1))
return -1;
(*fun)(str, data, num ? (*num)++ : 0);
s++;
{
if (argc != 2)
{
- logf(LOG_FATAL, "Syntax error in charmap");
+ logf(LOG_FATAL, "Syntax error in charmap for space");
++errors;
}
if (scan_string(argv[1], t_unicode, t_utf8,
++errors;
}
}
+ else if (!map_only && !yaz_matchstr(argv[0], "cut"))
+ {
+ if (argc != 2)
+ {
+ logf(LOG_FATAL, "Syntax error in charmap for cut");
+ ++errors;
+ }
+ if (scan_string(argv[1], t_unicode, t_utf8,
+ fun_addcut, res, 0) < 0)
+ {
+ logf(LOG_FATAL, "Bad cut specification");
+ ++errors;
+ }
+ }
else if (!yaz_matchstr(argv[0], "map"))
{
chrwork buf;