]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/iniparser/dictionary.c
Bump libatalk version info
[netatalk.git] / libatalk / iniparser / dictionary.c
index b7c9ebf148b6724f7571daac8a61e99c549308c0..191db576d52e95ece30eb1de5312dd71442073bc 100644 (file)
@@ -3,7 +3,6 @@
    @file       dictionary.c
    @author     N. Devillard
    @date       Sep 2007
-   @version    $Revision: 1.27 $
    @brief      Implements a dictionary for string variables.
 
    This module implements a simple dictionary object, i.e. a list
 /*--------------------------------------------------------------------------*/
 
 /*
-       $Id: dictionary.c,v 1.27 2007-11-23 21:39:18 ndevilla Exp $
-       $Revision: 1.27 $
 */
 /*---------------------------------------------------------------------------
                                                                Includes
  ---------------------------------------------------------------------------*/
-#include "dictionary.h"
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif /* HAVE_CONFIG_H */
+
+#include <atalk/dictionary.h>
+#include <atalk/compat.h>
 
 #include <stdio.h>
 #include <stdlib.h>
                                                        Private functions
  ---------------------------------------------------------------------------*/
 
+#define MAXKEYSIZE 1024
+static char *makekey(const char *section, const char *entry)
+{
+    static char buf[MAXKEYSIZE];
+
+    strlcpy(buf, section, MAXKEYSIZE);
+    if (entry) {
+        strlcat(buf, ":", MAXKEYSIZE);
+        strlcat(buf, entry, MAXKEYSIZE);
+    }
+
+    return buf;
+}
+
 /* Doubles the allocated size associated to a pointer */
 /* 'size' is the current allocated size. */
 static void * mem_double(void * ptr, int size)
@@ -91,7 +107,7 @@ static char * xstrdup(char * s)
   by comparing the key itself in last resort.
  */
 /*--------------------------------------------------------------------------*/
-unsigned dictionary_hash(char * key)
+unsigned atalkdict_hash(char * key)
 {
        int                     len ;
        unsigned        hash ;
@@ -120,7 +136,7 @@ unsigned dictionary_hash(char * key)
   dictionary, give size=0.
  */
 /*--------------------------------------------------------------------------*/
-dictionary * dictionary_new(int size)
+dictionary * atalkdict_new(int size)
 {
        dictionary      *       d ;
 
@@ -146,7 +162,7 @@ dictionary * dictionary_new(int size)
   Deallocate a dictionary object and all memory associated to it.
  */
 /*--------------------------------------------------------------------------*/
-void dictionary_del(dictionary * d)
+void atalkdict_del(dictionary * d)
 {
        int             i ;
 
@@ -178,19 +194,19 @@ void dictionary_del(dictionary * d)
   dictionary object, you should not try to free it or modify it.
  */
 /*--------------------------------------------------------------------------*/
-char * dictionary_get(dictionary * d, char * key, char * def)
+const char * atalkdict_get(const dictionary * d, const char *section, const char * key, const char * def)
 {
        unsigned        hash ;
        int                     i ;
 
-       hash = dictionary_hash(key);
+       hash = atalkdict_hash(makekey(section, key));
        for (i=0 ; i<d->size ; i++) {
         if (d->key[i]==NULL)
             continue ;
         /* Compare hash */
                if (hash==d->hash[i]) {
             /* Compare string, to avoid hash collisions */
-            if (!strcmp(key, d->key[i])) {
+            if (!strcmp(makekey(section, key), d->key[i])) {
                                return d->val[i] ;
                        }
                }
@@ -214,8 +230,8 @@ char * dictionary_get(dictionary * d, char * key, char * def)
   or the key are considered as errors: the function will return immediately
   in such a case.
 
-  Notice that if you dictionary_set a variable to NULL, a call to
-  dictionary_get will return a NULL value: the variable will be found, and
+  Notice that if you atalkdict_set a variable to NULL, a call to
+  atalkdict_get will return a NULL value: the variable will be found, and
   its value (NULL) is returned. In other words, setting the variable
   content to NULL is equivalent to deleting the variable from the
   dictionary. It is not possible (in this implementation) to have a key in
@@ -224,22 +240,22 @@ char * dictionary_get(dictionary * d, char * key, char * def)
   This function returns non-zero in case of failure.
  */
 /*--------------------------------------------------------------------------*/
-int dictionary_set(dictionary * d, char * key, char * val)
+int atalkdict_set(dictionary * d, char *section, char * key, char * val)
 {
        int                     i ;
        unsigned        hash ;
 
-       if (d==NULL || key==NULL) return -1 ;
+       if (d==NULL || section==NULL) return -1 ;
        
        /* Compute hash for this key */
-       hash = dictionary_hash(key) ;
+       hash = atalkdict_hash(makekey(section, key));
        /* Find if value is already in dictionary */
        if (d->n>0) {
                for (i=0 ; i<d->size ; i++) {
             if (d->key[i]==NULL)
                 continue ;
                        if (hash==d->hash[i]) { /* Same hash value */
-                               if (!strcmp(key, d->key[i])) {   /* Same key */
+                               if (!strcmp(makekey(section, key), d->key[i])) {         /* Same key */
                                        /* Found a value: modify and return */
                                        if (d->val[i]!=NULL)
                                                free(d->val[i]);
@@ -274,7 +290,7 @@ int dictionary_set(dictionary * d, char * key, char * val)
         }
     }
        /* Copy key */
-       d->key[i]  = xstrdup(key);
+       d->key[i]  = xstrdup(makekey(section, key));
     d->val[i]  = val ? xstrdup(val) : NULL ;
        d->hash[i] = hash;
        d->n ++ ;
@@ -292,7 +308,7 @@ int dictionary_set(dictionary * d, char * key, char * val)
   key cannot be found.
  */
 /*--------------------------------------------------------------------------*/
-void dictionary_unset(dictionary * d, char * key)
+void atalkdict_unset(dictionary * d, char *section, char * key)
 {
        unsigned        hash ;
        int                     i ;
@@ -301,14 +317,14 @@ void dictionary_unset(dictionary * d, char * key)
                return;
        }
 
-       hash = dictionary_hash(key);
+       hash = atalkdict_hash(makekey(section, key));
        for (i=0 ; i<d->size ; i++) {
         if (d->key[i]==NULL)
             continue ;
         /* Compare hash */
                if (hash==d->hash[i]) {
             /* Compare string, to avoid hash collisions */
-            if (!strcmp(key, d->key[i])) {
+            if (!strcmp(makekey(section, key), d->key[i])) {
                 /* Found key */
                 break ;
                        }
@@ -341,7 +357,7 @@ void dictionary_unset(dictionary * d, char * key)
   output file pointers.
  */
 /*--------------------------------------------------------------------------*/
-void dictionary_dump(dictionary * d, FILE * out)
+void atalkdict_dump(dictionary * d, FILE * out)
 {
        int             i ;
 
@@ -359,47 +375,3 @@ void dictionary_dump(dictionary * d, FILE * out)
        }
        return ;
 }
-
-
-/* Test code */
-#ifdef TESTDIC
-#define NVALS 20000
-int main(int argc, char *argv[])
-{
-       dictionary      *       d ;
-       char    *       val ;
-       int                     i ;
-       char            cval[90] ;
-
-       /* Allocate dictionary */
-       printf("allocating...\n");
-       d = dictionary_new(0);
-       
-       /* Set values in dictionary */
-       printf("setting %d values...\n", NVALS);
-       for (i=0 ; i<NVALS ; i++) {
-               sprintf(cval, "%04d", i);
-               dictionary_set(d, cval, "salut");
-       }
-       printf("getting %d values...\n", NVALS);
-       for (i=0 ; i<NVALS ; i++) {
-               sprintf(cval, "%04d", i);
-               val = dictionary_get(d, cval, DICT_INVALID_KEY);
-               if (val==DICT_INVALID_KEY) {
-                       printf("cannot get value for key [%s]\n", cval);
-               }
-       }
-    printf("unsetting %d values...\n", NVALS);
-       for (i=0 ; i<NVALS ; i++) {
-               sprintf(cval, "%04d", i);
-               dictionary_unset(d, cval);
-       }
-    if (d->n != 0) {
-        printf("error deleting values\n");
-    }
-       printf("deallocating...\n");
-       dictionary_del(d);
-       return 0 ;
-}
-#endif
-/* vim: set ts=4 et sw=4 tw=75 */