Moving data1 to yaz/retrieval
[idzebra-moved-to-github.git] / index / attribute.c
1 /*
2  * This interface is used by other modules (the Z-server in particular)
3  * to normalize the attributes given in queries.
4  */
5
6 #include <stdio.h>
7
8 #include <log.h>
9 #include <alexutil.h>
10 #include <res.h>
11
12 #include "d1_attset.h"
13
14 #include "attribute.h"
15
16 static int initialized = 0;
17
18 static data1_attset *registered_sets = 0;
19
20 static void att_loadset(const char *n, const char *name)
21 {
22     data1_attset *new;
23
24     if (!(new = data1_read_attset((char*) name)))
25     {
26         logf(LOG_WARN|LOG_ERRNO, "%s", name);
27         return;
28     }
29     new->next = registered_sets;
30     registered_sets = new;
31     return;
32 }
33
34 static void load_atts()
35 {
36     res_trav(common_resource, "attset", att_loadset);
37 }
38
39 static data1_att *getatt(data1_attset *p, int att)
40 {
41     data1_att *a;
42
43     for (; p; p = p->next)
44     {
45         /* scan local set */
46         for (a = p->atts; a; a = a->next)
47             if (a->value == att)
48                 return a;
49         /* scan included sets */
50         if (p->children && (a = getatt(p->children, att)))
51             return a;
52     }
53     return 0;
54 }
55
56 attent *att_getentbyatt(oid_value set, int att)
57 {
58     static attent res;
59     data1_att *r;
60     data1_attset *p;
61
62     if (!initialized)
63     {
64         initialized = 1;
65         load_atts();
66     }
67     for (p = registered_sets; p; p = p->next)
68         if (p->reference == set && (r = getatt(p, att)))
69             break;;
70     if (!p)
71         return 0;
72     res.attset_ordinal = r->parent->ordinal;
73     res.local_attribute = r->local;
74     return &res;
75 }