mirror of
https://codeberg.org/grunfink/snac2.git
synced 2024-11-28 07:33:37 +00:00
Backport from xs.
This commit is contained in:
parent
e12af77799
commit
00b019b890
6 changed files with 50 additions and 15 deletions
44
xs.h
44
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);
|
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))
|
#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_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);
|
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)
|
#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);
|
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)
|
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 */
|
/* returns the value directed by key, or the default value */
|
||||||
{
|
{
|
||||||
XS_ASSERT_TYPE(dict, XSTYPE_DICT);
|
XS_ASSERT_TYPE(dict, XSTYPE_DICT);
|
||||||
XS_ASSERT_TYPE(key, XSTYPE_STRING);
|
XS_ASSERT_TYPE(key, XSTYPE_STRING);
|
||||||
|
|
||||||
xs_dict *p = (xs_dict *)dict;
|
|
||||||
xs_str *k;
|
xs_str *k;
|
||||||
xs_val *v;
|
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)
|
if (strcmp(k, key) == 0)
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
@ -1051,9 +1087,9 @@ xs_dict *xs_dict_del(xs_dict *dict, const xs_str *key)
|
||||||
|
|
||||||
xs_str *k;
|
xs_str *k;
|
||||||
xs_val *v;
|
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) {
|
if (strcmp(k, key) == 0) {
|
||||||
/* the address of the item is just behind the key */
|
/* the address of the item is just behind the key */
|
||||||
char *i = k - 1;
|
char *i = k - 1;
|
||||||
|
|
|
@ -93,7 +93,6 @@ xs_dict *xs_http_request(const char *method, const char *url,
|
||||||
xs_dict *response;
|
xs_dict *response;
|
||||||
CURL *curl;
|
CURL *curl;
|
||||||
struct curl_slist *list = NULL;
|
struct curl_slist *list = NULL;
|
||||||
xs_dict *p;
|
|
||||||
xs_str *k;
|
xs_str *k;
|
||||||
xs_val *v;
|
xs_val *v;
|
||||||
long lstatus = 0;
|
long lstatus = 0;
|
||||||
|
@ -147,8 +146,8 @@ xs_dict *xs_http_request(const char *method, const char *url,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* fill the request headers */
|
/* fill the request headers */
|
||||||
p = (xs_dict *)headers;
|
int c = 0;
|
||||||
while (xs_dict_iter(&p, &k, &v)) {
|
while (xs_dict_next(headers, &k, &v, &c)) {
|
||||||
xs *h = xs_fmt("%s: %s", k, v);
|
xs *h = xs_fmt("%s: %s", k, v);
|
||||||
|
|
||||||
list = curl_slist_append(list, h);
|
list = curl_slist_append(list, 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_record_header hdr = {0};
|
||||||
struct fcgi_end_request ereq = {0};
|
struct fcgi_end_request ereq = {0};
|
||||||
xs *out = xs_str_new(NULL);
|
xs *out = xs_str_new(NULL);
|
||||||
xs_dict *p;
|
|
||||||
xs_str *k;
|
xs_str *k;
|
||||||
xs_str *v;
|
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);
|
out = xs_str_cat(out, s1);
|
||||||
}
|
}
|
||||||
|
|
||||||
p = headers;
|
int c = 0;
|
||||||
while (xs_dict_iter(&p, &k, &v)) {
|
while (xs_dict_next(headers, &k, &v, &c)) {
|
||||||
xs *s1 = xs_fmt("%s: %s\r\n", k, v);
|
xs *s1 = xs_fmt("%s: %s\r\n", k, v);
|
||||||
out = xs_str_cat(out, s1);
|
out = xs_str_cat(out, s1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,15 +98,14 @@ void xs_httpd_response(FILE *f, int status, xs_dict *headers, xs_str *body, int
|
||||||
/* sends an httpd response */
|
/* sends an httpd response */
|
||||||
{
|
{
|
||||||
xs *proto;
|
xs *proto;
|
||||||
xs_dict *p;
|
|
||||||
xs_str *k;
|
xs_str *k;
|
||||||
xs_val *v;
|
xs_val *v;
|
||||||
|
|
||||||
proto = xs_fmt("HTTP/1.1 %d %s", status, status / 100 == 2 ? "OK" : "ERROR");
|
proto = xs_fmt("HTTP/1.1 %d %s", status, status / 100 == 2 ? "OK" : "ERROR");
|
||||||
fprintf(f, "%s\r\n", proto);
|
fprintf(f, "%s\r\n", proto);
|
||||||
|
|
||||||
p = headers;
|
int c = 0;
|
||||||
while (xs_dict_iter(&p, &k, &v)) {
|
while (xs_dict_next(headers, &k, &v, &c)) {
|
||||||
fprintf(f, "%s: %s\r\n", k, v);
|
fprintf(f, "%s: %s\r\n", k, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -115,7 +115,9 @@ static void _xs_json_dump(const xs_val *s_data, int level, int indent, FILE *f)
|
||||||
fputc('{', f);
|
fputc('{', f);
|
||||||
|
|
||||||
xs_str *k;
|
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)
|
if (c != 0)
|
||||||
fputc(',', f);
|
fputc(',', f);
|
||||||
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
/* 73ff6e75bec88fa0b908b039462180a8ac1401de 2024-03-08T07:17:30+01:00 */
|
/* f46d5b29627b20a6e9ec4ef60c01df1d2d778520 2024-03-09T08:26:31+01:00 */
|
||||||
|
|
Loading…
Reference in a new issue