4068da04fa8592e8342f80cc60cb98f74fc08758
[idzebra-moved-to-github.git] / index / inline.c
1 /*
2     $Id: inline.c,v 1.1 2006-07-03 14:27:09 adam Exp $
3 */
4 #include <stdio.h>
5 #include <string.h>
6 #include <ctype.h>
7 #include <yaz/yaz-util.h>
8 #include "inline.h"
9
10 static void inline_destroy_subfield_recursive(inline_subfield *p);
11
12 inline_field *inline_mk_field(void)
13 {
14     inline_field *p = (inline_field *) xmalloc(sizeof(*p));
15
16     if (p)
17     {
18         memset(p, 0, sizeof(*p));
19         p->name = (char *) xmalloc(SZ_FNAME+1);
20         *(p->name) = '\0';
21         p->ind1 = (char *) xmalloc(SZ_IND+1);
22         *(p->ind1) = '\0';
23         p->ind2 = (char *) xmalloc(SZ_IND+1);
24         *(p->ind2) = '\0';
25     }
26     return p;
27 }
28 void inline_destroy_field(inline_field *p)
29 {
30     if (p)
31     {
32         if (p->name) xfree(p->name);
33         if (p->ind1) xfree(p->ind1);
34         if (p->ind2) xfree(p->ind2);
35         if (p->list)
36             inline_destroy_subfield_recursive(p->list);
37         xfree(p);
38     }
39 }
40 static inline_subfield *inline_mk_subfield(inline_subfield *parent)
41 {
42     inline_subfield *p = (inline_subfield *)xmalloc(sizeof(*p));
43     
44     if (p)
45     {
46         memset(p, 0, sizeof(*p));
47         p->name = (char *) xmalloc(SZ_SFNAME+1);
48         *(p->name) = '\0';
49         p->parent = parent;
50     }
51     return p;
52 }
53
54 #if 0
55 static void inline_destroy_subfield(inline_subfield *p)
56 {
57     if (p)
58     {
59         if (p->name) xfree(p->name);
60         if (p->data) xfree(p->data);
61         if (p->parent) p->parent->next = p->next;
62         xfree(p);
63     }
64 }
65 #endif
66
67 static void inline_destroy_subfield_recursive(inline_subfield *p)
68 {
69     if (p)
70     {
71         inline_destroy_subfield_recursive(p->next);
72         if (p->name) xfree(p->name);
73         if (p->data) xfree(p->data);
74         if (p->parent)
75             p->parent->next = 0;
76         xfree(p);
77     }
78 }
79 int inline_parse(inline_field *pif, const char *tag, const char *s)
80 {
81     inline_field *pf = pif;
82     char *p = (char *)s;
83     
84     if (!pf)
85         return -1;
86         
87     if (pf->name[0] == '\0')
88     {
89         if ((sscanf(p, "%3s", pf->name)) != 1)
90             return -2;
91
92         p += SZ_FNAME;
93
94         if (!memcmp(pf->name, "00", 2))
95         {
96             pf->list = inline_mk_subfield(0);
97             pf->list->data = xstrdup(p);
98         }
99         else
100         {
101             if ((sscanf(p, "%c%c", pf->ind1, pf->ind2)) != 2)
102                 return -3;
103         }
104     }
105     else
106     {
107         inline_subfield *psf = inline_mk_subfield(0);
108         
109         sscanf(tag, "%1s", psf->name);
110         psf->data = xstrdup(p);
111         
112         if (!pf->list)
113         {
114             pf->list = psf;
115         }
116         else
117         {
118             inline_subfield *last = pf->list;
119             while (last->next)
120                 last = last->next;
121             last->next = psf;
122         }
123     }
124     return 0;
125 }
126 /*
127  * Local variables:
128  * c-basic-offset: 4
129  * indent-tabs-mode: nil
130  * End:
131  * vim: shiftwidth=4 tabstop=8 expandtab
132  */
133