lists.arthurdejong.org
RSS feed

nss-pam-ldapd commit: r1279 - in nss-pam-ldapd: common tests

[Date Prev][Date Next] [Thread Prev][Thread Next]

nss-pam-ldapd commit: r1279 - in nss-pam-ldapd: common tests



Author: arthur
Date: Fri Oct 15 12:31:11 2010
New Revision: 1279
URL: http://arthurdejong.org/viewvc/nss-pam-ldapd?view=rev&revision=1279

Log:
implement dict_getany() and set_pop() functions to be able to pick and remove 
entries

Modified:
   nss-pam-ldapd/common/dict.c
   nss-pam-ldapd/common/dict.h
   nss-pam-ldapd/common/set.c
   nss-pam-ldapd/common/set.h
   nss-pam-ldapd/tests/test_set.c

Modified: nss-pam-ldapd/common/dict.c
==============================================================================
--- nss-pam-ldapd/common/dict.c Fri Oct 15 12:21:35 2010        (r1278)
+++ nss-pam-ldapd/common/dict.c Fri Oct 15 12:31:11 2010        (r1279)
@@ -176,6 +176,17 @@
   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;

Modified: nss-pam-ldapd/common/dict.h
==============================================================================
--- nss-pam-ldapd/common/dict.h Fri Oct 15 12:21:35 2010        (r1278)
+++ nss-pam-ldapd/common/dict.h Fri Oct 15 12:31:11 2010        (r1279)
@@ -48,6 +48,11 @@
 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);*/

Modified: nss-pam-ldapd/common/set.c
==============================================================================
--- nss-pam-ldapd/common/set.c  Fri Oct 15 12:21:35 2010        (r1278)
+++ nss-pam-ldapd/common/set.c  Fri Oct 15 12:31:11 2010        (r1279)
@@ -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 @@
   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;

Modified: nss-pam-ldapd/common/set.h
==============================================================================
--- nss-pam-ldapd/common/set.h  Fri Oct 15 12:21:35 2010        (r1278)
+++ nss-pam-ldapd/common/set.h  Fri Oct 15 12:31:11 2010        (r1279)
@@ -47,6 +47,11 @@
 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);

Modified: nss-pam-ldapd/tests/test_set.c
==============================================================================
--- nss-pam-ldapd/tests/test_set.c      Fri Oct 15 12:21:35 2010        (r1278)
+++ nss-pam-ldapd/tests/test_set.c      Fri Oct 15 12:31:11 2010        (r1279)
@@ -30,6 +30,14 @@
 #include "common/set.h"
 #include "compat/attrs.h"
 
+static int isknownvalue(const char *value)
+{
+  return value!=NULL && (
+          (strcmp(value,"key1")==0) ||
+          (strcmp(value,"key2")==0) ||
+          (strcmp(value,"key3")==0) );
+}
+
 /* the main program... */
 int main(int UNUSED(argc),char UNUSED(*argv[]))
 {
@@ -57,11 +65,15 @@
   list=set_tolist(set);
   for (i=0;list[i]!=NULL;i++)
   {
-    assert( (strcmp(list[i],"key1")==0) ||
-            (strcmp(list[i],"key2")==0) ||
-            (strcmp(list[i],"key3")==0) );
+    assert(isknownvalue(list[i]));
   }
 
+  /* remove keys from the set */
+  assert(isknownvalue(set_pop(set)));
+  assert(isknownvalue(set_pop(set)));
+  assert(isknownvalue(set_pop(set)));
+  assert(set_pop(set)==NULL);
+
   /* free set */
   set_free(set);
   free(list);
--
To unsubscribe send an email to
nss-pam-ldapd-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/nss-pam-ldapd-commits