From b43df60d7f6918f52a461d43de9c31689c4e77ce Mon Sep 17 00:00:00 2001 From: Roberto De Ioris Date: Sat, 27 Oct 2012 14:58:19 +0200 Subject: [PATCH] added uwsgi_kvlist_parse api function --- core/utils.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ uwsgi.h | 2 ++ 2 files changed, 76 insertions(+) diff --git a/core/utils.c b/core/utils.c index 1f48eabe..33bfdb35 100644 --- a/core/utils.c +++ b/core/utils.c @@ -4813,3 +4813,77 @@ char *uwsgi_matheval_str(char *expr) { return uwsgi_num2str((int) ret); } #endif + +int uwsgi_kvlist_parse(char *src, size_t len, char list_separator, char kv_separator, ...) { + size_t i; + va_list ap; + struct uwsgi_string_list *itemlist = NULL; + + char *buf = uwsgi_calloc(len + 1); + + // ok let's start splitting the string + int escaped = 0; + char *base = buf; + char *ptr = buf; + for(i=0;i base) { + uwsgi_string_new_list(&itemlist, base); + } + + struct uwsgi_string_list *usl = itemlist; + while(usl) { + len = strlen(usl->value); + char *item_buf = uwsgi_calloc(len + 1); + base = item_buf; + ptr = item_buf; + escaped = 0; + for(i=0;ivalue[i] == kv_separator && !escaped) { + *ptr++= 0; + va_start(ap, kv_separator); + for(;;) { + char *p = va_arg(ap, char *); + if (!p) break; + char **pp = va_arg(ap, char **); + if (!pp) break; + if (!strcmp(p, base)) { + *pp = uwsgi_str(usl->value+i+1); + } + } + va_end(ap); + base = ptr; + break; + } + else if (usl->value[i] == '\\' && !escaped) { + escaped = 1; + } + else if (escaped) { + escaped = 0; + } + else { + *ptr++= usl->value[i]; + } + } + free(item_buf); + usl = usl->next; + } + + free(buf); + return 0; +} diff --git a/uwsgi.h b/uwsgi.h index 0a30dea3..d32c375b 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -3385,6 +3385,8 @@ double uwsgi_matheval(char *); char *uwsgi_matheval_str(char *); #endif +int uwsgi_kvlist_parse(char *, size_t, char, char, ...); + void uwsgi_check_emperor(void); #ifdef UWSGI_AS_SHARED_LIBRARY int uwsgi_init(int, char **, char **);