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