Old versions of GILS tables
[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.5  1996-05-09 07:27:43  quinn
8  * Multiple local attributes values supported.
9  *
10  * Revision 1.4  1996/02/21  15:23:36  quinn
11  * Reversed fclose and return;
12  *
13  * Revision 1.3  1995/12/13  17:14:26  quinn
14  * *** empty log message ***
15  *
16  * Revision 1.2  1995/11/01  16:34:55  quinn
17  * Making data1 look for tables in data1_tabpath
18  *
19  * Revision 1.1  1995/11/01  11:56:07  quinn
20  * Added Retrieval (data management) functions en masse.
21  *
22  *
23  */
24
25 #include <ctype.h>
26 #include <stdio.h>
27 #include <assert.h>
28 #include <stdlib.h>
29
30 #include <xmalloc.h>
31 #include <log.h>
32 #include <d1_attset.h>
33 #include <data1.h>
34 #include <tpath.h>
35
36 data1_att *data1_getattbyname(data1_attset *s, char *name)
37 {
38     data1_att *r;
39
40     for (; s; s = s->next)
41     {
42         /* scan local set */
43         for (r = s->atts; r; r = r->next)
44             if (!data1_matchstr(r->name, name))
45                 return r;
46         /* scan included sets */
47         if (s->children && (r = data1_getattbyname(s->children, name)))
48             return r;
49     }
50     return 0;
51 }
52
53 data1_attset *data1_read_attset(char *file)
54 {
55     char line[512], *r, cmd[512], args[512];
56     data1_attset *res = 0, **childp;
57     data1_att **attp;
58     FILE *f;
59
60     if (!(f = yaz_path_fopen(data1_tabpath, file, "r")))
61     {
62         logf(LOG_WARN|LOG_ERRNO, "%s", file);
63         return 0;
64     }
65
66     if (!(res = xmalloc(sizeof(*res))))
67         abort();
68     res->name = 0;
69     res->reference = VAL_NONE;
70     res->ordinal = -1;
71     res->atts = 0;
72     res->children = res->next = 0;
73     childp = &res->children;
74     attp = &res->atts;
75
76     for (;;)
77     {
78         while ((r = fgets(line, 512, f)))
79         {
80             while (*r && isspace(*r))
81                 r++;
82             if (*r && *r != '#')
83                 break;
84         }
85         if (!r)
86         {
87             fclose(f);
88             return res;
89         }
90         if (sscanf(r, "%s %[^\n]", cmd, args) < 2)
91             *args = '\0';
92         if (!strcmp(cmd, "att"))
93         {
94             int num, rr;
95             char name[512], localstr[512];
96             data1_att *t;
97             data1_local_attribute *locals;
98
99             if ((rr = sscanf(args, "%511d %s %511s", &num, name, localstr)) < 2)
100             {
101                 logf(LOG_WARN, "Not enough arguments to att in '%s' in %s",
102                     args, file);
103                 fclose(f);
104                 return 0;
105             }
106             if (rr < 3) /* no local attributes given */
107             {
108                 locals = xmalloc(sizeof(*locals));
109                 locals->local = num;
110                 locals->next = 0;
111             }
112             else /* parse the string "local{,local}" */
113             {
114                 char *p = localstr;
115                 data1_local_attribute **ap = &locals;
116                 do
117                 {
118                     *ap = xmalloc(sizeof(**ap));
119                     (*ap)->local = atoi(p);
120                     (*ap)->next = 0;
121                     ap = &(*ap)->next;
122                 }
123                 while ((p = strchr(p, ',')) && *(++p));
124             }
125             if (!(t = *attp = xmalloc(sizeof(*t))))
126                 abort();
127             t->parent = res;
128             if (!(t->name = xmalloc(strlen(name)+1)))
129                 abort();
130             strcpy(t->name, name);
131             t->value = num;
132             t->locals = locals;
133             t->next = 0;
134             attp = &t->next;
135         }
136         else if (!strcmp(cmd, "name"))
137         {
138             char name[512];
139
140             if (!sscanf(args, "%s", name))
141             {
142                 logf(LOG_WARN, "%s malformed name directive in %s", file);
143                 fclose(f);
144                 return 0;
145             }
146             if (!(res->name = xmalloc(strlen(args)+1)))
147                 abort();
148             strcpy(res->name, name);
149         }
150         else if (!strcmp(cmd, "reference"))
151         {
152             char name[512];
153
154             if (!sscanf(args, "%s", name))
155             {
156                 logf(LOG_WARN, "%s malformed reference directive in %s", file);
157                 fclose(f);
158                 return 0;
159             }
160             if ((res->reference = oid_getvalbyname(name)) == VAL_NONE)
161             {
162                 logf(LOG_WARN, "Unknown attset name '%s' in %s", name, file);
163                 fclose(f);
164                 return 0;
165             }
166         }
167         else if (!strcmp(cmd, "ordinal"))
168         {
169             if (!sscanf(args, "%d", &res->ordinal))
170             {
171                 logf(LOG_WARN, "%s malformed ordinal directive in %s", file);
172                 fclose(f);
173                 return 0;
174             }
175         }
176         else if (!strcmp(cmd, "include"))
177         {
178             char name[512];
179
180             if (!sscanf(args, "%s", name))
181             {
182                 logf(LOG_WARN, "%s malformed reference directive in %s", file);
183                 fclose(f);
184                 return 0;
185             }
186             if (!(*childp = data1_read_attset(name)))
187             {
188                 logf(LOG_WARN, "Inclusion failed in %s", file);
189                 fclose(f);
190                 return 0;
191             }
192             childp = &(*childp)->next;
193         }
194         else
195         {
196             logf(LOG_WARN, "Unknown directive '%s' in %s", cmd, file);
197             fclose(f);
198             return 0;
199         }
200     }
201 }