1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2011 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <yaz/yaz-util.h>
32 static mc_token mc_gettoken(mc_context *c);
33 static void mc_ungettoken(mc_context *c);
34 static int mc_getval(mc_context *c);
35 static int mc_getdata(mc_context *c, char *s, int sz);
36 static void mc_getinterval(mc_context *c, int *start, int *end);
38 static mc_subfield *mc_mk_subfield(mc_subfield *parent);
39 static mc_field *mc_mk_field(void);
41 static struct mc_errmsg
48 {EMCF, "not complete field"},
49 {EMCSF, "not complete subfield"},
50 {EMCSFGROUP, "not closed GROUP"},
51 {EMCSFVAR, "not closed VARIANT"},
52 {EMCSFINLINE, "not closed IN-LINE"},
53 {EMCEND, "not correct errno"}
55 mc_errcode mc_errno(mc_context *c)
59 const char *mc_error(mc_errcode no)
61 if (no >= EMCOK && no<EMCEND)
62 return mc_errmsg[no].msg;
64 return mc_errmsg[EMCEND].msg;
66 mc_context *mc_mk_context(const char *s)
72 p = (mc_context*) xmalloc(sizeof(*p));
77 memset(p, 0, sizeof(*p));
86 void mc_destroy_context(mc_context *c)
90 mc_token mc_gettoken(mc_context *c)
92 if (c->offset >= c->len)
95 switch (*(c->data+c->offset))
97 case '{': c->crrtok = LVARIANT; break;
98 case '}': c->crrtok = RVARIANT; break;
99 case '(': c->crrtok = LGROUP; break;
100 case ')': c->crrtok = RGROUP; break;
101 case '<': c->crrtok = LINLINE; break;
102 case '>': c->crrtok = RINLINE; break;
103 case '$': c->crrtok = SUBFIELD; break;
104 case '[': c->crrtok = LINTERVAL; break;
105 case ']': c->crrtok = RINTERVAL; break;
107 if (isspace(*(unsigned char *) (c->data+c->offset))
108 || *(c->data+c->offset) == '\n')
115 c->crrval = *(c->data+c->offset);
119 fprintf(stderr, "gettoken(): offset: %d", c->offset);
120 if (c->crrtok == REGULAR)
121 fprintf(stderr, "<%c>", c->crrval);
122 fprintf(stderr, "\n");
127 void mc_ungettoken(mc_context *c)
132 int mc_getval(mc_context *c)
136 int mc_getdata(mc_context *c, char *s, int sz)
142 if (mc_gettoken(c)!=REGULAR)
153 void mc_getinterval(mc_context *c, int *start, int *end)
156 int start_pos, end_pos;
158 start_pos = end_pos = -1;
160 if (mc_gettoken(c) == LINTERVAL)
166 mc_token tok = mc_gettoken(c);
168 if (tok == RINTERVAL || tok == NOP)
171 buf[i] = mc_getval(c);
175 i = sscanf(buf, "%d-%d", &start_pos, &end_pos);
187 mc_field *mc_mk_field(void)
189 mc_field *p = (mc_field *)xmalloc(sizeof(*p));
193 memset(p, 0, sizeof(*p));
194 p->name = (char *)xmalloc(SZ_FNAME+1);
196 p->ind1 = (char *)xmalloc(SZ_IND+1);
198 p->ind2 = (char *)xmalloc(SZ_IND+1);
200 p->interval.start = p->interval.end = -1;
204 void mc_destroy_field(mc_field *p)
208 if (p->name) xfree(p->name);
209 if (p->ind1) xfree(p->ind1);
210 if (p->ind2) xfree(p->ind2);
211 if (p->list) mc_destroy_subfields_recursive(p->list);
214 mc_field *mc_getfield(mc_context *c)
222 c->errcode = EMCNOMEM;
226 if (mc_getdata(c, pf->name, SZ_FNAME) == SZ_FNAME)
228 mc_token nexttok = mc_gettoken(c);
232 if (nexttok == LINTERVAL)
234 mc_getinterval(c, &pf->interval.start, &pf->interval.end);
236 fprintf(stderr, "ineterval (%d)-(%d)\n", pf->interval.start,
241 if ((mc_getdata(c, pf->ind1, SZ_IND) == SZ_IND) &&
242 (mc_getdata(c, pf->ind2, SZ_IND) == SZ_IND))
244 pf->list = mc_getsubfields(c, 0);
250 mc_destroy_field(pf);
256 mc_subfield *mc_mk_subfield(mc_subfield *parent)
258 mc_subfield *p = (mc_subfield*)xmalloc(sizeof(*p));
262 memset(p, 0, sizeof(*p));
264 p->name = (char *)xmalloc(SZ_SFNAME+1);
266 p->prefix = (char *)xmalloc(SZ_PREFIX+1);
268 p->suffix = (char *)xmalloc(SZ_SUFFIX+1);
271 p->interval.start = p->interval.end = -1;
275 void mc_destroy_subfield(mc_subfield *p)
280 if (p->which == MC_SFGROUP || p->which == MC_SFVARIANT)
283 mc_destroy_subfields_recursive(p->u.child);
285 else if (p->which == MC_SF)
288 mc_destroy_field(p->u.in_line);
290 if (p->name) xfree(p->name);
291 if (p->prefix) xfree(p->prefix);
292 if (p->suffix) xfree(p->suffix);
293 if (p->parent) p->parent->next = p->next;
296 void mc_destroy_subfields_recursive(mc_subfield *p)
301 mc_destroy_subfields_recursive(p->next);
303 if (p->which == MC_SFGROUP || p->which == MC_SFVARIANT)
306 mc_destroy_subfields_recursive(p->u.child);
308 else if (p->which == MC_SF)
311 mc_destroy_field(p->u.in_line);
314 if (p->name) xfree(p->name);
315 if (p->prefix) xfree(p->prefix);
316 if (p->suffix) xfree(p->suffix);
317 if (p->parent) p->parent->next = 0;
320 mc_subfield *mc_getsubfields(mc_context *c, mc_subfield *parent)
323 mc_token tok = mc_gettoken(c);
330 if (!(psf = mc_mk_subfield(parent)))
332 c->errcode = EMCNOMEM;
336 psf->which = MC_SFGROUP;
337 psf->u.child = mc_getsubfields(c, psf);
339 if (mc_gettoken(c) == RGROUP)
340 psf->next = mc_getsubfields(c, psf);
343 c->errcode = EMCSFGROUP;
344 mc_destroy_subfield(psf);
348 else if (tok == LVARIANT)
350 if (!(psf = mc_mk_subfield(parent)))
352 c->errcode = EMCNOMEM;
356 psf->which = MC_SFVARIANT;
357 psf->u.child = mc_getsubfields(c, psf);
359 if (mc_gettoken(c) == RVARIANT)
360 psf->next = mc_getsubfields(c, psf);
363 c->errcode = EMCSFVAR;
364 mc_destroy_subfield(psf);
368 else if (tok == RGROUP || tok == RVARIANT || tok == RINLINE)
373 else if (tok == REGULAR)
375 if (!(psf = mc_mk_subfield(parent)))
377 c->errcode = EMCNOMEM;
383 if((mc_getdata(c, psf->prefix, SZ_PREFIX) == SZ_PREFIX) &&
384 (mc_gettoken(c) == SUBFIELD) &&
385 (mc_getdata(c, psf->name, SZ_SFNAME) == SZ_SFNAME))
387 mc_token tok = mc_gettoken(c);
391 if (tok == LINTERVAL)
393 mc_getinterval(c, &psf->interval.start, &psf->interval.end);
395 else if (tok == LINLINE)
398 psf->u.in_line = mc_getfield(c);
399 if (mc_gettoken(c) != RINLINE)
401 c->errcode = EMCSFINLINE;
402 mc_destroy_subfield(psf);
407 if (mc_getdata(c, psf->suffix, SZ_SUFFIX) == SZ_SUFFIX)
410 psf->next = mc_getsubfields(c, psf);
415 mc_destroy_subfield(psf);
425 * c-file-style: "Stroustrup"
426 * indent-tabs-mode: nil
428 * vim: shiftwidth=4 tabstop=8 expandtab