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
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_private.h"
41 find an element in a message by attribute name
43 struct ldb_message_element *ldb_msg_find_element(const struct ldb_message *msg,
44 const char *attr_name)
47 for (i=0;i<msg->num_elements;i++) {
48 if (ldb_attr_cmp(msg->elements[i].name, attr_name) == 0) {
49 return &msg->elements[i];
56 see if two ldb_val structures contain exactly the same data
57 return 1 for a match, 0 for a mis-match
59 int ldb_val_equal_exact(const struct ldb_val *v1, const struct ldb_val *v2)
61 if (v1->length != v2->length) return 0;
63 if (v1->length == 0) return 1;
65 if (memcmp(v1->data, v2->data, v1->length) == 0) {
73 find a value in an element
74 assumes case sensitive comparison
76 struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el,
80 for (i=0;i<el->num_values;i++) {
81 if (ldb_val_equal_exact(val, &el->values[i])) {
82 return &el->values[i];
89 duplicate a ldb_val structure
91 struct ldb_val ldb_val_dup(struct ldb_context *ldb,
92 const struct ldb_val *v)
95 v2.length = v->length;
101 /* the +1 is to cope with buggy C library routines like strndup
102 that look one byte beyond */
103 v2.data = ldb_malloc(ldb, v->length+1);
109 memcpy(v2.data, v->data, v->length);
110 ((char *)v2.data)[v->length] = 0;
115 add an empty element to a message
117 int ldb_msg_add_empty(struct ldb_context *ldb,
118 struct ldb_message *msg, const char *attr_name, int flags)
120 struct ldb_message_element *els;
122 els = ldb_realloc_p(ldb, msg->elements,
123 struct ldb_message_element, msg->num_elements+1);
129 els[msg->num_elements].values = NULL;
130 els[msg->num_elements].num_values = 0;
131 els[msg->num_elements].flags = flags;
132 els[msg->num_elements].name = ldb_strdup(ldb, attr_name);
133 if (!els[msg->num_elements].name) {
144 add an empty element to a message
146 int ldb_msg_add(struct ldb_context *ldb,
147 struct ldb_message *msg,
148 const struct ldb_message_element *el,
151 if (ldb_msg_add_empty(ldb, msg, el->name, flags) != 0) {
155 msg->elements[msg->num_elements-1] = *el;
156 msg->elements[msg->num_elements-1].flags = flags;
162 add a value to a message
164 int ldb_msg_add_value(struct ldb_context *ldb,
165 struct ldb_message *msg,
166 const char *attr_name,
169 struct ldb_message_element *el;
170 struct ldb_val *vals;
172 el = ldb_msg_find_element(msg, attr_name);
174 ldb_msg_add_empty(ldb, msg, attr_name, 0);
175 el = ldb_msg_find_element(msg, attr_name);
181 vals = ldb_realloc_p(ldb, el->values, struct ldb_val, el->num_values+1);
187 el->values[el->num_values] = *val;
195 add a string element to a message
197 int ldb_msg_add_string(struct ldb_context *ldb, struct ldb_message *msg,
198 const char *attr_name, char *str)
203 val.length = strlen(str);
205 return ldb_msg_add_value(ldb, msg, attr_name, &val);
209 compare two ldb_message_element structures
210 assumes case senistive comparison
212 int ldb_msg_element_compare(struct ldb_message_element *el1,
213 struct ldb_message_element *el2)
217 if (el1->num_values != el2->num_values) {
218 return el1->num_values - el2->num_values;
221 for (i=0;i<el1->num_values;i++) {
222 if (!ldb_msg_find_val(el2, &el1->values[i])) {
231 convenience functions to return common types from a message
232 these return the first value if the attribute is multi-valued
234 const struct ldb_val *ldb_msg_find_ldb_val(const struct ldb_message *msg, const char *attr_name)
236 struct ldb_message_element *el = ldb_msg_find_element(msg, attr_name);
237 if (!el || el->num_values == 0) {
240 return &el->values[0];
243 int ldb_msg_find_int(const struct ldb_message *msg,
244 const char *attr_name,
247 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
248 if (!v || !v->data) {
249 return default_value;
251 return strtol(v->data, NULL, 0);
254 unsigned int ldb_msg_find_uint(const struct ldb_message *msg,
255 const char *attr_name,
256 unsigned int default_value)
258 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
259 if (!v || !v->data) {
260 return default_value;
262 return strtoul(v->data, NULL, 0);
265 int64_t ldb_msg_find_int64(const struct ldb_message *msg,
266 const char *attr_name,
267 int64_t default_value)
269 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
270 if (!v || !v->data) {
271 return default_value;
273 return strtoll(v->data, NULL, 0);
276 uint64_t ldb_msg_find_uint64(const struct ldb_message *msg,
277 const char *attr_name,
278 uint64_t default_value)
280 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
281 if (!v || !v->data) {
282 return default_value;
284 return strtoull(v->data, NULL, 0);
287 double ldb_msg_find_double(const struct ldb_message *msg,
288 const char *attr_name,
289 double default_value)
291 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
292 if (!v || !v->data) {
293 return default_value;
295 return strtod(v->data, NULL);
298 const char *ldb_msg_find_string(const struct ldb_message *msg,
299 const char *attr_name,
300 const char *default_value)
302 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
303 if (!v || !v->data) {
304 return default_value;