change license for data1 source
[idzebra-moved-to-github.git] / data1 / d1_attset.c
1 /* $Id: d1_attset.c,v 1.2 2002-10-22 13:19:50 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 <d1_attset.h>
29 #include <data1.h>
30
31 data1_att *data1_getattbyname(data1_handle dh, data1_attset *s, char *name)
32 {
33     data1_att *r;
34     data1_attset_child *c;
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     for (c = s->children; c; c = c->next)
41     {
42         assert (c->child);
43         /* scan included sets */
44         if ((r = data1_getattbyname (dh, c->child, name)))
45             return r;
46     }
47     return 0;
48 }
49
50 data1_attset *data1_empty_attset(data1_handle dh)
51 {
52     NMEM mem = data1_nmem_get (dh);
53     data1_attset *res = (data1_attset*) nmem_malloc(mem,sizeof(*res));
54
55     res->name = 0;
56     res->reference = VAL_NONE;
57     res->atts = 0;
58     res->children = 0;
59     res->next = 0;
60     return res;
61 }
62
63 data1_attset *data1_read_attset(data1_handle dh, const char *file)
64 {
65     data1_attset *res = 0;
66     data1_attset_child **childp;
67     data1_att **attp;
68     FILE *f;
69     NMEM mem = data1_nmem_get (dh);
70     int lineno = 0;
71     int argc;
72     char *argv[50], line[512];
73
74     if (!(f = data1_path_fopen(dh, file, "r")))
75         return NULL;
76     res = data1_empty_attset (dh);
77
78     childp = &res->children;
79     attp = &res->atts;
80
81     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
82     {
83         char *cmd = argv[0];
84         if (!strcmp(cmd, "att"))
85         {
86             int num;
87             char *name;
88             data1_att *t;
89             data1_local_attribute *locals;
90             
91             if (argc < 3)
92             {
93                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to att", file, lineno);
94                 continue;
95             }
96             num = atoi (argv[1]);
97             name = argv[2];
98             
99             if (argc == 3) /* no local attributes given */
100             {
101                 locals = (data1_local_attribute *)
102                     nmem_malloc(mem, sizeof(*locals));
103                 locals->local = num;
104                 locals->next = 0;
105             }
106             else /* parse the string "local{,local}" */
107             {
108                 char *p = argv[3];
109                 data1_local_attribute **ap = &locals;
110                 do
111                 {
112                     *ap = (data1_local_attribute *)
113                         nmem_malloc(mem, sizeof(**ap));
114                     (*ap)->local = atoi(p);
115                     (*ap)->next = 0;
116                     ap = &(*ap)->next;
117                 }
118                 while ((p = strchr(p, ',')) && *(++p));
119             }
120             t = *attp = (data1_att *)nmem_malloc(mem, sizeof(*t));
121             t->parent = res;
122             t->name = nmem_strdup(mem, name);
123             t->value = num;
124             t->locals = locals;
125             t->next = 0;
126             attp = &t->next;
127         }
128         else if (!strcmp(cmd, "name"))
129         {
130             if (argc != 2)
131             {
132                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to name", file, lineno);
133                 continue;
134             }
135         }
136         else if (!strcmp(cmd, "reference"))
137         {
138             char *name;
139
140             if (argc != 2)
141             {
142                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to reference",
143                         file, lineno);
144                 continue;
145             }
146             name = argv[1];
147             if ((res->reference = oid_getvalbyname(name)) == VAL_NONE)
148             {
149                 yaz_log(LOG_WARN, "%s:%d: Unknown reference oid '%s'",
150                         file, lineno, name);
151                 fclose(f);
152                 return 0;
153             }
154         }
155         else if (!strcmp(cmd, "ordinal"))
156         {
157             yaz_log (LOG_WARN, "%s:%d: Directive ordinal ignored",
158                      file, lineno);
159         }
160         else if (!strcmp(cmd, "include"))
161         {
162             char *name;
163             data1_attset *attset;
164
165             if (argc != 2)
166             {
167                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to include",
168                         file, lineno);
169                 continue;
170             }
171             name = argv[1];
172
173             if (!(attset = data1_get_attset (dh, name)))
174             {
175                 yaz_log(LOG_WARN, "%s:%d: Include of attset %s failed",
176                         file, lineno, name);
177                 continue;
178                 
179             }
180             *childp = (data1_attset_child *)
181                 nmem_malloc (mem, sizeof(**childp));
182             (*childp)->child = attset;
183             (*childp)->next = 0;
184             childp = &(*childp)->next;
185         }
186         else
187         {
188             yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'",
189                     file, lineno, cmd);
190         }
191     }
192     fclose(f);
193     return res;
194 }