diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2008-04-17 21:12:21 +0000 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2008-04-17 21:12:21 +0000 |
commit | 2e48b85903b04117f9314b9ed69f0ac2d4d78356 (patch) | |
tree | 3414b0f55788c52c3ed8da25977ddb632a3f0c4e | |
parent | 15b3f8bc5a757d984e5510fdaec46196d8939a56 (diff) |
change dict_values_first() and dict_values_next() into dict_loop_first() and dict_loop_next() to have a looping mechanism over keys and values
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-ldapd@675 ef36b2f9-881f-0410-afb5-c4e39611909c
-rw-r--r-- | common/dict.c | 12 | ||||
-rw-r--r-- | common/dict.h | 18 | ||||
-rw-r--r-- | nslcd/myldap.c | 4 | ||||
-rw-r--r-- | tests/test_dict.c | 33 |
4 files changed, 36 insertions, 31 deletions
diff --git a/common/dict.c b/common/dict.c index b6e54cf..70c746a 100644 --- a/common/dict.c +++ b/common/dict.c @@ -2,7 +2,7 @@ dict.c - dictionary functions This file is part of the nss-ldapd library. - Copyright (C) 2007 Arthur de Jong + Copyright (C) 2007, 2008 Arthur de Jong This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -141,12 +141,12 @@ void dict_free(DICT *dict) free(dict); } -void dict_values_first(DICT *dict) +void dict_loop_first(DICT *dict) { dict->ptr=dict->head; } -void *dict_values_next(DICT *dict) +const char *dict_loop_next(DICT *dict,const char **key,void **value) { struct dict_entry *ptr; ptr=dict->ptr; @@ -157,8 +157,12 @@ void *dict_values_next(DICT *dict) if (ptr==NULL) { dict->ptr=NULL; + if (key!=NULL) *key=NULL; + if (value!=NULL) *value=NULL; return NULL; } dict->ptr=ptr->next; - return ptr->value; + if (key!=NULL) *key=ptr->key; + if (value!=NULL) *value=ptr->value; + return ptr->key; } diff --git a/common/dict.h b/common/dict.h index c91c901..c34ecc2 100644 --- a/common/dict.h +++ b/common/dict.h @@ -2,7 +2,7 @@ dict.h - dictionary functions This file is part of the nss-ldapd library. - Copyright (C) 2007 Arthur de Jong + Copyright (C) 2007, 2008 Arthur de Jong This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -57,15 +57,17 @@ void *dict_get(DICT *dict,const char *key) for the dictionary and the keys is freed. */ void dict_free(DICT *dict); -/* Function for looping over all dictionary values. +/* Function for looping over all dictionary keys and values. This resets the search to the beginning of the dictionary. - This is required before calling dict_values_next(); */ -void dict_values_first(DICT *dict); + This is required before calling dict_loop_next(); */ +void dict_loop_first(DICT *dict); -/* Function for looping over all dictionary values. - This returns a stored value. NULL is returned when all - stored values have been returned. */ -void *dict_values_next(DICT *dict) +/* Function for looping over all dictionary keys and values. + This returns a stored key. NULL is returned when all + keys have been returned. The key and value are + stored in the key and value parameters if they aren't + NULL. */ +const char *dict_loop_next(DICT *dict,const char **key,void **value) MUST_USE; #endif /* _DICT_H */ diff --git a/nslcd/myldap.c b/nslcd/myldap.c index 12df572..6547113 100644 --- a/nslcd/myldap.c +++ b/nslcd/myldap.c @@ -169,8 +169,8 @@ static void myldap_entry_free(MYLDAP_ENTRY *entry) if (entry->exploded_rdn!=NULL) ldap_value_free(entry->exploded_rdn); /* free all attribute values */ - dict_values_first(entry->attributevalues); - while ((values=(char **)dict_values_next(entry->attributevalues))!=NULL) + dict_loop_first(entry->attributevalues); + while (dict_loop_next(entry->attributevalues,NULL,(void *)&values)!=NULL) ldap_value_free(values); dict_free(entry->attributevalues); /* we don't need the result anymore, ditch it. */ diff --git a/tests/test_dict.c b/tests/test_dict.c index 7cfc169..4c19c2b 100644 --- a/tests/test_dict.c +++ b/tests/test_dict.c @@ -2,7 +2,7 @@ test_dict.c - simple test for the dict module This file is part of the nss-ldapd library. - Copyright (C) 2007 Arthur de Jong + Copyright (C) 2007, 2008 Arthur de Jong This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -33,7 +33,8 @@ int main(int UNUSED(argc),char UNUSED(*argv[])) { DICT *dict; - void *ret; + const char *key; + void *val; static char *value1="value1"; static char *value2="value2"; static char *replace2="replace2"; @@ -48,31 +49,29 @@ int main(int UNUSED(argc),char UNUSED(*argv[])) dict_put(dict,"KEY2",replace2); /* check dictionary contents */ - ret=dict_get(dict,"KeY1"); - assert(ret==value1); - ret=dict_get(dict,"kEy2"); - assert(ret==replace2); - ret=dict_get(dict,"KeY3"); - assert(ret==dict); - ret=dict_get(dict,"key4"); - assert(ret==NULL); + val=dict_get(dict,"KeY1"); + assert(val==value1); + val=dict_get(dict,"kEy2"); + assert(val==replace2); + val=dict_get(dict,"KeY3"); + assert(val==dict); + val=dict_get(dict,"key4"); + assert(val==NULL); /* remove a key */ dict_put(dict,"kEy3",NULL); - ret=dict_get(dict,"keY3"); - assert(ret==NULL); + val=dict_get(dict,"keY3"); + assert(val==NULL); /* loop over dictionary contents */ - dict_values_first(dict); - while ((ret=dict_values_next(dict))!=NULL) + dict_loop_first(dict); + while (dict_loop_next(dict,&key,&val)!=NULL) { - assert(((ret==value1)||(ret==replace2))); + assert(((val==value1)||(val==replace2))); } /* free dictionary */ dict_free(dict); - /* TODO: test dict_values_first() and dict_values_next() */ - return 0; } |