summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar default2024-03-09 08:32:20 +0100
committerGravatar default2024-03-09 08:32:20 +0100
commit00b019b8902e9d319d855b9156f97f931923331c (patch)
tree3d17736c3884f119b3e223947bea31b6f5953ce1
parentBackport from xs. (diff)
downloadpenes-snac2-00b019b8902e9d319d855b9156f97f931923331c.tar.gz
penes-snac2-00b019b8902e9d319d855b9156f97f931923331c.tar.xz
penes-snac2-00b019b8902e9d319d855b9156f97f931923331c.zip
Backport from xs.
Diffstat (limited to '')
-rw-r--r--xs.h44
-rw-r--r--xs_curl.h5
-rw-r--r--xs_fcgi.h5
-rw-r--r--xs_httpd.h5
-rw-r--r--xs_json.h4
-rw-r--r--xs_version.h2
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
109xs_dict *xs_dict_prepend_m(xs_dict *dict, const xs_str *key, const xs_val *mem, int dsz); 109xs_dict *xs_dict_prepend_m(xs_dict *dict, const xs_str *key, const xs_val *mem, int dsz);
110#define xs_dict_prepend(dict, key, data) xs_dict_prepend_m(dict, key, data, xs_size(data)) 110#define xs_dict_prepend(dict, key, data) xs_dict_prepend_m(dict, key, data, xs_size(data))
111int xs_dict_iter(xs_dict **dict, xs_str **key, xs_val **value); 111int xs_dict_iter(xs_dict **dict, xs_str **key, xs_val **value);
112int xs_dict_next(const xs_dict *dict, xs_str **key, xs_val **value, int *ctxt);
112xs_val *xs_dict_get_def(const xs_dict *dict, const xs_str *key, const xs_val *def); 113xs_val *xs_dict_get_def(const xs_dict *dict, const xs_str *key, const xs_val *def);
113#define xs_dict_get(dict, key) xs_dict_get_def(dict, key, NULL) 114#define xs_dict_get(dict, key) xs_dict_get_def(dict, key, NULL)
114xs_dict *xs_dict_del(xs_dict *dict, const xs_str *key); 115xs_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)
1024} 1025}
1025 1026
1026 1027
1028int xs_dict_next(const xs_dict *dict, xs_str **key, xs_val **value, int *ctxt)
1029/* iterates a dict, with context */
1030{
1031 int goon = 1;
1032
1033 char *p = (char *)dict;
1034
1035 /* skip the start of the list */
1036 if (*ctxt == 0)
1037 *ctxt = 1 + _XS_TYPE_SIZE;
1038
1039 p += *ctxt;
1040
1041 /* an element? */
1042 if (xs_type(p) == XSTYPE_DITEM) {
1043 p++;
1044
1045 *key = p;
1046 p += xs_size(*key);
1047
1048 *value = p;
1049 p += xs_size(*value);
1050 }
1051 else {
1052 /* end of list */
1053 goon = 0;
1054 }
1055
1056 /* store back the pointer */
1057 *ctxt = p - dict;
1058
1059 return goon;
1060}
1061
1062
1027xs_val *xs_dict_get_def(const xs_dict *dict, const xs_str *key, const xs_val *def) 1063xs_val *xs_dict_get_def(const xs_dict *dict, const xs_str *key, const xs_val *def)
1028/* returns the value directed by key, or the default value */ 1064/* returns the value directed by key, or the default value */
1029{ 1065{
1030 XS_ASSERT_TYPE(dict, XSTYPE_DICT); 1066 XS_ASSERT_TYPE(dict, XSTYPE_DICT);
1031 XS_ASSERT_TYPE(key, XSTYPE_STRING); 1067 XS_ASSERT_TYPE(key, XSTYPE_STRING);
1032 1068
1033 xs_dict *p = (xs_dict *)dict;
1034 xs_str *k; 1069 xs_str *k;
1035 xs_val *v; 1070 xs_val *v;
1071 int c = 0;
1036 1072
1037 while (xs_dict_iter(&p, &k, &v)) { 1073 while (xs_dict_next(dict, &k, &v, &c)) {
1038 if (strcmp(k, key) == 0) 1074 if (strcmp(k, key) == 0)
1039 return v; 1075 return v;
1040 } 1076 }
@@ -1051,9 +1087,9 @@ xs_dict *xs_dict_del(xs_dict *dict, const xs_str *key)
1051 1087
1052 xs_str *k; 1088 xs_str *k;
1053 xs_val *v; 1089 xs_val *v;
1054 xs_dict *p = dict; 1090 int c = 0;
1055 1091
1056 while (xs_dict_iter(&p, &k, &v)) { 1092 while (xs_dict_next(dict, &k, &v, &c)) {
1057 if (strcmp(k, key) == 0) { 1093 if (strcmp(k, key) == 0) {
1058 /* the address of the item is just behind the key */ 1094 /* the address of the item is just behind the key */
1059 char *i = k - 1; 1095 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,
93 xs_dict *response; 93 xs_dict *response;
94 CURL *curl; 94 CURL *curl;
95 struct curl_slist *list = NULL; 95 struct curl_slist *list = NULL;
96 xs_dict *p;
97 xs_str *k; 96 xs_str *k;
98 xs_val *v; 97 xs_val *v;
99 long lstatus = 0; 98 long lstatus = 0;
@@ -147,8 +146,8 @@ xs_dict *xs_http_request(const char *method, const char *url,
147 } 146 }
148 147
149 /* fill the request headers */ 148 /* fill the request headers */
150 p = (xs_dict *)headers; 149 int c = 0;
151 while (xs_dict_iter(&p, &k, &v)) { 150 while (xs_dict_next(headers, &k, &v, &c)) {
152 xs *h = xs_fmt("%s: %s", k, v); 151 xs *h = xs_fmt("%s: %s", k, v);
153 152
154 list = curl_slist_append(list, h); 153 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
293 struct fcgi_record_header hdr = {0}; 293 struct fcgi_record_header hdr = {0};
294 struct fcgi_end_request ereq = {0}; 294 struct fcgi_end_request ereq = {0};
295 xs *out = xs_str_new(NULL); 295 xs *out = xs_str_new(NULL);
296 xs_dict *p;
297 xs_str *k; 296 xs_str *k;
298 xs_str *v; 297 xs_str *v;
299 298
@@ -307,8 +306,8 @@ void xs_fcgi_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b
307 out = xs_str_cat(out, s1); 306 out = xs_str_cat(out, s1);
308 } 307 }
309 308
310 p = headers; 309 int c = 0;
311 while (xs_dict_iter(&p, &k, &v)) { 310 while (xs_dict_next(headers, &k, &v, &c)) {
312 xs *s1 = xs_fmt("%s: %s\r\n", k, v); 311 xs *s1 = xs_fmt("%s: %s\r\n", k, v);
313 out = xs_str_cat(out, s1); 312 out = xs_str_cat(out, s1);
314 } 313 }
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
98/* sends an httpd response */ 98/* sends an httpd response */
99{ 99{
100 xs *proto; 100 xs *proto;
101 xs_dict *p;
102 xs_str *k; 101 xs_str *k;
103 xs_val *v; 102 xs_val *v;
104 103
105 proto = xs_fmt("HTTP/1.1 %d %s", status, status / 100 == 2 ? "OK" : "ERROR"); 104 proto = xs_fmt("HTTP/1.1 %d %s", status, status / 100 == 2 ? "OK" : "ERROR");
106 fprintf(f, "%s\r\n", proto); 105 fprintf(f, "%s\r\n", proto);
107 106
108 p = headers; 107 int c = 0;
109 while (xs_dict_iter(&p, &k, &v)) { 108 while (xs_dict_next(headers, &k, &v, &c)) {
110 fprintf(f, "%s: %s\r\n", k, v); 109 fprintf(f, "%s: %s\r\n", k, v);
111 } 110 }
112 111
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)
115 fputc('{', f); 115 fputc('{', f);
116 116
117 xs_str *k; 117 xs_str *k;
118 while (xs_dict_iter(&data, &k, &v)) { 118 int ct = 0;
119
120 while (xs_dict_next(s_data, &k, &v, &ct)) {
119 if (c != 0) 121 if (c != 0)
120 fputc(',', f); 122 fputc(',', f);
121 123
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 */