Omit CVS Id. Update copyright year.
[idzebra-moved-to-github.git] / util / attrfind.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1995-2008 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #include <assert.h>
21
22 #include <attrfind.h>
23
24 void attr_init_APT(AttrType *src, Z_AttributesPlusTerm *zapt, int type)
25 {
26     src->attributeList = zapt->attributes->attributes;
27     src->num_attributes = zapt->attributes->num_attributes;
28     src->type = type;
29     src->major = 0;
30     src->minor = 0;
31 }
32
33 void attr_init_AttrList(AttrType *src, Z_AttributeList *list, int type)
34 {
35     src->attributeList = list->attributes;
36     src->num_attributes = list->num_attributes;
37     src->type = type;
38     src->major = 0;
39     src->minor = 0;
40 }
41
42 int attr_find_ex(AttrType *src, const Odr_oid **attribute_set_oid,
43                  const char **string_value)
44 {
45     int num_attributes;
46
47     num_attributes = src->num_attributes;
48     while (src->major < num_attributes)
49     {
50         Z_AttributeElement *element;
51
52         element = src->attributeList[src->major];
53         if (src->type == *element->attributeType)
54         {
55             switch (element->which) 
56             {
57             case Z_AttributeValue_numeric:
58                 ++(src->major);
59                 if (element->attributeSet && attribute_set_oid)
60                     *attribute_set_oid = element->attributeSet;
61                 return *element->value.numeric;
62                 break;
63             case Z_AttributeValue_complex:
64                 if (src->minor >= element->value.complex->num_list)
65                     break;
66                 if (element->attributeSet && attribute_set_oid)
67                     *attribute_set_oid = element->attributeSet;
68                 if (element->value.complex->list[src->minor]->which ==  
69                     Z_StringOrNumeric_numeric)
70                 {
71                     ++(src->minor);
72                     return
73                         *element->value.complex->list[src->minor-1]->u.numeric;
74                 }
75                 else if (element->value.complex->list[src->minor]->which ==  
76                          Z_StringOrNumeric_string)
77                 {
78                     if (!string_value)
79                         break;
80                     ++(src->minor);
81                     *string_value = 
82                         element->value.complex->list[src->minor-1]->u.string;
83                     return -2;
84                 }
85                 else
86                     break;
87             default:
88                 assert(0);
89             }
90         }
91         ++(src->major);
92     }
93     return -1;
94 }
95
96 int attr_find(AttrType *src, const Odr_oid **attribute_set_id)
97 {
98     return attr_find_ex(src, attribute_set_id, 0);
99 }
100
101
102 /*
103  * Local variables:
104  * c-basic-offset: 4
105  * indent-tabs-mode: nil
106  * End:
107  * vim: shiftwidth=4 tabstop=8 expandtab
108  */
109