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