Zebra version corresponds to YAZ version 1.4.
[idzebra-moved-to-github.git] / index / attribute.c
1 /*
2  * Copyright (C) 1994-1997, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: attribute.c,v $
7  * Revision 1.6  1997-09-17 12:19:11  adam
8  * Zebra version corresponds to YAZ version 1.4.
9  * Changed Zebra server so that it doesn't depend on global common_resource.
10  *
11  * Revision 1.5  1997/09/05 15:30:08  adam
12  * Changed prototype for chr_map_input - added const.
13  * Added support for C++, headers uses extern "C" for public definitions.
14  *
15  * Revision 1.4  1996/10/29 14:06:48  adam
16  * Include zebrautl.h instead of alexutil.h.
17  *
18  * Revision 1.3  1996/05/09 07:28:54  quinn
19  * Work towards phrases and multiple registers
20  *
21  * Revision 1.2  1995/11/15  19:13:07  adam
22  * Work on record management.
23  *
24  *
25  * This interface is used by other modules (the Z-server in particular)
26  * to normalize the attributes given in queries.
27  */
28
29 #include <stdio.h>
30
31 #include <log.h>
32 #include <res.h>
33 #include <zebrautl.h>
34 #include "zserver.h"
35
36 static void att_loadset(void *p, const char *n, const char *name)
37 {
38     data1_attset *cnew;
39     ZServerInfo *zi = p;
40
41     if (!(cnew = data1_read_attset(zi->dh, (char*) name)))
42     {
43         logf(LOG_WARN|LOG_ERRNO, "%s", name);
44         return;
45     }
46     cnew->next = zi->registered_sets;
47     zi->registered_sets = cnew;
48 }
49
50 static void load_atts(ZServerInfo *zi)
51 {
52     res_trav(zi->res, "attset", zi, att_loadset);
53 }
54
55 static data1_att *getatt(data1_attset *p, int att)
56 {
57     data1_att *a;
58
59     for (; p; p = p->next)
60     {
61         /* scan local set */
62         for (a = p->atts; a; a = a->next)
63             if (a->value == att)
64                 return a;
65         /* scan included sets */
66         if (p->children && (a = getatt(p->children, att)))
67             return a;
68     }
69     return 0;
70 }
71
72 int att_getentbyatt(ZServerInfo *zi, attent *res, oid_value set, int att)
73 {
74     data1_att *r;
75     data1_attset *p;
76
77     if (!zi->registered_sets)
78         load_atts(zi);
79     for (p = zi->registered_sets; p; p = p->next)
80         if (p->reference == set && (r = getatt(p, att)))
81             break;;
82     if (!p)
83         return 0;
84     res->attset_ordinal = r->parent->ordinal;
85     res->local_attributes = r->locals;
86     return 1;
87 }