diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2009-12-13 11:27:33 +0100 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2009-12-13 11:27:33 +0100 |
commit | ef8cc767b201e4798a060282d4a6f280094bb8cc (patch) | |
tree | 4f19eae6870321f280959be4b7b24cd0e38d4c2a /common | |
parent | 7dd703c9af5f8b4a50f056c47588a9e51d4ea681 (diff) |
change dict and set API to perform loops with a list of strings instead of loop_first() and loop_next() functions
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-pam-ldapd@1028 ef36b2f9-881f-0410-afb5-c4e39611909c
Diffstat (limited to 'common')
-rw-r--r-- | common/dict.c | 69 | ||||
-rw-r--r-- | common/dict.h | 16 | ||||
-rw-r--r-- | common/set.c | 20 | ||||
-rw-r--r-- | common/set.h | 14 |
4 files changed, 59 insertions, 60 deletions
diff --git a/common/dict.c b/common/dict.c index a0f9f35..128321d 100644 --- a/common/dict.c +++ b/common/dict.c @@ -2,7 +2,7 @@ dict.c - dictionary functions This file is part of the nss-pam-ldapd library. - Copyright (C) 2007, 2008 Arthur de Jong + Copyright (C) 2007, 2008, 2009 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,9 +46,6 @@ All the keys are copied in a separate linked list of buffers where each new buffer that is allocated is larger than the previous one. The first buffer in the linked list is always the current one. - - Note that the initial sizes of hashtable and the loadfactor still need to - be tuned to the use in this application. */ /* an entry stores one key/value pair */ @@ -70,8 +67,6 @@ struct dictionary { int size; /* size of the hashtable */ int num; /* total number of keys stored */ struct dict_entry **table; /* the hashtable */ - int loop_idx; /* for looping */ - struct dict_entry *loop_entry; /* for looping */ }; /* Simple hash function that computes the hash value of a lower-cased @@ -243,27 +238,47 @@ int dict_put(DICT *dict,const char *key,void *value) return 0; } -void dict_loop_first(DICT *dict) -{ - dict->loop_idx=0; - dict->loop_entry=NULL; -} - -const char *dict_loop_next(DICT *dict,const char **key,void **value) +const char **dict_keys(DICT *dict) { + int i; struct dict_entry *entry; - /* find non-empty entry */ - while ( (dict->loop_idx<dict->size) && (dict->loop_entry==NULL) ) - dict->loop_entry=dict->table[dict->loop_idx++]; - if (dict->loop_entry==NULL) - return NULL; /* no more entries to check */ - /* save current result and go to next entry */ - entry=dict->loop_entry; - dict->loop_entry=entry->next; - /* return results */ - if (key!=NULL) - *key=entry->key; - if (value!=NULL) - *value=entry->value; - return entry->key; + char *buf; + const char **values; + size_t sz; + int num; + /* figure out how much memory to allocate */ + num=0; + sz=0; + for (i=0;i<dict->size;i++) + { + entry=dict->table[i]; + while (entry!=NULL) + { + num++; + sz+=strlen(entry->key)+1; + entry=entry->next; + } + } + /* allocate the needed memory */ + buf=(char *)malloc((num+1)*sizeof(char *)+sz); + if (buf==NULL) + return NULL; + values=(const char **)(void *)buf; + buf+=(num+1)*sizeof(char *); + /* fill the array with the keys */ + num=0; + for (i=0;i<dict->size;i++) + { + entry=dict->table[i]; + while (entry!=NULL) + { + strcpy(buf,entry->key); + values[num++]=buf; + buf+=strlen(buf)+1; + entry=entry->next; + } + } + values[num]=NULL; + /* done */ + return values; } diff --git a/common/dict.h b/common/dict.h index ba9fd6a..3a6108f 100644 --- a/common/dict.h +++ b/common/dict.h @@ -2,7 +2,7 @@ dict.h - dictionary functions This file is part of the nss-pam-ldapd library. - Copyright (C) 2007, 2008 Arthur de Jong + Copyright (C) 2007, 2008, 2009 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 @@ -59,17 +59,9 @@ void *dict_get(DICT *dict,const char *key) of the caller. */ void dict_free(DICT *dict); -/* 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_loop_next(); */ -void dict_loop_first(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) +/* Return the keys of the dicta as a list of strings. + The caller should free the memory with a single call to free(). */ +const char **dict_keys(DICT *dict) MUST_USE; #endif /* _DICT_H */ diff --git a/common/set.c b/common/set.c index 7e82730..d36ce2d 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 Arthur de Jong + Copyright (C) 2008, 2009 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 @@ -30,6 +30,12 @@ #include "set.h" #include "dict.h" +/* + The SET object is just a DICT which is passed around. The value + for each entry in the dict is just the pointer to the dict. + Another API is provided to give it a more set-like interface. +*/ + SET *set_new(void) { return (SET *)dict_new(); @@ -50,15 +56,7 @@ void set_free(SET *set) dict_free((DICT *)set); } -void set_loop_first(SET *set) -{ - dict_loop_first((DICT *)set); -} - -const char *set_loop_next(SET *set) +const char **set_tolist(SET *set) { - const char *value=NULL; - if (dict_loop_next((DICT *)set,&value,NULL)==NULL) - return NULL; - return value; + return dict_keys((DICT *)set); } diff --git a/common/set.h b/common/set.h index e38b52e..111a2cc 100644 --- a/common/set.h +++ b/common/set.h @@ -2,7 +2,7 @@ set.h - set functions This file is part of the nss-pam-ldapd library. - Copyright (C) 2008 Arthur de Jong + Copyright (C) 2008, 2009 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 @@ -51,15 +51,9 @@ int set_contains(SET *set,const char *value) for the set and the values is freed. */ void set_free(SET *set); -/* Function for looping over all set values. - This resets the search to the beginning of the set. - This is required before calling set_loop_next(); */ -void set_loop_first(SET *set); - -/* Function for looping over all set values. - This returns a stored value. NULL is returned when all - values have been returned. */ -const char *set_loop_next(SET *set) +/* Return the content of the set as a list of strings. + The caller should free the memory with a single call to free(). */ +const char **set_tolist(SET *set) MUST_USE; #endif /* _SET_H */ |