X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=netatalk.git;a=blobdiff_plain;f=libatalk%2Finiparser%2Fdictionary.c;h=6ba083583de8d3b6b7daf9c3ed1f71d3be0e5c92;hp=b7c9ebf148b6724f7571daac8a61e99c549308c0;hb=df7560dfdb12b06090dc4b2c6e88d0858930b591;hpb=8658ae6f5b6d37c769c722c84b1bc09408a34285 diff --git a/libatalk/iniparser/dictionary.c b/libatalk/iniparser/dictionary.c index b7c9ebf1..6ba08358 100644 --- a/libatalk/iniparser/dictionary.c +++ b/libatalk/iniparser/dictionary.c @@ -19,7 +19,8 @@ /*--------------------------------------------------------------------------- Includes ---------------------------------------------------------------------------*/ -#include "dictionary.h" +#include +#include #include #include @@ -39,6 +40,20 @@ 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) @@ -178,19 +193,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) +char * dictionary_get(dictionary * d, char *section, char * key, char * def) { unsigned hash ; int i ; - hash = dictionary_hash(key); + hash = dictionary_hash(makekey(section, key)); for (i=0 ; isize ; 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] ; } } @@ -224,7 +239,7 @@ 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 dictionary_set(dictionary * d, char *section, char * key, char * val) { int i ; unsigned hash ; @@ -232,14 +247,14 @@ int dictionary_set(dictionary * d, char * key, char * val) if (d==NULL || key==NULL) return -1 ; /* Compute hash for this key */ - hash = dictionary_hash(key) ; + hash = dictionary_hash(makekey(section, key)); /* Find if value is already in dictionary */ if (d->n>0) { for (i=0 ; isize ; 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 +289,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 +307,7 @@ int dictionary_set(dictionary * d, char * key, char * val) key cannot be found. */ /*--------------------------------------------------------------------------*/ -void dictionary_unset(dictionary * d, char * key) +void dictionary_unset(dictionary * d, char *section, char * key) { unsigned hash ; int i ; @@ -301,14 +316,14 @@ void dictionary_unset(dictionary * d, char * key) return; } - hash = dictionary_hash(key); + hash = dictionary_hash(makekey(section, key)); for (i=0 ; isize ; 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 ; } @@ -359,47 +374,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 ; in != 0) { - printf("error deleting values\n"); - } - printf("deallocating...\n"); - dictionary_del(d); - return 0 ; -} -#endif -/* vim: set ts=4 et sw=4 tw=75 */