diff options
Diffstat (limited to 'source4/lib/ldb/common/ldb_msg.c')
-rw-r--r-- | source4/lib/ldb/common/ldb_msg.c | 71 |
1 files changed, 69 insertions, 2 deletions
diff --git a/source4/lib/ldb/common/ldb_msg.c b/source4/lib/ldb/common/ldb_msg.c index 633cc91f2c..8eb8a8c5ef 100644 --- a/source4/lib/ldb/common/ldb_msg.c +++ b/source4/lib/ldb/common/ldb_msg.c @@ -43,23 +43,40 @@ struct ldb_message_element *ldb_msg_find_element(const struct ldb_message *msg, { int i; for (i=0;i<msg->num_elements;i++) { - if (strcmp(msg->elements[i].name, attr_name) == 0) { + if (ldb_attr_cmp(msg->elements[i].name, attr_name) == 0) { return &msg->elements[i]; } } return NULL; } +/* + see if two ldb_val structures contain exactly the same data + return 1 for a match, 0 for a mis-match +*/ +int ldb_val_equal_exact(const struct ldb_val *v1, const struct ldb_val *v2) +{ + if (v1->length != v2->length) return 0; + + if (v1->length == 0) return 1; + + if (memcmp(v1->data, v2->data, v1->length) == 0) { + return 1; + } + + return 0; +} /* find a value in an element + assumes case sensitive comparison */ struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el, struct ldb_val *val) { int i; for (i=0;i<el->num_values;i++) { - if (ldb_val_equal(val, &el->values[i])) { + if (ldb_val_equal_exact(val, &el->values[i])) { return &el->values[i]; } } @@ -113,6 +130,7 @@ int ldb_msg_add(struct ldb_message *msg, /* compare two ldb_message_element structures + assumes case senistive comparison */ int ldb_msg_element_compare(struct ldb_message_element *el1, struct ldb_message_element *el2) @@ -131,3 +149,52 @@ int ldb_msg_element_compare(struct ldb_message_element *el1, return 0; } + + +/* + convenience functions to return common types from a message + these return the first value if the attribute is multi-valued +*/ +int ldb_msg_find_int(const struct ldb_message *msg, + const char *attr_name, + int default_value) +{ + struct ldb_message_element *el = ldb_msg_find_element(msg, attr_name); + if (!el || el->num_values == 0) { + return default_value; + } + return strtol(el->values[0].data, NULL, 0); +} + +unsigned int ldb_msg_find_uint(const struct ldb_message *msg, + const char *attr_name, + int default_value) +{ + struct ldb_message_element *el = ldb_msg_find_element(msg, attr_name); + if (!el || el->num_values == 0) { + return default_value; + } + return strtoul(el->values[0].data, NULL, 0); +} + +double ldb_msg_find_double(const struct ldb_message *msg, + const char *attr_name, + double default_value) +{ + struct ldb_message_element *el = ldb_msg_find_element(msg, attr_name); + if (!el || el->num_values == 0) { + return default_value; + } + return strtod(el->values[0].data, NULL); +} + +const char *ldb_msg_find_string(const struct ldb_message *msg, + const char *attr_name, + const char *default_value) +{ + struct ldb_message_element *el = ldb_msg_find_element(msg, attr_name); + if (!el || el->num_values == 0) { + return default_value; + } + return el->values[0].data; +} |