From 6fe9cae62abb6a5fca55914b0f8f187c9f556645 Mon Sep 17 00:00:00 2001 From: Dmitry Frank Date: Sun, 27 Aug 2017 13:43:17 +0100 Subject: [PATCH] Add mg_next_comma_list_entry_n() And mgos_conf_check_access_n() PUBLISHED_FROM=c7de937b4f5463ca463d604183a4ee831617bd6d --- mongoose.c | 27 +++++++++++++++++++-------- mongoose.h | 2 ++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/mongoose.c b/mongoose.c index 1b54cf81..510fee0e 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1926,19 +1926,30 @@ const char *mg_next_comma_list_entry(const char *, struct mg_str *, struct mg_str *) WEAK; const char *mg_next_comma_list_entry(const char *list, struct mg_str *val, struct mg_str *eq_val) { - if (list == NULL || *list == '\0') { + struct mg_str ret = mg_next_comma_list_entry_n(mg_mk_str(list), val, eq_val); + return ret.p; +} + +struct mg_str mg_next_comma_list_entry_n(struct mg_str list, struct mg_str *val, + struct mg_str *eq_val) WEAK; +struct mg_str mg_next_comma_list_entry_n(struct mg_str list, struct mg_str *val, + struct mg_str *eq_val) { + if (list.len == 0) { /* End of the list */ - list = NULL; + list = mg_mk_str(NULL); } else { - val->p = list; - if ((list = strchr(val->p, ',')) != NULL) { + const char *chr = NULL; + *val = list; + + if ((chr = mg_strchr(*val, ',')) != NULL) { /* Comma found. Store length and shift the list ptr */ - val->len = list - val->p; - list++; + val->len = chr - val->p; + chr++; + list.len -= (chr - list.p); + list.p = chr; } else { /* This value is the last one */ - list = val->p + strlen(val->p); - val->len = list - val->p; + list = mg_mk_str_n(list.p + list.len, 0); } if (eq_val != NULL) { diff --git a/mongoose.h b/mongoose.h index daae6ec0..5e431391 100644 --- a/mongoose.h +++ b/mongoose.h @@ -2104,6 +2104,8 @@ int mg_avprintf(char **buf, size_t size, const char *fmt, va_list ap); */ const char *mg_next_comma_list_entry(const char *list, struct mg_str *val, struct mg_str *eq_val); +struct mg_str mg_next_comma_list_entry_n(struct mg_str list, struct mg_str *val, + struct mg_str *eq_val); /* * Matches 0-terminated string (mg_match_prefix) or string with given length