-/*
- $Id: marcomp.c,v 1.1 2006-07-03 14:27:09 adam Exp $
+/* This file is part of the Zebra server.
+ Copyright (C) Index Data
+
+Zebra is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- marcomp.c - compiler of MARC statements.
*/
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
mc_context *mc_mk_context(const char *s)
{
mc_context *p=0;
-
+
if (s && strlen(s))
{
p = (mc_context*) xmalloc(sizeof(*p));
-
+
if (!p)
return 0;
-
+
memset(p, 0, sizeof(*p));
p->errcode = EMCOK;
p->data = s;
p->len = strlen(s);
p->crrtok = NOP;
}
-
+
return p;
}
void mc_destroy_context(mc_context *c)
case '[': c->crrtok = LINTERVAL; break;
case ']': c->crrtok = RINTERVAL; break;
default:
- if (isspace(*(unsigned char *) (c->data+c->offset))
+ if (isspace(*(unsigned char *) (c->data+c->offset))
|| *(c->data+c->offset) == '\n')
{
c->crrtok = NOP;
int mc_getdata(mc_context *c, char *s, int sz)
{
int i;
-
+
for (i=0; i<sz; i++)
{
if (mc_gettoken(c)!=REGULAR)
s[i] = mc_getval(c);
}
s[i] = '\0';
-
+
return i;
}
void mc_getinterval(mc_context *c, int *start, int *end)
{
char buf[6+1];
int start_pos, end_pos;
-
+
start_pos = end_pos = -1;
-
+
if (mc_gettoken(c) == LINTERVAL)
{
int i;
-
+
for (i=0;i<6;i++)
{
mc_token tok = mc_gettoken(c);
-
+
if (tok == RINTERVAL || tok == NOP)
break;
-
+
buf[i] = mc_getval(c);
}
-
+
buf[i] = '\0';
i = sscanf(buf, "%d-%d", &start_pos, &end_pos);
-
+
if (i == 1)
end_pos = start_pos;
else if ( i == 0)
p->interval.start = p->interval.end = -1;
}
return p;
-}
+}
void mc_destroy_field(mc_field *p)
{
if (!p)
return;
- if (p->name) xfree(p->name);
- if (p->ind1) xfree(p->ind1);
+ if (p->name) xfree(p->name);
+ if (p->ind1) xfree(p->ind1);
if (p->ind2) xfree(p->ind2);
if (p->list) mc_destroy_subfields_recursive(p->list);
xfree(p);
mc_field *pf;
pf = mc_mk_field();
-
+
if (!pf)
{
c->errcode = EMCNOMEM;
if (mc_getdata(c, pf->name, SZ_FNAME) == SZ_FNAME)
{
mc_token nexttok = mc_gettoken(c);
-
+
mc_ungettoken(c);
-
+
if (nexttok == LINTERVAL)
{
mc_getinterval(c, &pf->interval.start, &pf->interval.end);
pf->interval.end);
#endif
}
-
+
if ((mc_getdata(c, pf->ind1, SZ_IND) == SZ_IND) &&
(mc_getdata(c, pf->ind2, SZ_IND) == SZ_IND))
{
mc_destroy_field(pf);
return 0;
}
-
+
return pf;
}
mc_subfield *mc_mk_subfield(mc_subfield *parent)
{
if (!p)
return;
-
+
if (p->which == MC_SFGROUP || p->which == MC_SFVARIANT)
{
if (p->u.child)
if (p->u.in_line)
mc_destroy_field(p->u.in_line);
}
- if (p->name) xfree(p->name);
- if (p->prefix) xfree(p->prefix);
+ if (p->name) xfree(p->name);
+ if (p->prefix) xfree(p->prefix);
if (p->suffix) xfree(p->suffix);
if (p->parent) p->parent->next = p->next;
xfree(p);
return;
mc_destroy_subfields_recursive(p->next);
-
+
if (p->which == MC_SFGROUP || p->which == MC_SFVARIANT)
{
if (p->u.child)
if (p->u.in_line)
mc_destroy_field(p->u.in_line);
}
-
+
if (p->name) xfree(p->name);
if (p->prefix) xfree(p->prefix);
if (p->suffix) xfree(p->suffix);
{
mc_subfield *psf=0;
mc_token tok = mc_gettoken(c);
-
+
if (tok == NOP)
return 0;
-
+
if (tok == LGROUP)
{
if (!(psf = mc_mk_subfield(parent)))
psf->which = MC_SFGROUP;
psf->u.child = mc_getsubfields(c, psf);
-
+
if (mc_gettoken(c) == RGROUP)
psf->next = mc_getsubfields(c, psf);
else
psf->which = MC_SFVARIANT;
psf->u.child = mc_getsubfields(c, psf);
-
+
if (mc_gettoken(c) == RVARIANT)
psf->next = mc_getsubfields(c, psf);
else
mc_token tok = mc_gettoken(c);
mc_ungettoken(c);
-
+
if (tok == LINTERVAL)
{
mc_getinterval(c, &psf->interval.start, &psf->interval.end);
return 0;
}
}
-
+
if (mc_getdata(c, psf->suffix, SZ_SUFFIX) == SZ_SUFFIX)
{
psf->which = MC_SF;
return 0;
}
}
- }
+ }
return psf;
}
/*
* Local variables:
* c-basic-offset: 4
+ * c-file-style: "Stroustrup"
* indent-tabs-mode: nil
* End:
* vim: shiftwidth=4 tabstop=8 expandtab