From 3ead49c38ca05e0b00cabdbda16bf7d34833656f Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Mon, 11 Jun 2007 18:40:48 +0000 Subject: move dict into the common directory git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-ldapd@279 ef36b2f9-881f-0410-afb5-c4e39611909c --- common/Makefile.am | 4 +- common/dict.c | 151 +++++++++++++++++++++++++++++++++++++++++++++++++ common/dict.h | 71 +++++++++++++++++++++++ nslcd/Makefile.am | 3 +- nslcd/cfg.h | 2 +- nslcd/dict.c | 151 ------------------------------------------------- nslcd/dict.h | 71 ----------------------- tests/dict/Makefile.am | 4 +- tests/dict/test_dict.c | 2 +- 9 files changed, 230 insertions(+), 229 deletions(-) create mode 100644 common/dict.c create mode 100644 common/dict.h delete mode 100644 nslcd/dict.c delete mode 100644 nslcd/dict.h 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 +#include +#include +#include + +#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 */ diff --git a/nslcd/Makefile.am b/nslcd/Makefile.am index 201ff0b..5d0cd44 100644 --- a/nslcd/Makefile.am +++ b/nslcd/Makefile.am @@ -31,7 +31,6 @@ nslcd_SOURCES = nslcd.c ../nslcd.h ../nslcd-common.h \ pagectrl.c pagectrl.h \ util.c util.h \ cfg.c cfg.h \ - dict.c dict.h \ alias.c ether.c group.c host.c netgroup.c network.c \ passwd.c protocol.c rpc.c service.c shadow.c -nslcd_LDADD = @nslcd_LIBS@ ../common/libtio.a +nslcd_LDADD = @nslcd_LIBS@ ../common/libtio.a ../common/libdict.a diff --git a/nslcd/cfg.h b/nslcd/cfg.h index b9009f9..67a6f98 100644 --- a/nslcd/cfg.h +++ b/nslcd/cfg.h @@ -27,7 +27,7 @@ #define _CFG_H #include "ldap-nss.h" -#include "dict.h" +#include "common/dict.h" #include "compat/attrs.h" /* maximum number of URIs */ diff --git a/nslcd/dict.c b/nslcd/dict.c deleted file mode 100644 index 84c30d7..0000000 --- a/nslcd/dict.c +++ /dev/null @@ -1,151 +0,0 @@ -/* - 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 -#include -#include -#include - -#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/nslcd/dict.h b/nslcd/dict.h deleted file mode 100644 index 104c5a7..0000000 --- a/nslcd/dict.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - 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 */ diff --git a/tests/dict/Makefile.am b/tests/dict/Makefile.am index 0d67814..3e42007 100644 --- a/tests/dict/Makefile.am +++ b/tests/dict/Makefile.am @@ -19,5 +19,5 @@ noinst_PROGRAMS = test_dict -test_dict_SOURCES = test_dict.c ../../nslcd/dict.h -test_dict_LDADD = ../../nslcd/dict.o +test_dict_SOURCES = test_dict.c ../../common/dict.h +test_dict_LDADD = ../../common/dict.o diff --git a/tests/dict/test_dict.c b/tests/dict/test_dict.c index e89aa63..0524a1b 100644 --- a/tests/dict/test_dict.c +++ b/tests/dict/test_dict.c @@ -26,7 +26,7 @@ #include #include -#include "../../nslcd/dict.h" +#include "common/dict.h" #include "compat/attrs.h" /* the main program... */ -- cgit v1.2.3