Use Odr_oid for OIDs. Require YAZ 3.0.2 or later.
[idzebra-moved-to-github.git] / util / attrfind.c
1 /* $Id: attrfind.c,v 1.4 2007-05-08 12:50:06 adam Exp $
2    Copyright (C) 2005-2007
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 <assert.h>
24
25 #include <attrfind.h>
26
27 void attr_init_APT(AttrType *src, Z_AttributesPlusTerm *zapt, int type)
28 {
29     src->attributeList = zapt->attributes->attributes;
30     src->num_attributes = zapt->attributes->num_attributes;
31     src->type = type;
32     src->major = 0;
33     src->minor = 0;
34 }
35
36 void attr_init_AttrList(AttrType *src, Z_AttributeList *list, int type)
37 {
38     src->attributeList = list->attributes;
39     src->num_attributes = list->num_attributes;
40     src->type = type;
41     src->major = 0;
42     src->minor = 0;
43 }
44
45 int attr_find_ex(AttrType *src, const Odr_oid **attribute_set_oid,
46                  const char **string_value)
47 {
48     int num_attributes;
49
50     num_attributes = src->num_attributes;
51     while (src->major < num_attributes)
52     {
53         Z_AttributeElement *element;
54
55         element = src->attributeList[src->major];
56         if (src->type == *element->attributeType)
57         {
58             switch (element->which) 
59             {
60             case Z_AttributeValue_numeric:
61                 ++(src->major);
62                 if (element->attributeSet && attribute_set_oid)
63                     *attribute_set_oid = element->attributeSet;
64                 return *element->value.numeric;
65                 break;
66             case Z_AttributeValue_complex:
67                 if (src->minor >= element->value.complex->num_list)
68                     break;
69                 if (element->attributeSet && attribute_set_oid)
70                     *attribute_set_oid = element->attributeSet;
71                 if (element->value.complex->list[src->minor]->which ==  
72                     Z_StringOrNumeric_numeric)
73                 {
74                     ++(src->minor);
75                     return
76                         *element->value.complex->list[src->minor-1]->u.numeric;
77                 }
78                 else if (element->value.complex->list[src->minor]->which ==  
79                          Z_StringOrNumeric_string)
80                 {
81                     if (!string_value)
82                         break;
83                     ++(src->minor);
84                     *string_value = 
85                         element->value.complex->list[src->minor-1]->u.string;
86                     return -2;
87                 }
88                 else
89                     break;
90             default:
91                 assert(0);
92             }
93         }
94         ++(src->major);
95     }
96     return -1;
97 }
98
99 int attr_find(AttrType *src, const Odr_oid **attribute_set_id)
100 {
101     return attr_find_ex(src, attribute_set_id, 0);
102 }
103
104
105 /*
106  * Local variables:
107  * c-basic-offset: 4
108  * indent-tabs-mode: nil
109  * End:
110  * vim: shiftwidth=4 tabstop=8 expandtab
111  */
112