From 876bebd9ac904ca930117237edaf8c3dcae7a922 Mon Sep 17 00:00:00 2001 From: default Date: Sat, 28 Jan 2023 17:49:02 +0100 Subject: [PATCH] Backport from xs. --- xs.h | 321 ++++++++++++++++++++++++++++++--------------------- xs_curl.h | 22 ++-- xs_encdec.h | 28 ++--- xs_glob.h | 6 +- xs_httpd.h | 43 +++---- xs_io.h | 16 +-- xs_json.h | 12 +- xs_openssl.h | 34 +++--- xs_regex.h | 19 +-- xs_set.h | 12 +- xs_time.h | 6 +- xs_version.h | 2 +- 12 files changed, 293 insertions(+), 228 deletions(-) diff --git a/xs.h b/xs.h index 993aa04..28b2725 100644 --- a/xs.h +++ b/xs.h @@ -14,25 +14,31 @@ #include typedef enum { - XSTYPE_NULL = 0x18, - XSTYPE_TRUE = 0x06, - XSTYPE_FALSE = 0x15, - XSTYPE_LIST = 0x11, - XSTYPE_LITEM = 0x1f, - XSTYPE_EOL = 0x12, - XSTYPE_DICT = 0x13, - XSTYPE_DITEM = 0x1e, - XSTYPE_EOD = 0x14, - XSTYPE_NUMBER = 0x17, - XSTYPE_STRING = 0x02 + XSTYPE_STRING = 0x02, /* C string (\0 delimited) (NOT STORED) */ + XSTYPE_NUMBER = 0x17, /* C string (\0 delimited) */ + XSTYPE_NULL = 0x18, /* Special NULL value */ + XSTYPE_TRUE = 0x06, /* Boolean */ + XSTYPE_FALSE = 0x15, /* Boolean */ + XSTYPE_LIST = 0x1d, /* Sequence of LITEMs up to EOM (with 24bit size) */ + XSTYPE_LITEM = 0x1f, /* Element of a list (any type) */ + XSTYPE_DICT = 0x1c, /* Sequence of DITEMs up to EOM (with 24bit size) */ + XSTYPE_DITEM = 0x1e, /* Element of a dict (STRING key + any type) */ + XSTYPE_EOM = 0x19 /* End of Multiple (LIST or DICT) */ } xstype; /* dynamic strings */ typedef char d_char; +/* types */ +typedef char xs_val; +typedef char xs_str; +typedef char xs_list; +typedef char xs_dict; +typedef char xs_number; + /* auto-destroyable strings */ -#define xs __attribute__ ((__cleanup__ (_xs_destroy))) d_char +#define xs __attribute__ ((__cleanup__ (_xs_destroy))) xs_val /* not really all, just very much */ #define XS_ALL 0xfffffff @@ -43,60 +49,75 @@ void *_xs_realloc(void *ptr, size_t size, const char *file, int line, const char int _xs_blk_size(int sz); void _xs_destroy(char **var); #define xs_debug() raise(SIGTRAP) -xstype xs_type(const char *data); -int xs_size(const char *data); -int xs_is_null(const char *data); -d_char *xs_dup(const char *data); -d_char *xs_expand(d_char *data, int offset, int size); -d_char *xs_collapse(d_char *data, int offset, int size); -d_char *xs_insert_m(d_char *data, int offset, const char *mem, int size); +xstype xs_type(const xs_val *data); +int xs_size(const xs_val *data); +int xs_is_null(const xs_val *data); +xs_val *xs_dup(const xs_val *data); +xs_val *xs_expand(xs_val *data, int offset, int size); +xs_val *xs_collapse(xs_val *data, int offset, int size); +xs_val *xs_insert_m(xs_val *data, int offset, const char *mem, int size); #define xs_insert(data, offset, data2) xs_insert_m(data, offset, data2, xs_size(data2)) #define xs_append_m(data, mem, size) xs_insert_m(data, xs_size(data) - 1, mem, size) -d_char *xs_str_new(const char *str); -d_char *xs_str_wrap_i(const char *prefix, d_char *str, const char *suffix); + +xs_str *xs_str_new(const char *str); +xs_str *xs_str_wrap_i(const char *prefix, xs_str *str, const char *suffix); #define xs_str_prepend_i(str, prefix) xs_str_wrap_i(prefix, str, NULL) #define xs_str_cat(str, suffix) xs_str_wrap_i(NULL, str, suffix) -d_char *xs_replace_i(d_char *str, const char *sfrom, const char *sto); +xs_str *xs_replace_i(xs_str *str, const char *sfrom, const char *sto); #define xs_replace(str, sfrom, sto) xs_replace_i(xs_dup(str), sfrom, sto) -d_char *xs_fmt(const char *fmt, ...); +xs_str *xs_fmt(const char *fmt, ...); int xs_str_in(const char *haystack, const char *needle); int xs_startswith(const char *str, const char *prefix); int xs_endswith(const char *str, const char *postfix); -d_char *xs_crop_i(d_char *str, int start, int end); -d_char *xs_strip_chars_i(d_char *str, const char *chars); +xs_str *xs_crop_i(xs_str *str, int start, int end); +xs_str *xs_strip_chars_i(xs_str *str, const char *chars); #define xs_strip_i(str) xs_strip_chars_i(str, " \r\n\t\v\f") -d_char *xs_tolower_i(d_char *str); -d_char *xs_list_new(void); -d_char *xs_list_append_m(d_char *list, const char *mem, int dsz); +xs_str *xs_tolower_i(xs_str *str); + +xs_list *xs_list_new(void); +xs_list *xs_list_append_m(xs_list *list, const char *mem, int dsz); #define xs_list_append(list, data) xs_list_append_m(list, data, xs_size(data)) -int xs_list_iter(char **list, char **value); -int xs_list_len(char *list); -char *xs_list_get(char *list, int num); -d_char *xs_list_del(d_char *list, int num); -d_char *xs_list_insert(d_char *list, int num, const char *data); -d_char *xs_list_insert_sorted(d_char *list, const char *str); -d_char *xs_list_set(d_char *list, int num, const char *data); -d_char *xs_list_dequeue(d_char *list, char **data, int last); +int xs_list_iter(xs_list **list, xs_val **value); +int xs_list_len(xs_list *list); +char *xs_list_get(xs_list *list, int num); +xs_list *xs_list_del(xs_list *list, int num); +xs_list *xs_list_insert(xs_list *list, int num, const xs_val *data); +xs_list *xs_list_insert_sorted(xs_list *list, const char *str); +xs_list *xs_list_set(xs_list *list, int num, const xs_val *data); +xs_list *xs_list_dequeue(xs_list *list, xs_val **data, int last); #define xs_list_pop(list, data) xs_list_dequeue(list, data, 1) #define xs_list_shift(list, data) xs_list_dequeue(list, data, 0) -int xs_list_in(char *list, const char *val); -d_char *xs_join(char *list, const char *sep); -d_char *xs_split_n(const char *str, const char *sep, int times); +int xs_list_in(xs_list *list, const xs_val *val); +xs_str *xs_join(xs_list *list, const char *sep); +xs_list *xs_split_n(const char *str, const char *sep, int times); #define xs_split(str, sep) xs_split_n(str, sep, XS_ALL) -d_char *xs_dict_new(void); -d_char *xs_dict_append_m(d_char *dict, const char *key, const char *mem, int dsz); + +xs_dict *xs_dict_new(void); +xs_dict *xs_dict_append_m(xs_dict *dict, const xs_str *key, const xs_val *mem, int dsz); #define xs_dict_append(dict, key, data) xs_dict_append_m(dict, key, data, xs_size(data)) -int xs_dict_iter(char **dict, char **key, char **value); -char *xs_dict_get(char *dict, const char *key); -d_char *xs_dict_del(d_char *dict, const char *key); -d_char *xs_dict_set(d_char *dict, const char *key, const char *data); -d_char *xs_val_new(xstype t); -d_char *xs_number_new(double f); -double xs_number_get(const char *v); -const char *xs_number_str(const char *v); +int xs_dict_iter(xs_dict **dict, xs_str **key, xs_val **value); +xs_dict *xs_dict_get(xs_dict *dict, const xs_str *key); +xs_dict *xs_dict_del(xs_dict *dict, const xs_str *key); +xs_dict *xs_dict_set(xs_dict *dict, const xs_str *key, const xs_val *data); + +xs_val *xs_val_new(xstype t); +xs_number *xs_number_new(double f); +double xs_number_get(const xs_number *v); +const char *xs_number_str(const xs_number *v); void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size); + +#ifdef XS_ASSERT +#include +#define XS_ASSERT_TYPE(v, t) assert(xs_type(v) == t) +#define XS_ASSERT_TYPE_NULL(v, t) assert(v == NULL || xs_type(v) == t) +#else +#define XS_ASSERT_TYPE(v, t) (void)(0) +#define XS_ASSERT_TYPE_NULL(v, t) (void)(0) +#endif + + #ifdef XS_IMPLEMENTATION void *_xs_realloc(void *ptr, size_t size, const char *file, int line, const char *func) @@ -183,22 +204,24 @@ int _xs_blk_size(int sz) } -xstype xs_type(const char *data) +xstype xs_type(const xs_val *data) /* return the type of data */ { xstype t; + if (data == NULL) + t = XSTYPE_NULL; + else switch (data[0]) { case XSTYPE_NULL: case XSTYPE_TRUE: case XSTYPE_FALSE: case XSTYPE_LIST: - case XSTYPE_EOL: - case XSTYPE_DICT: - case XSTYPE_EOD: case XSTYPE_LITEM: + case XSTYPE_DICT: case XSTYPE_DITEM: case XSTYPE_NUMBER: + case XSTYPE_EOM: t = data[0]; break; default: @@ -210,7 +233,7 @@ xstype xs_type(const char *data) } -void _xs_put_24b(char *ptr, int i) +void _xs_put_24b(xs_val *ptr, int i) /* writes i as a 24 bit value */ { unsigned char *p = (unsigned char *)ptr; @@ -221,7 +244,7 @@ void _xs_put_24b(char *ptr, int i) } -int _xs_get_24b(const char *ptr) +int _xs_get_24b(const xs_val *ptr) /* reads a 24 bit value */ { unsigned char *p = (unsigned char *)ptr; @@ -230,7 +253,7 @@ int _xs_get_24b(const char *ptr) } -int xs_size(const char *data) +int xs_size(const xs_val *data) /* returns the size of data in bytes */ { int len = 0; @@ -286,18 +309,18 @@ int xs_size(const char *data) } -int xs_is_null(const char *data) +int xs_is_null(const xs_val *data) /* checks for null */ { - return !!(data == NULL || xs_type(data) == XSTYPE_NULL); + return (xs_type(data) == XSTYPE_NULL); } -d_char *xs_dup(const char *data) +xs_val *xs_dup(const xs_val *data) /* creates a duplicate of data */ { int sz = xs_size(data); - d_char *s = xs_realloc(NULL, _xs_blk_size(sz)); + xs_val *s = xs_realloc(NULL, _xs_blk_size(sz)); memcpy(s, data, sz); @@ -305,7 +328,7 @@ d_char *xs_dup(const char *data) } -d_char *xs_expand(d_char *data, int offset, int size) +xs_val *xs_expand(xs_val *data, int offset, int size) /* opens a hole in data */ { int sz = xs_size(data); @@ -325,7 +348,7 @@ d_char *xs_expand(d_char *data, int offset, int size) } -d_char *xs_collapse(d_char *data, int offset, int size) +xs_val *xs_collapse(xs_val *data, int offset, int size) /* shrinks data */ { int sz = xs_size(data); @@ -348,7 +371,7 @@ d_char *xs_collapse(d_char *data, int offset, int size) } -d_char *xs_insert_m(d_char *data, int offset, const char *mem, int size) +xs_val *xs_insert_m(xs_val *data, int offset, const char *mem, int size) /* inserts a memory block */ { data = xs_expand(data, offset, size); @@ -360,16 +383,18 @@ d_char *xs_insert_m(d_char *data, int offset, const char *mem, int size) /** strings **/ -d_char *xs_str_new(const char *str) +xs_str *xs_str_new(const char *str) /* creates a new string */ { return xs_insert(NULL, 0, str ? str : ""); } -d_char *xs_str_wrap_i(const char *prefix, d_char *str, const char *suffix) +xs_str *xs_str_wrap_i(const char *prefix, xs_str *str, const char *suffix) /* wraps str with prefix and suffix */ { + XS_ASSERT_TYPE(str, XSTYPE_STRING); + if (prefix) str = xs_insert_m(str, 0, prefix, strlen(prefix)); @@ -380,9 +405,11 @@ d_char *xs_str_wrap_i(const char *prefix, d_char *str, const char *suffix) } -d_char *xs_replace_i(d_char *str, const char *sfrom, const char *sto) +xs_str *xs_replace_i(xs_str *str, const char *sfrom, const char *sto) /* replaces inline all sfrom with sto */ { + XS_ASSERT_TYPE(str, XSTYPE_STRING); + int sfsz = strlen(sfrom); int stsz = strlen(sto); char *ss; @@ -402,11 +429,11 @@ d_char *xs_replace_i(d_char *str, const char *sfrom, const char *sto) } -d_char *xs_fmt(const char *fmt, ...) +xs_str *xs_fmt(const char *fmt, ...) /* formats a string with printf()-like marks */ { int n; - d_char *s = NULL; + xs_str *s = NULL; va_list ap; va_start(ap, fmt); @@ -455,9 +482,11 @@ int xs_endswith(const char *str, const char *postfix) } -d_char *xs_crop_i(d_char *str, int start, int end) +xs_str *xs_crop_i(xs_str *str, int start, int end) /* crops the d_char to be only from start to end */ { + XS_ASSERT_TYPE(str, XSTYPE_STRING); + int sz = strlen(str); if (end <= 0) @@ -473,9 +502,11 @@ d_char *xs_crop_i(d_char *str, int start, int end) } -d_char *xs_strip_chars_i(d_char *str, const char *chars) +xs_str *xs_strip_chars_i(xs_str *str, const char *chars) /* strips the string of chars from the start and the end */ { + XS_ASSERT_TYPE(str, XSTYPE_STRING); + int n; /* strip first from the end */ @@ -494,9 +525,11 @@ d_char *xs_strip_chars_i(d_char *str, const char *chars) } -d_char *xs_tolower_i(d_char *str) +xs_str *xs_tolower_i(xs_str *str) /* convert to lowercase */ { + XS_ASSERT_TYPE(str, XSTYPE_STRING); + int n; for (n = 0; str[n]; n++) @@ -508,14 +541,14 @@ d_char *xs_tolower_i(d_char *str) /** lists **/ -d_char *xs_list_new(void) +xs_list *xs_list_new(void) /* creates a new list */ { - d_char *list; + xs_list *list; list = xs_realloc(NULL, _xs_blk_size(5)); list[0] = XSTYPE_LIST; - list[4] = XSTYPE_EOL; + list[4] = XSTYPE_EOM; _xs_put_24b(list + 1, 5); @@ -523,9 +556,11 @@ d_char *xs_list_new(void) } -d_char *_xs_list_write_litem(d_char *list, int offset, const char *mem, int dsz) +xs_list *_xs_list_write_litem(xs_list *list, int offset, const char *mem, int dsz) /* writes a list item */ { + XS_ASSERT_TYPE(list, XSTYPE_LIST); + char c = XSTYPE_LITEM; list = xs_insert_m(list, offset, &c, 1); @@ -535,30 +570,28 @@ d_char *_xs_list_write_litem(d_char *list, int offset, const char *mem, int dsz) } -d_char *xs_list_append_m(d_char *list, const char *mem, int dsz) +xs_list *xs_list_append_m(xs_list *list, const char *mem, int dsz) /* adds a memory block to the list */ { + XS_ASSERT_TYPE(list, XSTYPE_LIST); + return _xs_list_write_litem(list, xs_size(list) - 1, mem, dsz); } -int xs_list_iter(char **list, char **value) +int xs_list_iter(xs_list **list, xs_val **value) /* iterates a list value */ { int goon = 1; - char *p; - if (list == NULL || *list == NULL) - return 0; - - p = *list; + xs_val *p = *list; /* skip the start of the list */ - if (*p == XSTYPE_LIST) + if (xs_type(p) == XSTYPE_LIST) p += 4; /* an element? */ - if (*p == XSTYPE_LITEM) { + if (xs_type(p) == XSTYPE_LITEM) { p++; *value = p; @@ -567,7 +600,6 @@ int xs_list_iter(char **list, char **value) } else { /* end of list */ - p++; goon = 0; } @@ -578,11 +610,13 @@ int xs_list_iter(char **list, char **value) } -int xs_list_len(char *list) +int xs_list_len(xs_list *list) /* returns the number of elements in the list */ { + XS_ASSERT_TYPE_NULL(list, XSTYPE_LIST); + int c = 0; - char *v; + xs_val *v; while (xs_list_iter(&list, &v)) c++; @@ -591,11 +625,13 @@ int xs_list_len(char *list) } -char *xs_list_get(char *list, int num) +xs_val *xs_list_get(xs_list *list, int num) /* returns the element #num */ { - char *v; + XS_ASSERT_TYPE(list, XSTYPE_LIST); + int c = 0; + xs_val *v; if (num < 0) num = xs_list_len(list) + num; @@ -611,10 +647,12 @@ char *xs_list_get(char *list, int num) } -d_char *xs_list_del(d_char *list, int num) +xs_list *xs_list_del(xs_list *list, int num) /* deletes element #num */ { - char *v; + XS_ASSERT_TYPE(list, XSTYPE_LIST); + + xs_val *v; if ((v = xs_list_get(list, num)) != NULL) list = xs_collapse(list, v - 1 - list, xs_size(v - 1)); @@ -623,10 +661,12 @@ d_char *xs_list_del(d_char *list, int num) } -d_char *xs_list_insert(d_char *list, int num, const char *data) +xs_list *xs_list_insert(xs_list *list, int num, const xs_val *data) /* inserts an element at #num position */ { - char *v; + XS_ASSERT_TYPE(list, XSTYPE_LIST); + + xs_val *v; int offset; if ((v = xs_list_get(list, num)) != NULL) @@ -638,9 +678,12 @@ d_char *xs_list_insert(d_char *list, int num, const char *data) } -d_char *xs_list_insert_sorted(d_char *list, const char *str) +xs_list *xs_list_insert_sorted(xs_list *list, const xs_str *str) /* inserts a string in the list in its ordered position */ { + XS_ASSERT_TYPE(list, XSTYPE_LIST); + XS_ASSERT_TYPE(str, XSTYPE_STRING); + char *p, *v; int offset = xs_size(list); @@ -657,9 +700,11 @@ d_char *xs_list_insert_sorted(d_char *list, const char *str) } -d_char *xs_list_set(d_char *list, int num, const char *data) +xs_list *xs_list_set(xs_list *list, int num, const xs_val *data) /* sets the element at #num position */ { + XS_ASSERT_TYPE(list, XSTYPE_LIST); + list = xs_list_del(list, num); list = xs_list_insert(list, num, data); @@ -667,10 +712,13 @@ d_char *xs_list_set(d_char *list, int num, const char *data) } -d_char *xs_list_dequeue(d_char *list, char **data, int last) +xs_list *xs_list_dequeue(xs_list *list, xs_val **data, int last) /* gets a copy of the first or last element of a list, shrinking it */ { - char *p = list, *v = NULL; + XS_ASSERT_TYPE(list, XSTYPE_LIST); + + xs_list *p = list; + xs_val *v = NULL; if (!last) { /* get the first */ @@ -692,11 +740,13 @@ d_char *xs_list_dequeue(d_char *list, char **data, int last) } -int xs_list_in(char *list, const char *val) +int xs_list_in(xs_list *list, const xs_val *val) /* returns the position of val in list or -1 */ { + XS_ASSERT_TYPE_NULL(list, XSTYPE_LIST); + int n = 0; - char *v; + xs_val *v; int sz = xs_size(val); while (xs_list_iter(&list, &v)) { @@ -710,11 +760,13 @@ int xs_list_in(char *list, const char *val) } -d_char *xs_join(char *list, const char *sep) +xs_str *xs_join(xs_list *list, const char *sep) /* joins a list into a string */ { - d_char *s = NULL; - char *v; + XS_ASSERT_TYPE(list, XSTYPE_LIST); + + xs_str *s = NULL; + xs_val *v; int c = 0; int offset = 0; int ssz = strlen(sep); @@ -749,12 +801,12 @@ d_char *xs_join(char *list, const char *sep) } -d_char *xs_split_n(const char *str, const char *sep, int times) +xs_list *xs_split_n(const char *str, const char *sep, int times) /* splits a string into a list upto n times */ { int sz = strlen(sep); char *ss; - d_char *list; + xs_list *list; list = xs_list_new(); @@ -763,7 +815,7 @@ d_char *xs_split_n(const char *str, const char *sep, int times) list = xs_list_append_m(list, str, ss - str); /* add the asciiz */ - list = xs_str_cat(list, ""); + list = xs_insert_m(list, xs_size(list) - 1, "", 1); /* skip past the separator */ str = ss + sz; @@ -780,14 +832,14 @@ d_char *xs_split_n(const char *str, const char *sep, int times) /** dicts **/ -d_char *xs_dict_new(void) +xs_dict *xs_dict_new(void) /* creates a new dict */ { - d_char *dict; + xs_dict *dict; dict = xs_realloc(NULL, _xs_blk_size(5)); dict[0] = XSTYPE_DICT; - dict[4] = XSTYPE_EOD; + dict[4] = XSTYPE_EOM; _xs_put_24b(dict + 1, 5); @@ -795,9 +847,12 @@ d_char *xs_dict_new(void) } -d_char *xs_dict_append_m(d_char *dict, const char *key, const char *mem, int dsz) +xs_dict *xs_dict_append_m(xs_dict *dict, const xs_str *key, const xs_val *mem, int dsz) /* adds a memory block to the dict */ { + XS_ASSERT_TYPE(dict, XSTYPE_DICT); + XS_ASSERT_TYPE(key, XSTYPE_STRING); + char c = XSTYPE_DITEM; int sz = xs_size(dict); int ksz = xs_size(key); @@ -810,23 +865,19 @@ d_char *xs_dict_append_m(d_char *dict, const char *key, const char *mem, int dsz } -int xs_dict_iter(char **dict, char **key, char **value) +int xs_dict_iter(xs_dict **dict, xs_str **key, xs_val **value) /* iterates a dict value */ { int goon = 1; - char *p; - if (dict == NULL || *dict == NULL) - return 0; - - p = *dict; + xs_val *p = *dict; /* skip the start of the list */ - if (*p == XSTYPE_DICT) + if (xs_type(p) == XSTYPE_DICT) p += 4; /* an element? */ - if (*p == XSTYPE_DITEM) { + if (xs_type(p) == XSTYPE_DITEM) { p++; *key = p; @@ -837,7 +888,6 @@ int xs_dict_iter(char **dict, char **key, char **value) } else { /* end of list */ - p++; goon = 0; } @@ -848,10 +898,14 @@ int xs_dict_iter(char **dict, char **key, char **value) } -char *xs_dict_get(char *dict, const char *key) +xs_val *xs_dict_get(xs_dict *dict, const xs_str *key) /* returns the value directed by key */ { - char *k, *v; + XS_ASSERT_TYPE(dict, XSTYPE_DICT); + XS_ASSERT_TYPE(key, XSTYPE_STRING); + + xs_str *k; + xs_val *v; while (xs_dict_iter(&dict, &k, &v)) { if (strcmp(k, key) == 0) @@ -862,11 +916,15 @@ char *xs_dict_get(char *dict, const char *key) } -d_char *xs_dict_del(d_char *dict, const char *key) +xs_dict *xs_dict_del(xs_dict *dict, const xs_str *key) /* deletes a key */ { - char *k, *v; - char *p = dict; + XS_ASSERT_TYPE(dict, XSTYPE_DICT); + XS_ASSERT_TYPE(key, XSTYPE_STRING); + + xs_str *k; + xs_val *v; + xs_dict *p = dict; while (xs_dict_iter(&p, &k, &v)) { if (strcmp(k, key) == 0) { @@ -882,9 +940,12 @@ d_char *xs_dict_del(d_char *dict, const char *key) } -d_char *xs_dict_set(d_char *dict, const char *key, const char *data) +xs_dict *xs_dict_set(xs_dict *dict, const xs_str *key, const xs_val *data) /* sets (replaces) a key */ { + XS_ASSERT_TYPE(dict, XSTYPE_DICT); + XS_ASSERT_TYPE(key, XSTYPE_STRING); + /* delete the possibly existing key */ dict = xs_dict_del(dict, key); @@ -897,10 +958,10 @@ d_char *xs_dict_set(d_char *dict, const char *key, const char *data) /** other values **/ -d_char *xs_val_new(xstype t) +xs_val *xs_val_new(xstype t) /* adds a new special value */ { - d_char *v = xs_realloc(NULL, _xs_blk_size(1)); + xs_val *v = xs_realloc(NULL, _xs_blk_size(1)); v[0] = t; @@ -910,10 +971,10 @@ d_char *xs_val_new(xstype t) /** numbers */ -d_char *xs_number_new(double f) +xs_number *xs_number_new(double f) /* adds a new number value */ { - d_char *v; + xs_number *v; char tmp[64]; snprintf(tmp, sizeof(tmp), "%.15lf", f); @@ -940,7 +1001,7 @@ d_char *xs_number_new(double f) } -double xs_number_get(const char *v) +double xs_number_get(const xs_number *v) /* gets the number as a double */ { double f = 0.0; @@ -952,7 +1013,7 @@ double xs_number_get(const char *v) } -const char *xs_number_str(const char *v) +const char *xs_number_str(const xs_number *v) /* gets the number as a string */ { const char *p = NULL; diff --git a/xs_curl.h b/xs_curl.h index 09f51d6..45f3a91 100644 --- a/xs_curl.h +++ b/xs_curl.h @@ -4,18 +4,18 @@ #define _XS_CURL_H -d_char *xs_http_request(char *method, char *url, d_char *headers, - d_char *body, int b_size, int *status, - d_char **payload, int *p_size, int timeout); +xs_dict *xs_http_request(char *method, char *url, xs_dict *headers, + xs_str *body, int b_size, int *status, + xs_str **payload, int *p_size, int timeout); #ifdef XS_IMPLEMENTATION #include static size_t _header_callback(char *buffer, size_t size, - size_t nitems, d_char **userdata) + size_t nitems, xs_dict **userdata) { - d_char *headers = *userdata; + xs_dict *headers = *userdata; xs *l; /* get the line */ @@ -83,15 +83,17 @@ static int _post_callback(char *buffer, size_t size, } -d_char *xs_http_request(char *method, char *url, d_char *headers, - d_char *body, int b_size, int *status, - d_char **payload, int *p_size, int timeout) +xs_dict *xs_http_request(char *method, char *url, xs_dict *headers, + xs_str *body, int b_size, int *status, + xs_str **payload, int *p_size, int timeout) /* does an HTTP request */ { - d_char *response; + xs_dict *response; CURL *curl; struct curl_slist *list = NULL; - char *k, *v, *p; + xs_dict *p; + xs_str *k; + xs_val *v; long lstatus; struct _payload_data pd; diff --git a/xs_encdec.h b/xs_encdec.h index 5f9ae01..5966583 100644 --- a/xs_encdec.h +++ b/xs_encdec.h @@ -4,19 +4,19 @@ #define _XS_ENCDEC_H - d_char *xs_hex_enc(const char *data, int size); - d_char *xs_hex_dec(const char *hex, int *size); - d_char *xs_base64_enc(const char *data, int sz); - d_char *xs_base64_dec(const char *data, int *size); - d_char *xs_utf8_enc(d_char *str, unsigned int cpoint); + xs_str *xs_hex_enc(const xs_val *data, int size); + xs_val *xs_hex_dec(const xs_str *hex, int *size); + xs_str *xs_base64_enc(const xs_val *data, int sz); + xs_val *xs_base64_dec(const xs_str *data, int *size); + xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint); #ifdef XS_IMPLEMENTATION -d_char *xs_hex_enc(const char *data, int size) +xs_str *xs_hex_enc(const xs_val *data, int size) /* returns an hexdump of data */ { - d_char *s; + xs_str *s; char *p; int n; @@ -33,11 +33,11 @@ d_char *xs_hex_enc(const char *data, int size) } -d_char *xs_hex_dec(const char *hex, int *size) +xs_val *xs_hex_dec(const xs_str *hex, int *size) /* decodes an hexdump into data */ { int sz = strlen(hex); - d_char *s = NULL; + xs_val *s = NULL; char *p; int n; @@ -65,10 +65,10 @@ d_char *xs_hex_dec(const char *hex, int *size) } -d_char *xs_base64_enc(const char *data, int sz) +xs_str *xs_base64_enc(const xs_val *data, int sz) /* encodes data to base64 */ { - d_char *s; + xs_str *s; unsigned char *p; char *i; int bsz, n; @@ -110,10 +110,10 @@ d_char *xs_base64_enc(const char *data, int sz) } -d_char *xs_base64_dec(const char *data, int *size) +xs_val *xs_base64_dec(const xs_str *data, int *size) /* decodes data from base64 */ { - d_char *s = NULL; + xs_val *s = NULL; int sz = 0; char *p; static char *b64_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" @@ -171,7 +171,7 @@ d_char *xs_base64_dec(const char *data, int *size) } -d_char *xs_utf8_enc(d_char *str, unsigned int cpoint) +xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint) /* encodes an Unicode codepoint to utf8 */ { unsigned char tmp[4]; diff --git a/xs_glob.h b/xs_glob.h index 795abd9..2da3729 100644 --- a/xs_glob.h +++ b/xs_glob.h @@ -4,7 +4,7 @@ #define _XS_GLOB_H -d_char *xs_glob_n(const char *spec, int basename, int reverse, int max); +xs_list *xs_glob_n(const char *spec, int basename, int reverse, int max); #define xs_glob(spec, basename, reverse) xs_glob_n(spec, basename, reverse, XS_ALL) @@ -12,11 +12,11 @@ d_char *xs_glob_n(const char *spec, int basename, int reverse, int max); #include -d_char *xs_glob_n(const char *spec, int basename, int reverse, int max) +xs_list *xs_glob_n(const char *spec, int basename, int reverse, int max) /* does a globbing and returns the found files */ { glob_t globbuf; - d_char *list = xs_list_new(); + xs_list *list = xs_list_new(); if (glob(spec, 0, NULL, &globbuf) == 0) { int n; diff --git a/xs_httpd.h b/xs_httpd.h index 47901e8..829411c 100644 --- a/xs_httpd.h +++ b/xs_httpd.h @@ -4,18 +4,18 @@ #define _XS_HTTPD_H -d_char *xs_url_dec(char *str); -d_char *xs_url_vars(char *str); -d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size); -void xs_httpd_response(FILE *f, int status, d_char *headers, char *body, int b_size); +xs_str *xs_url_dec(char *str); +xs_dict *xs_url_vars(char *str); +xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size); +void xs_httpd_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b_size); #ifdef XS_IMPLEMENTATION -d_char *xs_url_dec(char *str) +xs_str *xs_url_dec(char *str) /* decodes an URL */ { - d_char *s = xs_str_new(NULL); + xs_str *s = xs_str_new(NULL); while (*str) { if (*str == '%') { @@ -41,19 +41,19 @@ d_char *xs_url_dec(char *str) } -d_char *xs_url_vars(char *str) +xs_dict *xs_url_vars(char *str) /* parse url variables */ { - d_char *vars; + xs_dict *vars; vars = xs_dict_new(); if (str != NULL) { - char *v, *l; - xs *args; - /* split by arguments */ - args = xs_split(str, "&"); + xs *args = xs_split(str, "&"); + + xs_list *l; + xs_val *v; l = args; while (xs_list_iter(&l, &v)) { @@ -69,7 +69,7 @@ d_char *xs_url_vars(char *str) } -d_char *_xs_multipart_form_data(char *payload, int p_size, char *header) +xs_dict *_xs_multipart_form_data(xs_str *payload, int p_size, char *header) /* parses a multipart/form-data payload */ { xs *boundary = NULL; @@ -89,7 +89,7 @@ d_char *_xs_multipart_form_data(char *payload, int p_size, char *header) bsz = strlen(boundary); - d_char *p_vars = xs_dict_new(); + xs_dict *p_vars = xs_dict_new(); /* iterate searching the boundaries */ while ((p = xs_memmem(payload + offset, p_size - offset, boundary, bsz)) != NULL) { @@ -173,12 +173,11 @@ d_char *_xs_multipart_form_data(char *payload, int p_size, char *header) } -d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size) +xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size) /* processes an httpd connection */ { - d_char *req = NULL; - xs *q_vars = NULL; - xs *p_vars = NULL; + xs *q_vars = NULL; + xs *p_vars = NULL; xs *l1, *l2; char *v; @@ -193,7 +192,7 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size) return NULL; } - req = xs_dict_new(); + xs_dict *req = xs_dict_new(); req = xs_dict_append(req, "method", xs_list_get(l2, 0)); req = xs_dict_append(req, "proto", xs_list_get(l2, 2)); @@ -258,11 +257,13 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size) } -void xs_httpd_response(FILE *f, int status, d_char *headers, char *body, int b_size) +void xs_httpd_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b_size) /* sends an httpd response */ { xs *proto; - char *p, *k, *v; + xs_dict *p; + xs_str *k; + xs_val *v; proto = xs_fmt("HTTP/1.1 %d ", status); fprintf(f, "%s\r\n", proto); diff --git a/xs_io.h b/xs_io.h index cb6ba85..f32e814 100644 --- a/xs_io.h +++ b/xs_io.h @@ -4,17 +4,17 @@ #define _XS_IO_H -d_char *xs_readline(FILE *f); -d_char *xs_read(FILE *f, int *size); -d_char *xs_readall(FILE *f); +xs_str *xs_readline(FILE *f); +xs_val *xs_read(FILE *f, int *size); +xs_val *xs_readall(FILE *f); #ifdef XS_IMPLEMENTATION -d_char *xs_readline(FILE *f) +xs_str *xs_readline(FILE *f) /* reads a line from a file */ { - d_char *s = NULL; + xs_str *s = NULL; errno = 0; @@ -38,10 +38,10 @@ d_char *xs_readline(FILE *f) } -d_char *xs_read(FILE *f, int *sz) +xs_val *xs_read(FILE *f, int *sz) /* reads up to size bytes from f */ { - d_char *s = NULL; + xs_val *s = NULL; int size = *sz; int rdsz = 0; @@ -76,7 +76,7 @@ d_char *xs_read(FILE *f, int *sz) } -d_char *xs_readall(FILE *f) +xs_val *xs_readall(FILE *f) /* reads the rest of the file into a string */ { int size = XS_ALL; diff --git a/xs_json.h b/xs_json.h index 2b047ae..3004027 100644 --- a/xs_json.h +++ b/xs_json.h @@ -4,9 +4,9 @@ #define _XS_JSON_H -d_char *xs_json_dumps_pp(char *data, int indent); +xs_str *xs_json_dumps_pp(xs_val *data, int indent); #define xs_json_dumps(data) xs_json_dumps_pp(data, 0) -d_char *xs_json_loads(const char *json); +xs_val *xs_json_loads(const xs_str *json); #ifdef XS_IMPLEMENTATION @@ -148,11 +148,11 @@ d_char *_xs_json_dumps(d_char *s, char *data, int level, int indent) } -d_char *xs_json_dumps_pp(char *data, int indent) +xs_str *xs_json_dumps_pp(xs_val *data, int indent) /* dumps a piece of data as JSON */ { xstype t = xs_type(data); - d_char *s = NULL; + xs_str *s = NULL; if (t == XSTYPE_LIST || t == XSTYPE_DICT) { s = xs_str_new(NULL); @@ -481,10 +481,10 @@ d_char *_xs_json_loads_object(const char **json, js_type *t) } -d_char *xs_json_loads(const char *json) +xs_val *xs_json_loads(const xs_str *json) /* loads a string in JSON format and converts to a multiple data */ { - d_char *v = NULL; + xs_val *v = NULL; js_type t; xs_free(_xs_json_loads_lexer(&json, &t)); diff --git a/xs_openssl.h b/xs_openssl.h index 33de853..f9cc452 100644 --- a/xs_openssl.h +++ b/xs_openssl.h @@ -4,14 +4,14 @@ #define _XS_OPENSSL_H -d_char *xs_md5_hex(const void *input, int size); -d_char *xs_sha1_hex(const void *input, int size); -d_char *xs_sha256_hex(const void *input, int size); -d_char *xs_sha256_base64(const void *input, int size); -d_char *xs_rsa_genkey(int bits); -d_char *xs_rsa_sign(const char *secret, const char *mem, int size); +xs_str *xs_md5_hex(const xs_val *input, int size); +xs_str *xs_sha1_hex(const xs_val *input, int size); +xs_str *xs_sha256_hex(const xs_val *input, int size); +xs_str *xs_sha256_base64(const xs_val *input, int size); +xs_dict *xs_rsa_genkey(int bits); +xs_str *xs_rsa_sign(const char *secret, const char *mem, int size); int xs_rsa_verify(const char *pubkey, const char *mem, int size, const char *b64sig); -d_char *xs_evp_sign(const char *secret, const char *mem, int size); +xs_str *xs_evp_sign(const char *secret, const char *mem, int size); int xs_evp_verify(const char *pubkey, const char *mem, int size, const char *b64sig); @@ -23,7 +23,7 @@ int xs_evp_verify(const char *pubkey, const char *mem, int size, const char *b64 #include "openssl/pem.h" #include "openssl/evp.h" -d_char *xs_md5_hex(const void *input, int size) +xs_str *xs_md5_hex(const xs_val *input, int size) { unsigned char md5[16]; MD5_CTX ctx; @@ -36,7 +36,7 @@ d_char *xs_md5_hex(const void *input, int size) } -d_char *xs_sha1_hex(const void *input, int size) +xs_str *xs_sha1_hex(const xs_val *input, int size) { unsigned char sha1[20]; SHA_CTX ctx; @@ -61,7 +61,7 @@ unsigned char *_xs_sha256(const void *input, int size, unsigned char *sha256) } -d_char *xs_sha256_hex(const void *input, int size) +xs_str *xs_sha256_hex(const xs_val *input, int size) { unsigned char sha256[32]; @@ -71,7 +71,7 @@ d_char *xs_sha256_hex(const void *input, int size) } -d_char *xs_sha256_base64(const void *input, int size) +xs_str *xs_sha256_base64(const xs_val *input, int size) { unsigned char sha256[32]; @@ -81,12 +81,12 @@ d_char *xs_sha256_base64(const void *input, int size) } -d_char *xs_rsa_genkey(int bits) +xs_dict *xs_rsa_genkey(int bits) /* generates an RSA keypair */ { BIGNUM *bne; RSA *rsa; - d_char *keypair = NULL; + xs_dict *keypair = NULL; if ((bne = BN_new()) != NULL) { if (BN_set_word(bne, RSA_F4) == 1) { @@ -119,10 +119,10 @@ d_char *xs_rsa_genkey(int bits) } -d_char *xs_rsa_sign(const char *secret, const char *mem, int size) +xs_str *xs_rsa_sign(const char *secret, const char *mem, int size) /* signs a memory block (secret is in PEM format) */ { - d_char *signature = NULL; + xs_str *signature = NULL; BIO *b; RSA *rsa; unsigned char *sig; @@ -176,10 +176,10 @@ int xs_rsa_verify(const char *pubkey, const char *mem, int size, const char *b64 } -d_char *xs_evp_sign(const char *secret, const char *mem, int size) +xs_str *xs_evp_sign(const char *secret, const char *mem, int size) /* signs a memory block (secret is in PEM format) */ { - d_char *signature = NULL; + xs_str *signature = NULL; BIO *b; unsigned char *sig; unsigned int sig_len; diff --git a/xs_regex.h b/xs_regex.h index c01a19a..79b1b3a 100644 --- a/xs_regex.h +++ b/xs_regex.h @@ -4,22 +4,22 @@ #define _XS_REGEX_H -d_char *xs_regex_split_n(const char *str, const char *rx, int count); +xs_list *xs_regex_split_n(const char *str, const char *rx, int count); #define xs_regex_split(str, rx) xs_regex_split_n(str, rx, XS_ALL) -d_char *xs_regex_match_n(const char *str, const char *rx, int count); +xs_list *xs_regex_match_n(const char *str, const char *rx, int count); #define xs_regex_match(str, rx) xs_regex_match_n(str, rx, XS_ALL) #ifdef XS_IMPLEMENTATION #include -d_char *xs_regex_split_n(const char *str, const char *rx, int count) +xs_list *xs_regex_split_n(const char *str, const char *rx, int count) /* splits str by regex */ { regex_t re; regmatch_t rm; int offset = 0; - d_char *list = NULL; + xs_list *list = NULL; const char *p; if (regcomp(&re, rx, REG_EXTENDED)) @@ -30,11 +30,11 @@ d_char *xs_regex_split_n(const char *str, const char *rx, int count) while (count > 0 && !regexec(&re, (p = str + offset), 1, &rm, offset > 0 ? REG_NOTBOL : 0)) { /* add first the leading part of the string */ list = xs_list_append_m(list, p, rm.rm_so); - list = xs_str_cat(list, ""); + list = xs_insert_m(list, xs_size(list) - 1, "", 1); /* add now the matched text as the separator */ list = xs_list_append_m(list, p + rm.rm_so, rm.rm_eo - rm.rm_so); - list = xs_str_cat(list, ""); + list = xs_insert_m(list, xs_size(list) - 1, "", 1); /* move forward */ offset += rm.rm_eo; @@ -51,12 +51,13 @@ d_char *xs_regex_split_n(const char *str, const char *rx, int count) } -d_char *xs_regex_match_n(const char *str, const char *rx, int count) +xs_list *xs_regex_match_n(const char *str, const char *rx, int count) /* returns a list with upto count matches */ { - d_char *list = xs_list_new(); + xs_list *list = xs_list_new(); xs *split = NULL; - char *p, *v; + xs_list *p; + xs_val *v; int n = 0; /* split */ diff --git a/xs_set.h b/xs_set.h index d1daa01..8829f14 100644 --- a/xs_set.h +++ b/xs_set.h @@ -8,13 +8,13 @@ typedef struct _xs_set { int elems; /* number of hash entries */ int used; /* number of used hash entries */ int *hash; /* hashed offsets */ - d_char *list; /* list of stored data */ + xs_list *list; /* list of stored data */ } xs_set; void xs_set_init(xs_set *s); -d_char *xs_set_result(xs_set *s); +xs_list *xs_set_result(xs_set *s); void xs_set_free(xs_set *s); -int xs_set_add(xs_set *s, const char *data); +int xs_set_add(xs_set *s, const xs_val *data); #ifdef XS_IMPLEMENTATION @@ -33,10 +33,10 @@ void xs_set_init(xs_set *s) } -d_char *xs_set_result(xs_set *s) +xs_list *xs_set_result(xs_set *s) /* returns the set as a list and frees it */ { - d_char *list = s->list; + xs_list *list = s->list; s->list = NULL; s->hash = xs_free(s->hash); @@ -93,7 +93,7 @@ static int _store_hash(xs_set *s, const char *data, int value) } -int xs_set_add(xs_set *s, const char *data) +int xs_set_add(xs_set *s, const xs_val *data) /* adds the data to the set */ /* returns: 1 if added, 0 if already there */ { diff --git a/xs_time.h b/xs_time.h index ff0e73e..94e472d 100644 --- a/xs_time.h +++ b/xs_time.h @@ -6,7 +6,7 @@ #include -d_char *xs_str_time(time_t t, const char *fmt, int local); +xs_str *xs_str_time(time_t t, const char *fmt, int local); #define xs_str_localtime(t, fmt) xs_str_time(t, fmt, 1) #define xs_str_utctime(t, fmt) xs_str_time(t, fmt, 0) time_t xs_parse_time(const char *str, const char *fmt, int local); @@ -15,8 +15,8 @@ time_t xs_parse_time(const char *str, const char *fmt, int local); #ifdef XS_IMPLEMENTATION -d_char *xs_str_time(time_t t, const char *fmt, int local) -/* returns a d_char with a formated time */ +xs_str *xs_str_time(time_t t, const char *fmt, int local) +/* returns a string with a formated time */ { struct tm tm; char tmp[64]; diff --git a/xs_version.h b/xs_version.h index 1475b6f..0fe4322 100644 --- a/xs_version.h +++ b/xs_version.h @@ -1 +1 @@ -/* f64030f1449390169bd09bdcbb052b1a70f8002a */ +/* 86f70ffc75a8c9601adde42146af65c14d41518a */