Changed log message.
[idzebra-moved-to-github.git] / util / zebramap.c
1 /*
2  * Copyright (C) 1994-1997, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: zebramap.c,v $
7  * Revision 1.2  1997-10-31 12:39:30  adam
8  * Changed log message.
9  *
10  * Revision 1.1  1997/10/27 14:33:06  adam
11  * Moved towards generic character mapping depending on "structure"
12  * field in abstract syntax file. Fixed a few memory leaks. Fixed
13  * bug with negative integers when doing searches with relational
14  * operators.
15  *
16  */
17
18 #include <assert.h>
19 #include <ctype.h>
20
21 #include <yaz-util.h>
22 #include <charmap.h>
23 #include <zebramap.h>
24
25 struct zebra_map {
26     int reg_type;
27     chrmaptab maptab;
28     struct zebra_map *next;
29 };
30
31 struct zebra_maps {
32     char *tabpath;
33     NMEM nmem;
34     struct zebra_map *map_list;
35 };
36
37 void zebra_maps_close (ZebraMaps zms)
38 {
39     struct zebra_map *zm = zms->map_list;
40     while (zm)
41     {
42         struct zebra_map *zm_next = zm->next;
43
44         chrmaptab_destroy (zm->maptab);
45         xfree (zm);
46         zm = zm_next;
47     }
48     nmem_destroy (zms->nmem);
49     xfree (zms);
50 }
51
52 ZebraMaps zebra_maps_open (const char *tabpath)
53 {
54     ZebraMaps zms = xmalloc (sizeof(*zms));
55
56     zms->nmem = nmem_create ();
57     zms->tabpath = nmem_strdup (zms->nmem, tabpath);
58     zms->map_list = NULL;
59     return zms;
60 }
61
62 chrmaptab zebra_map_get (ZebraMaps zms, int reg_type)
63 {
64     char name[512];
65     struct zebra_map *zm;
66
67     for (zm = zms->map_list; zm; zm = zm->next)
68     {
69         if (reg_type == zm->reg_type)
70             return zm->maptab;
71     }
72     *name = '\0';
73     switch (reg_type)
74     {
75     case 'w':
76     case 'p':
77         strcat (name, "string");
78         break;
79     case 'n':
80         strcat (name, "numeric");
81         break;
82     case 'u':
83         strcat (name, "urx");
84         break;
85     default:
86         strcat (name, "null");
87     }
88     strcat (name, ".chr");
89
90     zm = xmalloc (sizeof(*zm));
91     zm->reg_type = reg_type;
92     zm->next = zms->map_list;
93     zms->map_list = zm;
94     if (!(zm->maptab = chrmaptab_create (zms->tabpath, name, 0)))
95         logf(LOG_WARN, "Failed to read character table %s", name);
96     else
97         logf(LOG_DEBUG, "Read character table %s", name);
98     return zm->maptab;
99 }
100
101 const char **zebra_maps_input (ZebraMaps zms, int reg_type,
102                                const char **from, int len)
103 {
104     static char str[2] = {0,0};
105     static const char *buf[2] = {0,0};
106     chrmaptab maptab;
107
108     maptab = zebra_map_get (zms, reg_type);
109     if (maptab)
110         return chr_map_input(maptab, from, len);
111         
112     if (isalnum(**from))
113     {
114         str[0] = isupper(**from) ? tolower(**from) : **from;
115         buf[0] = str;
116     }
117     else
118         buf[0] = (char*) CHR_SPACE;
119     (*from)++;
120     return buf;
121 }
122
123 const char *zebra_maps_output(ZebraMaps zms, int reg_type, const char **from)
124 {
125     chrmaptab maptab;
126     unsigned char i = (unsigned char) **from;
127     static char buf[2] = {0,0};
128
129     maptab = zebra_map_get (zms, reg_type);
130     if (maptab)
131         return chr_map_output (maptab, from, 1);
132     (*from)++;
133     buf[0] = i;
134     return buf;
135 }
136
137
138 /* ------------------------------------ */
139
140 typedef struct {
141     int type;
142     int major;
143     int minor;
144     Z_AttributesPlusTerm *zapt;
145 } AttrType;
146
147 static int attr_find (AttrType *src, oid_value *attributeSetP)
148 {
149     while (src->major < src->zapt->num_attributes)
150     {
151         Z_AttributeElement *element;
152
153         element = src->zapt->attributeList[src->major];
154         if (src->type == *element->attributeType)
155         {
156             switch (element->which) 
157             {
158             case Z_AttributeValue_numeric:
159                 ++(src->major);
160                 if (element->attributeSet && attributeSetP)
161                 {
162                     oident *attrset;
163
164                     attrset = oid_getentbyoid (element->attributeSet);
165                     *attributeSetP = attrset->value;
166                 }
167                 return *element->value.numeric;
168                 break;
169             case Z_AttributeValue_complex:
170                 if (src->minor >= element->value.complex->num_list ||
171                     element->value.complex->list[src->minor]->which !=  
172                     Z_StringOrNumeric_numeric)
173                     break;
174                 ++(src->minor);
175                 if (element->attributeSet && attributeSetP)
176                 {
177                     oident *attrset;
178
179                     attrset = oid_getentbyoid (element->attributeSet);
180                     *attributeSetP = attrset->value;
181                 }
182                 return *element->value.complex->list[src->minor-1]->u.numeric;
183             default:
184                 assert (0);
185             }
186         }
187         ++(src->major);
188     }
189     return -1;
190 }
191
192 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
193                        int type)
194 {
195     src->zapt = zapt;
196     src->type = type;
197     src->major = 0;
198     src->minor = 0;
199 }
200
201 /* ------------------------------------ */
202
203 int zebra_maps_is_complete (ZebraMaps zms, int reg_type)
204 {
205     if (reg_type == 'p')
206         return 1;
207     return 0;
208 }
209
210 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
211                      int *reg_type, char **search_type, int *complete_flag)
212 {
213     AttrType completeness;
214     AttrType structure;
215     AttrType relation;
216     int completeness_value;
217     int structure_value;
218     int relation_value;
219
220     attr_init (&structure, zapt, 4);
221     attr_init (&completeness, zapt, 6);
222     attr_init (&relation, zapt, 2);
223
224     completeness_value = attr_find (&completeness, NULL);
225     structure_value = attr_find (&structure, NULL);
226
227     if (completeness_value == 2 || completeness_value == 3)
228         *complete_flag = 1;
229     else
230         *complete_flag = 0;
231     *reg_type = 0;
232
233     *search_type = "phrase";
234     if (relation_value == 102)
235         *search_type = "ranked";
236     
237     switch (structure_value)
238     {
239     case -1:
240     case 1:   /* phrase */
241     case 2:   /* word */
242     case 3:   /* key */
243     case 6:   /* word list */
244     case 105: /* free-form-text */
245     case 106: /* document-text */
246     case 108: /* string */ 
247         if (*complete_flag)
248             *reg_type = 'p';
249         else
250             *reg_type = 'w';
251         break;
252     case 107: /* local-number */
253         *search_type = "local";
254         *reg_type = 0;
255     case 109: /* numeric string */
256         *reg_type = 'n';
257         break;
258     case 104: /* urx */
259         *reg_type = 'u';
260         break;
261     default:
262         return -1;
263     }
264     return 0;
265 }