Fixed verbose of xrealloc.
[yaz-moved-to-github.git] / retrieval / d1_attset.c
1 /*
2  * Copyright (c) 1995, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_attset.c,v $
7  * Revision 1.2  1995-11-01 16:34:55  quinn
8  * Making data1 look for tables in data1_tabpath
9  *
10  * Revision 1.1  1995/11/01  11:56:07  quinn
11  * Added Retrieval (data management) functions en masse.
12  *
13  *
14  */
15
16 #include <ctype.h>
17 #include <stdio.h>
18 #include <assert.h>
19 #include <stdlib.h>
20
21 #include <xmalloc.h>
22 #include <log.h>
23 #include <d1_attset.h>
24 #include <data1.h>
25 #include <tpath.h>
26
27 data1_att *data1_getattbyname(data1_attset *s, char *name)
28 {
29     data1_att *r;
30
31     for (; s; s = s->next)
32     {
33         /* scan local set */
34         for (r = s->atts; r; r = r->next)
35             if (!strcmp(r->name, name))
36                 return r;
37         /* scan included sets */
38         if (s->children && (r = data1_getattbyname(s->children, name)))
39             return r;
40     }
41     return 0;
42 }
43
44 data1_attset *data1_read_attset(char *file)
45 {
46     char line[512], *r, cmd[512], args[512];
47     data1_attset *res = 0, **childp;
48     data1_att **attp;
49     FILE *f;
50
51     if (!(f = yaz_path_fopen(data1_tabpath, file, "r")))
52     {
53         logf(LOG_WARN|LOG_ERRNO, "%s", file);
54         return 0;
55     }
56
57     if (!(res = xmalloc(sizeof(*res))))
58         abort();
59     res->name = 0;
60     res->reference = VAL_NONE;
61     res->ordinal = -1;
62     res->atts = 0;
63     res->children = res->next = 0;
64     childp = &res->children;
65     attp = &res->atts;
66
67     for (;;)
68     {
69         while ((r = fgets(line, 512, f)))
70         {
71             while (*r && isspace(*r))
72                 r++;
73             if (*r && *r != '#')
74                 break;
75         }
76         if (!r)
77         {
78             return res;
79             fclose(f);
80         }
81         if (sscanf(r, "%s %[^\n]", cmd, args) < 2)
82             *args = '\0';
83         if (!strcmp(cmd, "att"))
84         {
85             int num, local, rr;
86             char name[512];
87             data1_att *t;
88
89             if ((rr = sscanf(args, "%d %s %d", &num, name, &local)) < 2)
90             {
91                 logf(LOG_WARN, "Not enough arguments to att in '%s' in %s",
92                     args, file);
93                 fclose(f);
94                 return 0;
95             }
96             if (rr < 3)
97                 local = num;
98             if (!(t = *attp = xmalloc(sizeof(*t))))
99                 abort();
100             t->parent = res;
101             if (!(t->name = xmalloc(strlen(name)+1)))
102                 abort();
103             strcpy(t->name, name);
104             t->value = num;
105             t->local = local;
106             t->next = 0;
107             attp = &t->next;
108         }
109         else if (!strcmp(cmd, "name"))
110         {
111             char name[512];
112
113             if (!sscanf(args, "%s", name))
114             {
115                 logf(LOG_WARN, "%s malformed name directive in %s", file);
116                 fclose(f);
117                 return 0;
118             }
119             if (!(res->name = xmalloc(strlen(args)+1)))
120                 abort();
121             strcpy(res->name, name);
122         }
123         else if (!strcmp(cmd, "reference"))
124         {
125             char name[512];
126
127             if (!sscanf(args, "%s", name))
128             {
129                 logf(LOG_WARN, "%s malformed reference directive in %s", file);
130                 fclose(f);
131                 return 0;
132             }
133             if ((res->reference = oid_getvalbyname(name)) == VAL_NONE)
134             {
135                 logf(LOG_WARN, "Unknown attset name '%s' in %s", name, file);
136                 fclose(f);
137                 return 0;
138             }
139         }
140         else if (!strcmp(cmd, "ordinal"))
141         {
142             if (!sscanf(args, "%d", &res->ordinal))
143             {
144                 logf(LOG_WARN, "%s malformed ordinal directive in %s", file);
145                 fclose(f);
146                 return 0;
147             }
148         }
149         else if (!strcmp(cmd, "include"))
150         {
151             char name[512];
152
153             if (!sscanf(args, "%s", name))
154             {
155                 logf(LOG_WARN, "%s malformed reference directive in %s", file);
156                 fclose(f);
157                 return 0;
158             }
159             if (!(*childp = data1_read_attset(name)))
160             {
161                 logf(LOG_WARN, "Inclusion failed in %s", file);
162                 fclose(f);
163                 return 0;
164             }
165             childp = &(*childp)->next;
166         }
167         else
168         {
169             logf(LOG_WARN, "Unknown directive '%s' in %s", cmd, file);
170             fclose(f);
171             return 0;
172         }
173     }
174 }