Start work on ICU based regexp searches
[idzebra-moved-to-github.git] / index / inline.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2011 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17
18 */
19 #if HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <yaz/yaz-util.h>
26 #include "inline.h"
27
28 static void inline_destroy_subfield_recursive(inline_subfield *p);
29
30 inline_field *inline_mk_field(void)
31 {
32     inline_field *p = (inline_field *) xmalloc(sizeof(*p));
33
34     if (p)
35     {
36         memset(p, 0, sizeof(*p));
37         p->name = (char *) xmalloc(SZ_FNAME+1);
38         *(p->name) = '\0';
39         p->ind1 = (char *) xmalloc(SZ_IND+1);
40         *(p->ind1) = '\0';
41         p->ind2 = (char *) xmalloc(SZ_IND+1);
42         *(p->ind2) = '\0';
43     }
44     return p;
45 }
46 void inline_destroy_field(inline_field *p)
47 {
48     if (p)
49     {
50         if (p->name) xfree(p->name);
51         if (p->ind1) xfree(p->ind1);
52         if (p->ind2) xfree(p->ind2);
53         if (p->list)
54             inline_destroy_subfield_recursive(p->list);
55         xfree(p);
56     }
57 }
58 static inline_subfield *inline_mk_subfield(inline_subfield *parent)
59 {
60     inline_subfield *p = (inline_subfield *)xmalloc(sizeof(*p));
61     
62     if (p)
63     {
64         memset(p, 0, sizeof(*p));
65         p->name = (char *) xmalloc(SZ_SFNAME+1);
66         *(p->name) = '\0';
67         p->parent = parent;
68     }
69     return p;
70 }
71
72 #if 0
73 static void inline_destroy_subfield(inline_subfield *p)
74 {
75     if (p)
76     {
77         if (p->name) xfree(p->name);
78         if (p->data) xfree(p->data);
79         if (p->parent) p->parent->next = p->next;
80         xfree(p);
81     }
82 }
83 #endif
84
85 static void inline_destroy_subfield_recursive(inline_subfield *p)
86 {
87     if (p)
88     {
89         inline_destroy_subfield_recursive(p->next);
90         if (p->name) xfree(p->name);
91         if (p->data) xfree(p->data);
92         if (p->parent)
93             p->parent->next = 0;
94         xfree(p);
95     }
96 }
97 int inline_parse(inline_field *pif, const char *tag, const char *s)
98 {
99     inline_field *pf = pif;
100     char *p = (char *)s;
101     
102     if (!pf)
103         return -1;
104         
105     if (pf->name[0] == '\0')
106     {
107         if ((sscanf(p, "%3s", pf->name)) != 1)
108             return -2;
109
110         p += SZ_FNAME;
111
112         if (!memcmp(pf->name, "00", 2))
113         {
114             pf->list = inline_mk_subfield(0);
115             pf->list->data = xstrdup(p);
116         }
117         else
118         {
119             if ((sscanf(p, "%c%c", pf->ind1, pf->ind2)) != 2)
120                 return -3;
121         }
122     }
123     else
124     {
125         inline_subfield *psf = inline_mk_subfield(0);
126         
127         sscanf(tag, "%1s", psf->name);
128         psf->data = xstrdup(p);
129         
130         if (!pf->list)
131         {
132             pf->list = psf;
133         }
134         else
135         {
136             inline_subfield *last = pf->list;
137             while (last->next)
138                 last = last->next;
139             last->next = psf;
140         }
141     }
142     return 0;
143 }
144 /*
145  * Local variables:
146  * c-basic-offset: 4
147  * c-file-style: "Stroustrup"
148  * indent-tabs-mode: nil
149  * End:
150  * vim: shiftwidth=4 tabstop=8 expandtab
151  */
152