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