diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2007-06-11 20:40:48 +0200 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2007-06-11 20:40:48 +0200 |
commit | 3ead49c38ca05e0b00cabdbda16bf7d34833656f (patch) | |
tree | 17905d7a86bdfaba47591c551236895930b0bdd0 /common | |
parent | 4c688ef3001dae3cbe870369ac0d8f8dfb2caa14 (diff) |
move dict into the common directory
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-ldapd@279 ef36b2f9-881f-0410-afb5-c4e39611909c
Diffstat (limited to 'common')
-rw-r--r-- | common/Makefile.am | 4 | ||||
-rw-r--r-- | common/dict.c | 151 | ||||
-rw-r--r-- | common/dict.h | 71 |
3 files changed, 225 insertions, 1 deletions
diff --git a/common/Makefile.am b/common/Makefile.am index c922af5..8821959 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -17,7 +17,9 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # 02110-1301 USA -noinst_LIBRARIES = libtio.a +noinst_LIBRARIES = libtio.a libdict.a libtio_a_SOURCES = tio.c tio.h libtio_a_CFLAGS = -fPIC + +libdict_a_SOURCES = dict.c dict.h diff --git a/common/dict.c b/common/dict.c new file mode 100644 index 0000000..84c30d7 --- /dev/null +++ b/common/dict.c @@ -0,0 +1,151 @@ +/* + dict.c - dictionary functions + This file is part of the nss-ldapd library. + + Copyright (C) 2007 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 + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA +*/ + +#include "config.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <strings.h> + +#include "dict.h" + +struct dict_entry { + const char *key; + void *value; + struct dict_entry *next; +}; + +struct dictionary { + struct dict_entry *head; + struct dict_entry *ptr; /* for searching */ +}; + +static struct dict_entry *dict_entry_new(const char *key) +{ + struct dict_entry *entry; + entry=(struct dict_entry *)malloc(sizeof(struct dict_entry)); + if (entry==NULL) + return NULL; + entry->key=strdup(key); + if (entry->key==NULL) + { + free(entry); + return NULL; + } + entry->value=NULL; + return entry; +} + +static void dict_entry_free(struct dict_entry *entry) +{ + /* free key */ + free((void *)entry->key); + /* free entry */ + free(entry); +} + +static struct dict_entry *dict_entry_find( + DICT *dict,const char *key) +{ + struct dict_entry *ptr; + for (ptr=dict->head;ptr!=NULL;ptr=ptr->next) + { + if (strcasecmp(ptr->key,key)==0) + return ptr; + } + return NULL; +} + +DICT *dict_new(void) +{ + struct dictionary *dict; + dict=(struct dictionary *)malloc(sizeof(struct dictionary)); + if (dict==NULL) + return NULL; + dict->head=NULL; + dict->ptr=NULL; + return dict; +} + +int dict_put(DICT *dict,const char *key,void *value) +{ + struct dict_entry *entry; + /* ignore setting of value to NULL */ + if (value==NULL) + return 0; /* probably do dict_del(dict,key) */ + entry=dict_entry_find(dict,key); + if (entry==NULL) + { + /* create new entry and insert it in the list */ + entry=dict_entry_new(key); + if (entry==NULL) + return -1; + /* insert entry in list */ + entry->next=dict->head; + dict->head=entry; + } + /* set value */ + entry->value=value; + return 0; +} + +void *dict_get(DICT *dict,const char *key) +{ + struct dict_entry *entry; + entry=dict_entry_find(dict,key); + if (entry==NULL) + return NULL; + return entry->value; +} + +void dict_free(DICT *dict) +{ + struct dict_entry *ptr,*nxt; + /* free all entries */ + ptr=dict->head; + while (ptr!=NULL) + { + nxt=ptr->next; + dict_entry_free(ptr); + ptr=nxt; + } + /* clear some references */ + dict->head=NULL; + dict->ptr=NULL; + /* free struct itself */ + free(dict); +} + +void dict_values_first(DICT *dict) +{ + dict->ptr=dict->head; +} + +void *dict_values_next(DICT *dict) +{ + struct dict_entry *ptr; + ptr=dict->ptr; + if (dict->ptr!=NULL) + dict->ptr=dict->ptr->next; + return ptr; +} diff --git a/common/dict.h b/common/dict.h new file mode 100644 index 0000000..104c5a7 --- /dev/null +++ b/common/dict.h @@ -0,0 +1,71 @@ +/* + dict.h - dictionary functions + This file is part of the nss-ldapd library. + + Copyright (C) 2007 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 + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA +*/ + +#ifndef _DICT_H +#define _DICT_H + +#include "compat/attrs.h" + +/* + These functions provide a mapping between a case insensitive + string and a pointer. +*/ +typedef struct dictionary DICT; + +/* Create a new instance of a dictionary. Returns NULL + in case of memory allocation errors. */ +DICT *dict_new(void) + LIKE_MALLOC MUST_USE; + +/* Add a relation in the dictionary. The key is duplicated + and can be reused by the caller. The pointer is just stored. + This function returns non-0 in case of memory allocation + errors. If the key was previously in use the value + is replaced. All key comparisons are case insensitive. */ +int dict_put(DICT *dict,const char *key,void *value); + +/* Look up a key in the dictionary and return the associated + value. NULL is returned if the key is not found in the dictionary. + All key comparisons are case insensitive. */ +void *dict_get(DICT *dict,const char *key) + MUST_USE; + +/* Delete a key-value association from the dictionary. + All key comparisons are case insensitive. */ +/*void dict_del(LEGACYDICT *dict,const char *key);*/ + +/* Remove the dictionary from memory. All allocated storage + for the dictionary and the keys is freed. */ +void dict_free(DICT *dict); + +/* Function for looping over all dictionary 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); + +/* 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) + MUST_USE; + +#endif /* _DICT_H */ |