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