From ba087e49b7b7e8f644b9fd59e4ea330d8566bb3d Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Sun, 22 Jul 2007 03:56:49 +0200 Subject: [PATCH] Added strpair function, analogous to the wcspair ones. --- common/utils.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- include/utils.h | 8 ++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/common/utils.c b/common/utils.c index d7ae752..6bc58ba 100644 --- a/common/utils.c +++ b/common/utils.c @@ -801,6 +801,51 @@ char *findfile(char *name, char *homedir, int filldef) } } +struct strpair *newstrpair(char *key, char *val, struct strpair **list) +{ + struct strpair *pair; + + pair = smalloc(sizeof(*pair)); + memset(pair, 0, sizeof(*pair)); + if(key != NULL) + pair->key = sstrdup(key); + if(val != NULL) + pair->val = sstrdup(val); + if(list != NULL) + { + pair->next = *list; + *list = pair; + } + return(pair); +} + +void freestrpair(struct strpair *pair, struct strpair **list) +{ + struct strpair *cur; + + for(cur = *list; cur != NULL; list = &(cur->next), cur = cur->next) + { + if(cur == pair) + { + *list = cur->next; + break; + } + } + free(pair->key); + free(pair->val); + free(pair); +} + +char *spfind(struct strpair *list, char *key) +{ + for(; list != NULL; list = list->next) + { + if(!strcmp(list->key, key)) + return(list->val); + } + return(NULL); +} + struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list) { struct wcspair *pair; @@ -811,10 +856,8 @@ struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list) pair->key = swcsdup(key); if(val != NULL) pair->val = swcsdup(val); - if(list == NULL) + if(list != NULL) { - pair->next = NULL; - } else { pair->next = *list; *list = pair; } diff --git a/include/utils.h b/include/utils.h index d09f172..cc62597 100644 --- a/include/utils.h +++ b/include/utils.h @@ -31,6 +31,11 @@ struct wcspair { wchar_t *val; }; +struct strpair { + struct strpair *next; + char *key; + char *val; +}; /* "Safe" functions */ #ifdef DAEMON @@ -101,6 +106,9 @@ char *base32decode(char *data, size_t *datalen); void _freeparr(void **arr); int _parrlen(void **arr); char *findfile(char *name, char *homedir, int filldef); +struct strpair *newstrpair(char *key, char *val, struct strpair **list); +void freestrpair(struct strpair *pair, struct strpair **list); +char *spfind(struct strpair *list, char *key); struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list); void freewcspair(struct wcspair *pair, struct wcspair **list); wchar_t *wpfind(struct wcspair *list, wchar_t *key); -- 2.11.0