From 00b019b8902e9d319d855b9156f97f931923331c Mon Sep 17 00:00:00 2001 From: default Date: Sat, 9 Mar 2024 08:32:20 +0100 Subject: [PATCH] Backport from xs. --- xs.h | 44 ++++++++++++++++++++++++++++++++++++++++---- xs_curl.h | 5 ++--- xs_fcgi.h | 5 ++--- xs_httpd.h | 5 ++--- xs_json.h | 4 +++- xs_version.h | 2 +- 6 files changed, 50 insertions(+), 15 deletions(-) diff --git a/xs.h b/xs.h index 7df7346..afd8245 100644 --- a/xs.h +++ b/xs.h @@ -109,6 +109,7 @@ xs_dict *xs_dict_append_m(xs_dict *dict, const xs_str *key, const xs_val *mem, i xs_dict *xs_dict_prepend_m(xs_dict *dict, const xs_str *key, const xs_val *mem, int dsz); #define xs_dict_prepend(dict, key, data) xs_dict_prepend_m(dict, key, data, xs_size(data)) int xs_dict_iter(xs_dict **dict, xs_str **key, xs_val **value); +int xs_dict_next(const xs_dict *dict, xs_str **key, xs_val **value, int *ctxt); xs_val *xs_dict_get_def(const xs_dict *dict, const xs_str *key, const xs_val *def); #define xs_dict_get(dict, key) xs_dict_get_def(dict, key, NULL) xs_dict *xs_dict_del(xs_dict *dict, const xs_str *key); @@ -1024,17 +1025,52 @@ int xs_dict_iter(xs_dict **dict, xs_str **key, xs_val **value) } +int xs_dict_next(const xs_dict *dict, xs_str **key, xs_val **value, int *ctxt) +/* iterates a dict, with context */ +{ + int goon = 1; + + char *p = (char *)dict; + + /* skip the start of the list */ + if (*ctxt == 0) + *ctxt = 1 + _XS_TYPE_SIZE; + + p += *ctxt; + + /* an element? */ + if (xs_type(p) == XSTYPE_DITEM) { + p++; + + *key = p; + p += xs_size(*key); + + *value = p; + p += xs_size(*value); + } + else { + /* end of list */ + goon = 0; + } + + /* store back the pointer */ + *ctxt = p - dict; + + return goon; +} + + xs_val *xs_dict_get_def(const xs_dict *dict, const xs_str *key, const xs_val *def) /* returns the value directed by key, or the default value */ { XS_ASSERT_TYPE(dict, XSTYPE_DICT); XS_ASSERT_TYPE(key, XSTYPE_STRING); - xs_dict *p = (xs_dict *)dict; xs_str *k; xs_val *v; + int c = 0; - while (xs_dict_iter(&p, &k, &v)) { + while (xs_dict_next(dict, &k, &v, &c)) { if (strcmp(k, key) == 0) return v; } @@ -1051,9 +1087,9 @@ xs_dict *xs_dict_del(xs_dict *dict, const xs_str *key) xs_str *k; xs_val *v; - xs_dict *p = dict; + int c = 0; - while (xs_dict_iter(&p, &k, &v)) { + while (xs_dict_next(dict, &k, &v, &c)) { if (strcmp(k, key) == 0) { /* the address of the item is just behind the key */ char *i = k - 1; diff --git a/xs_curl.h b/xs_curl.h index b08d902..f7783b9 100644 --- a/xs_curl.h +++ b/xs_curl.h @@ -93,7 +93,6 @@ xs_dict *xs_http_request(const char *method, const char *url, xs_dict *response; CURL *curl; struct curl_slist *list = NULL; - xs_dict *p; xs_str *k; xs_val *v; long lstatus = 0; @@ -147,8 +146,8 @@ xs_dict *xs_http_request(const char *method, const char *url, } /* fill the request headers */ - p = (xs_dict *)headers; - while (xs_dict_iter(&p, &k, &v)) { + int c = 0; + while (xs_dict_next(headers, &k, &v, &c)) { xs *h = xs_fmt("%s: %s", k, v); list = curl_slist_append(list, h); diff --git a/xs_fcgi.h b/xs_fcgi.h index 3bf21ee..4727c5c 100644 --- a/xs_fcgi.h +++ b/xs_fcgi.h @@ -293,7 +293,6 @@ void xs_fcgi_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b struct fcgi_record_header hdr = {0}; struct fcgi_end_request ereq = {0}; xs *out = xs_str_new(NULL); - xs_dict *p; xs_str *k; xs_str *v; @@ -307,8 +306,8 @@ void xs_fcgi_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b out = xs_str_cat(out, s1); } - p = headers; - while (xs_dict_iter(&p, &k, &v)) { + int c = 0; + while (xs_dict_next(headers, &k, &v, &c)) { xs *s1 = xs_fmt("%s: %s\r\n", k, v); out = xs_str_cat(out, s1); } diff --git a/xs_httpd.h b/xs_httpd.h index b7f614c..4d006d7 100644 --- a/xs_httpd.h +++ b/xs_httpd.h @@ -98,15 +98,14 @@ void xs_httpd_response(FILE *f, int status, xs_dict *headers, xs_str *body, int /* sends an httpd response */ { xs *proto; - xs_dict *p; xs_str *k; xs_val *v; proto = xs_fmt("HTTP/1.1 %d %s", status, status / 100 == 2 ? "OK" : "ERROR"); fprintf(f, "%s\r\n", proto); - p = headers; - while (xs_dict_iter(&p, &k, &v)) { + int c = 0; + while (xs_dict_next(headers, &k, &v, &c)) { fprintf(f, "%s: %s\r\n", k, v); } diff --git a/xs_json.h b/xs_json.h index e7f275d..d656b15 100644 --- a/xs_json.h +++ b/xs_json.h @@ -115,7 +115,9 @@ static void _xs_json_dump(const xs_val *s_data, int level, int indent, FILE *f) fputc('{', f); xs_str *k; - while (xs_dict_iter(&data, &k, &v)) { + int ct = 0; + + while (xs_dict_next(s_data, &k, &v, &ct)) { if (c != 0) fputc(',', f); diff --git a/xs_version.h b/xs_version.h index 3e76a99..50dcb5e 100644 --- a/xs_version.h +++ b/xs_version.h @@ -1 +1 @@ -/* 73ff6e75bec88fa0b908b039462180a8ac1401de 2024-03-08T07:17:30+01:00 */ +/* f46d5b29627b20a6e9ec4ef60c01df1d2d778520 2024-03-09T08:26:31+01:00 */