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