Happy new year
[idzebra-moved-to-github.git] / data1 / d1_attset.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2009 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #include <stdio.h>
21 #include <assert.h>
22 #include <stdlib.h>
23
24 #include <yaz/log.h>
25 #include <yaz/oid_db.h>
26 #include <idzebra/data1.h>
27
28 data1_att *data1_getattbyname(data1_handle dh, data1_attset *s, const char *name)
29 {
30     data1_att *r;
31     data1_attset_child *c;
32     
33     /* scan local set */
34     for (r = s->atts; r; r = r->next)
35         if (!data1_matchstr(r->name, name))
36             return r;
37     for (c = s->children; c; c = c->next)
38     {
39         assert (c->child);
40         /* scan included sets */
41         if ((r = data1_getattbyname (dh, c->child, name)))
42             return r;
43     }
44     return 0;
45 }
46
47 data1_attset *data1_empty_attset(data1_handle dh)
48 {
49     NMEM mem = data1_nmem_get (dh);
50     data1_attset *res = (data1_attset*) nmem_malloc(mem,sizeof(*res));
51
52     res->name = 0;
53     res->oid = 0;
54     res->atts = 0;
55     res->children = 0;
56     res->next = 0;
57     return res;
58 }
59
60 data1_attset *data1_read_attset(data1_handle dh, const char *file)
61 {
62     data1_attset *res = 0;
63     data1_attset_child **childp;
64     data1_att **attp;
65     FILE *f;
66     NMEM mem = data1_nmem_get (dh);
67     int lineno = 0;
68     int argc;
69     char *argv[50], line[512];
70
71     if (!(f = data1_path_fopen(dh, file, "r")))
72         return NULL;
73     res = data1_empty_attset (dh);
74
75     childp = &res->children;
76     attp = &res->atts;
77
78     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
79     {
80         char *cmd = argv[0];
81         if (!strcmp(cmd, "att"))
82         {
83             int num;
84             char *name;
85             char *endptr;
86             data1_att *t;
87             
88             if (argc < 3)
89             {
90                 yaz_log(YLOG_WARN, "%s:%d: Bad # of args to att", file, lineno);
91                 continue;
92             }
93             if (argc > 3)
94             {
95                 yaz_log(YLOG_WARN, "%s:%d: Local attributes not supported",
96                         file, lineno);
97             }
98             num = strtol(argv[1], &endptr, 10);
99             if (*endptr != '\0')
100             {
101                 yaz_log(YLOG_WARN, "%s:%d: Bad attribute integer %s",
102                         file, lineno, argv[1]);
103                 continue;
104             }
105             name = argv[2];
106             
107             t = *attp = (data1_att *)nmem_malloc(mem, sizeof(*t));
108             t->parent = res;
109             t->name = nmem_strdup(mem, name);
110             t->value = num;
111             t->next = 0;
112             attp = &t->next;
113         }
114         else if (!strcmp(cmd, "name"))
115         {
116             if (argc != 2)
117             {
118                 yaz_log(YLOG_WARN, "%s:%d: Bad # of args to name", file, lineno);
119                 continue;
120             }
121         }
122         else if (!strcmp(cmd, "reference"))
123         {
124             char *name;
125
126             if (argc != 2)
127             {
128                 yaz_log(YLOG_WARN, "%s:%d: Bad # of args to reference",
129                         file, lineno);
130                 continue;
131             }
132             name = argv[1];
133
134             res->oid  = yaz_string_to_oid_nmem(yaz_oid_std(),
135                                                CLASS_ATTSET, name, mem);
136             if (!res->oid)
137             {
138                 yaz_log(YLOG_WARN, "%s:%d: Unknown reference oid '%s'",
139                         file, lineno, name);
140                 fclose(f);
141                 return 0;
142             }
143         }
144         else if (!strcmp(cmd, "ordinal"))
145         {
146             yaz_log (YLOG_WARN, "%s:%d: Directive ordinal ignored",
147                      file, lineno);
148         }
149         else if (!strcmp(cmd, "include"))
150         {
151             char *name;
152             data1_attset *attset;
153
154             if (argc != 2)
155             {
156                 yaz_log(YLOG_WARN, "%s:%d: Bad # of args to include",
157                         file, lineno);
158                 continue;
159             }
160             name = argv[1];
161
162             if (!(attset = data1_get_attset (dh, name)))
163             {
164                 yaz_log(YLOG_WARN, "%s:%d: Include of attset %s failed",
165                         file, lineno, name);
166                 continue;
167                 
168             }
169             *childp = (data1_attset_child *)
170                 nmem_malloc (mem, sizeof(**childp));
171             (*childp)->child = attset;
172             (*childp)->next = 0;
173             childp = &(*childp)->next;
174         }
175         else
176         {
177             yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'",
178                     file, lineno, cmd);
179         }
180     }
181     fclose(f);
182     return res;
183 }
184 /*
185  * Local variables:
186  * c-basic-offset: 4
187  * indent-tabs-mode: nil
188  * End:
189  * vim: shiftwidth=4 tabstop=8 expandtab
190  */
191