diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2010-10-15 12:31:11 +0200 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2010-10-15 12:31:11 +0200 |
commit | f6bbbc5da9d72ec6637ef8fea58c73686abdace1 (patch) | |
tree | 523ca9a9ab64a3d5bd3623b51ad2bf50a556faf3 /common | |
parent | e6100f8a81d178f1307571a5773931dd976986c2 (diff) |
implement dict_getany() and set_pop() functions to be able to pick and remove entries
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-pam-ldapd@1279 ef36b2f9-881f-0410-afb5-c4e39611909c
Diffstat (limited to 'common')
-rw-r--r-- | common/dict.c | 11 | ||||
-rw-r--r-- | common/dict.h | 5 | ||||
-rw-r--r-- | common/set.c | 15 | ||||
-rw-r--r-- | common/set.h | 5 |
4 files changed, 35 insertions, 1 deletions
diff --git a/common/dict.c b/common/dict.c index a5068e9..c42b8b4 100644 --- a/common/dict.c +++ b/common/dict.c @@ -176,6 +176,17 @@ void *dict_get(DICT *dict,const char *key) return NULL; } +const char *dict_getany(DICT *dict) +{ + int i; + /* loop over the linked list in the hashtable */ + for (i=0;i<dict->size;i++) + if (dict->table[i]) + return dict->table[i]->key; + /* no matches found */ + return NULL; +} + int dict_put(DICT *dict,const char *key,void *value) { uint32_t hash; diff --git a/common/dict.h b/common/dict.h index c4f94ae..bb244f0 100644 --- a/common/dict.h +++ b/common/dict.h @@ -48,6 +48,11 @@ int dict_put(DICT *dict,const char *key,void *value); void *dict_get(DICT *dict,const char *key) MUST_USE; +/* Get a key from the dictionary that has a value set. The caller does + not need to free the returned value (it is freed when dict_free() + is called). */ +const char *dict_getany(DICT *dict); + /* Delete a key-value association from the dictionary. All key comparisons are case sensitive. */ /*void dict_del(DICT *dict,const char *key);*/ diff --git a/common/set.c b/common/set.c index d36ce2d..aec7e83 100644 --- a/common/set.c +++ b/common/set.c @@ -2,7 +2,7 @@ set.c - set functions This file is part of the nss-pam-ldapd library. - Copyright (C) 2008, 2009 Arthur de Jong + Copyright (C) 2008, 2009, 2010 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 @@ -46,6 +46,19 @@ int set_add(SET *set,const char *value) return dict_put((DICT *)set,value,set); } +char *set_pop(SET *set) +{ + const char *key; + char *value; + key=dict_getany((DICT *)set); + if (key==NULL) + return NULL; /* no more entries in set */ + /* remove the entry from the dict and return a copy */ + value=strdup(key); + dict_put((DICT *)set,key,NULL); + return value; +} + int set_contains(SET *set,const char *value) { return dict_get((DICT *)set,value)!=NULL; diff --git a/common/set.h b/common/set.h index ce5c843..d57c3db 100644 --- a/common/set.h +++ b/common/set.h @@ -47,6 +47,11 @@ int set_add(SET *set,const char *value); int set_contains(SET *set,const char *value) MUST_USE; +/* Get an element from the set and removes it from the set. + Returns NULL on an empty set. A copy of the string in the set + is returned, the caller should use free() to free it. */ +char *set_pop(SET *set); + /* Remove the set from memory. All allocated storage for the set and the values is freed. */ void set_free(SET *set); |