r152: a quick airport commit ....
[abartlet/samba.git/.git] / source4 / lib / ldb / common / ldb_msg.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 2 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb message component utility functions
29  *
30  *  Description: functions for manipulating ldb_message structures
31  *
32  *  Author: Andrew Tridgell
33  */
34
35 #include "includes.h"
36
37
38 /*
39   find an element in a message by attribute name
40 */
41 struct ldb_message_element *ldb_msg_find_element(const struct ldb_message *msg, 
42                                                  const char *attr_name)
43 {
44         int i;
45         for (i=0;i<msg->num_elements;i++) {
46                 if (strcmp(msg->elements[i].name, attr_name) == 0) {
47                         return &msg->elements[i];
48                 }
49         }
50         return NULL;
51 }
52
53
54 /*
55   find a value in an element
56 */
57 struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el, 
58                                  struct ldb_val *val)
59 {
60         int i;
61         for (i=0;i<el->num_values;i++) {
62                 if (ldb_val_equal(val, &el->values[i])) {
63                         return &el->values[i];
64                 }
65         }
66         return NULL;
67 }
68
69
70 /*
71   add an empty element to a message
72 */
73 int ldb_msg_add_empty(struct ldb_message *msg, const char *attr_name, int flags)
74 {
75         struct ldb_message_element *els;
76
77         els = realloc_p(msg->elements, struct ldb_message_element, msg->num_elements+1);
78         if (!els) {
79                 errno = ENOMEM;
80                 return -1;
81         }
82
83         els[msg->num_elements].values = NULL;
84         els[msg->num_elements].num_values = 0;
85         els[msg->num_elements].flags = flags;
86         els[msg->num_elements].name = strdup(attr_name);
87         if (!els[msg->num_elements].name) {
88                 return -1;
89         }
90
91         msg->elements = els;
92         msg->num_elements++;
93
94         return 0;
95 }
96
97 /*
98   add an empty element to a message
99 */
100 int ldb_msg_add(struct ldb_message *msg, 
101                 const struct ldb_message_element *el, 
102                 int flags)
103 {
104         if (ldb_msg_add_empty(msg, el->name, flags) != 0) {
105                 return -1;
106         }
107
108         msg->elements[msg->num_elements-1] = *el;
109         msg->elements[msg->num_elements-1].flags = flags;
110
111         return 0;
112 }
113
114 /*
115   compare two ldb_message_element structures
116 */
117 int ldb_msg_element_compare(struct ldb_message_element *el1, 
118                             struct ldb_message_element *el2)
119 {
120         int i;
121
122         if (el1->num_values != el2->num_values) {
123                 return el1->num_values - el2->num_values;
124         }
125
126         for (i=0;i<el1->num_values;i++) {
127                 if (!ldb_msg_find_val(el2, &el1->values[i])) {
128                         return -1;
129                 }
130         }
131
132         return 0;
133 }