4 Copyright (C) Andrew Tridgell 2004
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
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.
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.
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
28 * Component: ldb message component utility functions
30 * Description: functions for manipulating ldb_message structures
32 * Author: Andrew Tridgell
39 find an element in a message by attribute name
41 struct ldb_message_element *ldb_msg_find_element(const struct ldb_message *msg,
42 const char *attr_name)
45 for (i=0;i<msg->num_elements;i++) {
46 if (ldb_attr_cmp(msg->elements[i].name, attr_name) == 0) {
47 return &msg->elements[i];
54 see if two ldb_val structures contain exactly the same data
55 return 1 for a match, 0 for a mis-match
57 int ldb_val_equal_exact(const struct ldb_val *v1, const struct ldb_val *v2)
59 if (v1->length != v2->length) return 0;
61 if (v1->length == 0) return 1;
63 if (memcmp(v1->data, v2->data, v1->length) == 0) {
71 find a value in an element
72 assumes case sensitive comparison
74 struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el,
78 for (i=0;i<el->num_values;i++) {
79 if (ldb_val_equal_exact(val, &el->values[i])) {
80 return &el->values[i];
88 add an empty element to a message
90 int ldb_msg_add_empty(struct ldb_context *ldb,
91 struct ldb_message *msg, const char *attr_name, int flags)
93 struct ldb_message_element *els;
95 els = ldb_realloc_p(ldb, msg->elements,
96 struct ldb_message_element, msg->num_elements+1);
102 els[msg->num_elements].values = NULL;
103 els[msg->num_elements].num_values = 0;
104 els[msg->num_elements].flags = flags;
105 els[msg->num_elements].name = ldb_strdup(ldb, attr_name);
106 if (!els[msg->num_elements].name) {
117 add an empty element to a message
119 int ldb_msg_add(struct ldb_context *ldb,
120 struct ldb_message *msg,
121 const struct ldb_message_element *el,
124 if (ldb_msg_add_empty(ldb, msg, el->name, flags) != 0) {
128 msg->elements[msg->num_elements-1] = *el;
129 msg->elements[msg->num_elements-1].flags = flags;
135 add a value to a message
137 int ldb_msg_add_value(struct ldb_context *ldb,
138 struct ldb_message *msg,
139 const char *attr_name,
142 struct ldb_message_element *el;
143 struct ldb_val *vals;
145 el = ldb_msg_find_element(msg, attr_name);
147 ldb_msg_add_empty(ldb, msg, attr_name, 0);
148 el = ldb_msg_find_element(msg, attr_name);
154 vals = ldb_realloc_p(ldb, el->values, struct ldb_val, el->num_values+1);
160 el->values[el->num_values] = *val;
168 add a string element to a message
170 int ldb_msg_add_string(struct ldb_context *ldb, struct ldb_message *msg,
171 const char *attr_name, char *str)
176 val.length = strlen(str);
178 return ldb_msg_add_value(ldb, msg, attr_name, &val);
182 compare two ldb_message_element structures
183 assumes case senistive comparison
185 int ldb_msg_element_compare(struct ldb_message_element *el1,
186 struct ldb_message_element *el2)
190 if (el1->num_values != el2->num_values) {
191 return el1->num_values - el2->num_values;
194 for (i=0;i<el1->num_values;i++) {
195 if (!ldb_msg_find_val(el2, &el1->values[i])) {
204 convenience functions to return common types from a message
205 these return the first value if the attribute is multi-valued
207 const struct ldb_val *ldb_msg_find_ldb_val(const struct ldb_message *msg, const char *attr_name)
209 struct ldb_message_element *el = ldb_msg_find_element(msg, attr_name);
210 if (!el || el->num_values == 0) {
213 return &el->values[0];
216 int ldb_msg_find_int(const struct ldb_message *msg,
217 const char *attr_name,
220 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
221 if (!v || !v->data) {
222 return default_value;
224 return strtol(v->data, NULL, 0);
227 unsigned int ldb_msg_find_uint(const struct ldb_message *msg,
228 const char *attr_name,
229 unsigned int default_value)
231 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
232 if (!v || !v->data) {
233 return default_value;
235 return strtoul(v->data, NULL, 0);
238 int64_t ldb_msg_find_int64(const struct ldb_message *msg,
239 const char *attr_name,
240 int64_t default_value)
242 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
243 if (!v || !v->data) {
244 return default_value;
246 return strtoll(v->data, NULL, 0);
249 uint64_t ldb_msg_find_uint64(const struct ldb_message *msg,
250 const char *attr_name,
251 uint64_t default_value)
253 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
254 if (!v || !v->data) {
255 return default_value;
257 return strtoull(v->data, NULL, 0);
260 double ldb_msg_find_double(const struct ldb_message *msg,
261 const char *attr_name,
262 double default_value)
264 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
265 if (!v || !v->data) {
266 return default_value;
268 return strtod(v->data, NULL);
271 const char *ldb_msg_find_string(const struct ldb_message *msg,
272 const char *attr_name,
273 const char *default_value)
275 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
276 if (!v || !v->data) {
277 return default_value;