2023-01-17 08:50:16 +00:00
|
|
|
/* copyright (c) 2022 - 2023 grunfink / MIT license */
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
#ifndef _XS_H
|
|
|
|
|
|
|
|
#define _XS_H
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
typedef enum {
|
2023-01-28 16:49:02 +00:00
|
|
|
XSTYPE_STRING = 0x02, /* C string (\0 delimited) (NOT STORED) */
|
2023-02-06 10:09:11 +00:00
|
|
|
XSTYPE_NUMBER = 0x17, /* double in spirit, stored as a C string (\0 delimited) */
|
2023-01-28 16:49:02 +00:00
|
|
|
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) */
|
2023-02-06 10:09:11 +00:00
|
|
|
XSTYPE_EOM = 0x19, /* End of Multiple (LIST or DICT) */
|
|
|
|
XSTYPE_DATA = 0x10 /* A block of anonymous data */
|
2022-09-19 18:41:11 +00:00
|
|
|
} xstype;
|
|
|
|
|
|
|
|
|
|
|
|
/* dynamic strings */
|
|
|
|
typedef char d_char;
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
/* types */
|
|
|
|
typedef char xs_val;
|
|
|
|
typedef char xs_str;
|
|
|
|
typedef char xs_list;
|
|
|
|
typedef char xs_dict;
|
|
|
|
typedef char xs_number;
|
2023-02-06 10:09:11 +00:00
|
|
|
typedef char xs_data;
|
2023-01-28 16:49:02 +00:00
|
|
|
|
2022-09-19 18:41:11 +00:00
|
|
|
/* auto-destroyable strings */
|
2023-01-28 16:49:02 +00:00
|
|
|
#define xs __attribute__ ((__cleanup__ (_xs_destroy))) xs_val
|
2022-09-19 18:41:11 +00:00
|
|
|
|
2022-11-24 07:47:02 +00:00
|
|
|
/* not really all, just very much */
|
|
|
|
#define XS_ALL 0xfffffff
|
|
|
|
|
2022-10-25 07:32:41 +00:00
|
|
|
void *xs_free(void *ptr);
|
2022-10-25 08:39:30 +00:00
|
|
|
void *_xs_realloc(void *ptr, size_t size, const char *file, int line, const char *func);
|
|
|
|
#define xs_realloc(ptr, size) _xs_realloc(ptr, size, __FILE__, __LINE__, __FUNCTION__)
|
2022-10-22 03:59:55 +00:00
|
|
|
int _xs_blk_size(int sz);
|
2022-09-19 20:41:30 +00:00
|
|
|
void _xs_destroy(char **var);
|
2022-09-29 10:52:57 +00:00
|
|
|
#define xs_debug() raise(SIGTRAP)
|
2023-01-28 16:49:02 +00:00
|
|
|
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);
|
2022-09-19 18:41:11 +00:00
|
|
|
#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)
|
2023-01-28 16:49:02 +00:00
|
|
|
|
|
|
|
xs_str *xs_str_new(const char *str);
|
|
|
|
xs_str *xs_str_wrap_i(const char *prefix, xs_str *str, const char *suffix);
|
2023-01-17 08:50:16 +00:00
|
|
|
#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)
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_str *xs_replace_i(xs_str *str, const char *sfrom, const char *sto);
|
2022-09-27 08:14:29 +00:00
|
|
|
#define xs_replace(str, sfrom, sto) xs_replace_i(xs_dup(str), sfrom, sto)
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_str *xs_fmt(const char *fmt, ...);
|
2022-10-08 05:00:05 +00:00
|
|
|
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);
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_str *xs_crop_i(xs_str *str, int start, int end);
|
|
|
|
xs_str *xs_strip_chars_i(xs_str *str, const char *chars);
|
2023-01-12 08:28:02 +00:00
|
|
|
#define xs_strip_i(str) xs_strip_chars_i(str, " \r\n\t\v\f")
|
2023-01-28 16:49:02 +00:00
|
|
|
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);
|
2022-09-19 18:41:11 +00:00
|
|
|
#define xs_list_append(list, data) xs_list_append_m(list, data, xs_size(data))
|
2023-01-28 16:49:02 +00:00
|
|
|
int xs_list_iter(xs_list **list, xs_val **value);
|
2023-02-02 02:37:17 +00:00
|
|
|
int xs_list_len(const xs_list *list);
|
|
|
|
char *xs_list_get(const xs_list *list, int num);
|
2023-01-28 16:49:02 +00:00
|
|
|
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);
|
2023-01-24 13:57:04 +00:00
|
|
|
#define xs_list_pop(list, data) xs_list_dequeue(list, data, 1)
|
|
|
|
#define xs_list_shift(list, data) xs_list_dequeue(list, data, 0)
|
2023-02-02 02:37:17 +00:00
|
|
|
int xs_list_in(const xs_list *list, const xs_val *val);
|
|
|
|
xs_str *xs_join(const xs_list *list, const char *sep);
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_list *xs_split_n(const char *str, const char *sep, int times);
|
2022-11-24 07:47:02 +00:00
|
|
|
#define xs_split(str, sep) xs_split_n(str, sep, XS_ALL)
|
2023-01-28 16:49:02 +00:00
|
|
|
|
|
|
|
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);
|
2022-09-19 18:41:11 +00:00
|
|
|
#define xs_dict_append(dict, key, data) xs_dict_append_m(dict, key, data, xs_size(data))
|
2023-02-16 08:32:42 +00:00
|
|
|
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))
|
2023-01-28 16:49:02 +00:00
|
|
|
int xs_dict_iter(xs_dict **dict, xs_str **key, xs_val **value);
|
2023-02-02 02:37:17 +00:00
|
|
|
xs_dict *xs_dict_get(const xs_dict *dict, const xs_str *key);
|
2023-01-28 16:49:02 +00:00
|
|
|
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);
|
2022-09-19 18:41:11 +00:00
|
|
|
|
2023-02-06 10:09:11 +00:00
|
|
|
xs_data *xs_data_new(const void *data, int size);
|
|
|
|
int xs_data_size(const xs_data *value);
|
|
|
|
void xs_data_get(const xs_data *value, void *data);
|
|
|
|
|
2022-12-11 08:46:27 +00:00
|
|
|
void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size);
|
2022-09-19 18:41:11 +00:00
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
|
|
|
|
#ifdef XS_ASSERT
|
|
|
|
#include <assert.h>
|
|
|
|
#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
|
|
|
|
|
|
|
|
|
2022-09-19 18:41:11 +00:00
|
|
|
#ifdef XS_IMPLEMENTATION
|
|
|
|
|
2022-10-25 08:39:30 +00:00
|
|
|
void *_xs_realloc(void *ptr, size_t size, const char *file, int line, const char *func)
|
2022-10-25 07:32:41 +00:00
|
|
|
{
|
|
|
|
d_char *ndata = realloc(ptr, size);
|
|
|
|
|
|
|
|
if (ndata == NULL) {
|
|
|
|
fprintf(stderr, "**OUT OF MEMORY**\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XS_DEBUG
|
|
|
|
if (ndata != ptr) {
|
2022-10-25 11:59:15 +00:00
|
|
|
int n;
|
2022-10-25 07:32:41 +00:00
|
|
|
FILE *f = fopen("xs_memory.out", "a");
|
|
|
|
|
|
|
|
if (ptr != NULL)
|
2022-10-25 08:39:30 +00:00
|
|
|
fprintf(f, "%p r\n", ptr);
|
2022-10-25 07:32:41 +00:00
|
|
|
|
2022-10-25 11:59:15 +00:00
|
|
|
fprintf(f, "%p a %ld %s:%d: %s", ndata, size, file, line, func);
|
|
|
|
|
|
|
|
if (ptr != NULL) {
|
|
|
|
fprintf(f, " [");
|
|
|
|
for (n = 0; n < 32 && ndata[n]; n++) {
|
|
|
|
if (ndata[n] >= 32 && ndata[n] <= 127)
|
|
|
|
fprintf(f, "%c", ndata[n]);
|
|
|
|
else
|
|
|
|
fprintf(f, "\\%02x", (unsigned char)ndata[n]);
|
|
|
|
}
|
|
|
|
fprintf(f, "]");
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(f, "\n");
|
|
|
|
|
2022-10-25 07:32:41 +00:00
|
|
|
fclose(f);
|
|
|
|
}
|
2023-01-08 09:39:11 +00:00
|
|
|
#else
|
|
|
|
(void)file;
|
|
|
|
(void)line;
|
|
|
|
(void)func;
|
2022-10-25 07:32:41 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return ndata;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *xs_free(void *ptr)
|
|
|
|
{
|
|
|
|
#ifdef XS_DEBUG
|
|
|
|
if (ptr != NULL) {
|
|
|
|
FILE *f = fopen("xs_memory.out", "a");
|
|
|
|
fprintf(f, "%p b\n", ptr);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
free(ptr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
void _xs_destroy(char **var)
|
|
|
|
{
|
2022-10-25 07:32:41 +00:00
|
|
|
/*
|
2022-09-19 18:41:11 +00:00
|
|
|
if (_xs_debug)
|
|
|
|
printf("_xs_destroy %p\n", var);
|
2022-10-25 07:32:41 +00:00
|
|
|
*/
|
|
|
|
xs_free(*var);
|
2022-09-19 18:41:11 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 03:59:55 +00:00
|
|
|
|
|
|
|
int _xs_blk_size(int sz)
|
|
|
|
/* calculates the block size */
|
|
|
|
{
|
|
|
|
int blk_size = 4096;
|
|
|
|
|
|
|
|
if (sz < 256)
|
|
|
|
blk_size = 32;
|
|
|
|
else
|
|
|
|
if (sz < 4096)
|
|
|
|
blk_size = 256;
|
|
|
|
|
|
|
|
return ((((sz) + blk_size) / blk_size) * blk_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xstype xs_type(const xs_val *data)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* return the type of data */
|
|
|
|
{
|
|
|
|
xstype t;
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
if (data == NULL)
|
|
|
|
t = XSTYPE_NULL;
|
|
|
|
else
|
2022-09-19 18:41:11 +00:00
|
|
|
switch (data[0]) {
|
|
|
|
case XSTYPE_NULL:
|
|
|
|
case XSTYPE_TRUE:
|
|
|
|
case XSTYPE_FALSE:
|
2022-09-25 20:57:18 +00:00
|
|
|
case XSTYPE_LIST:
|
2022-09-19 18:41:11 +00:00
|
|
|
case XSTYPE_LITEM:
|
2023-01-28 16:49:02 +00:00
|
|
|
case XSTYPE_DICT:
|
2022-09-19 18:41:11 +00:00
|
|
|
case XSTYPE_DITEM:
|
|
|
|
case XSTYPE_NUMBER:
|
2023-01-28 16:49:02 +00:00
|
|
|
case XSTYPE_EOM:
|
2023-02-06 10:09:11 +00:00
|
|
|
case XSTYPE_DATA:
|
2022-09-19 18:41:11 +00:00
|
|
|
t = data[0];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
t = XSTYPE_STRING;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
void _xs_put_24b(xs_val *ptr, int i)
|
2022-12-06 18:12:26 +00:00
|
|
|
/* writes i as a 24 bit value */
|
|
|
|
{
|
|
|
|
unsigned char *p = (unsigned char *)ptr;
|
|
|
|
|
|
|
|
p[0] = (i >> 16) & 0xff;
|
|
|
|
p[1] = (i >> 8) & 0xff;
|
|
|
|
p[2] = i & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
int _xs_get_24b(const xs_val *ptr)
|
2022-12-06 18:12:26 +00:00
|
|
|
/* reads a 24 bit value */
|
|
|
|
{
|
|
|
|
unsigned char *p = (unsigned char *)ptr;
|
|
|
|
|
|
|
|
return (p[0] << 16) | (p[1] << 8) | p[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
int xs_size(const xs_val *data)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* returns the size of data in bytes */
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
if (data == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (xs_type(data)) {
|
|
|
|
case XSTYPE_STRING:
|
|
|
|
len = strlen(data) + 1;
|
|
|
|
break;
|
|
|
|
|
2022-09-25 20:57:18 +00:00
|
|
|
case XSTYPE_LIST:
|
|
|
|
case XSTYPE_DICT:
|
2023-02-06 10:09:11 +00:00
|
|
|
case XSTYPE_DATA:
|
2022-12-06 18:12:26 +00:00
|
|
|
len = _xs_get_24b(data + 1);
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XSTYPE_DITEM:
|
|
|
|
/* calculate the size of the key and the value */
|
|
|
|
p = data + 1;
|
|
|
|
p += xs_size(p);
|
|
|
|
p += xs_size(p);
|
|
|
|
|
|
|
|
len = p - data;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XSTYPE_LITEM:
|
|
|
|
/* it's the size of the item + 1 */
|
|
|
|
p = data + 1;
|
|
|
|
p += xs_size(p);
|
|
|
|
|
|
|
|
len = p - data;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XSTYPE_NUMBER:
|
2022-10-07 13:06:17 +00:00
|
|
|
len = 1 + xs_size(data + 1);
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
len = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
int xs_is_null(const xs_val *data)
|
2022-09-25 07:07:43 +00:00
|
|
|
/* checks for null */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
return (xs_type(data) == XSTYPE_NULL);
|
2022-09-25 07:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_val *xs_dup(const xs_val *data)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* creates a duplicate of data */
|
|
|
|
{
|
|
|
|
int sz = xs_size(data);
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_val *s = xs_realloc(NULL, _xs_blk_size(sz));
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
memcpy(s, data, sz);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_val *xs_expand(xs_val *data, int offset, int size)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* opens a hole in data */
|
|
|
|
{
|
|
|
|
int sz = xs_size(data);
|
|
|
|
|
|
|
|
/* open room */
|
2022-10-07 11:48:53 +00:00
|
|
|
if (sz == 0 || _xs_blk_size(sz) != _xs_blk_size(sz + size))
|
|
|
|
data = xs_realloc(data, _xs_blk_size(sz + size));
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
/* move up the rest of the data */
|
2022-10-07 11:48:53 +00:00
|
|
|
if (data != NULL)
|
|
|
|
memmove(data + offset + size, data + offset, sz - offset);
|
2022-09-19 18:41:11 +00:00
|
|
|
|
2023-02-06 10:09:11 +00:00
|
|
|
if (xs_type(data) == XSTYPE_LIST ||
|
|
|
|
xs_type(data) == XSTYPE_DICT ||
|
|
|
|
xs_type(data) == XSTYPE_DATA)
|
2022-12-06 18:12:26 +00:00
|
|
|
_xs_put_24b(data + 1, sz + size);
|
|
|
|
|
2022-09-19 18:41:11 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_val *xs_collapse(xs_val *data, int offset, int size)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* shrinks data */
|
|
|
|
{
|
|
|
|
int sz = xs_size(data);
|
|
|
|
int n;
|
|
|
|
|
|
|
|
/* don't try to delete beyond the limit */
|
|
|
|
if (offset + size > sz)
|
|
|
|
size = sz - offset;
|
|
|
|
|
|
|
|
/* shrink total size */
|
|
|
|
sz -= size;
|
|
|
|
|
|
|
|
for (n = offset; n < sz; n++)
|
|
|
|
data[n] = data[n + size];
|
|
|
|
|
2023-02-06 10:09:11 +00:00
|
|
|
if (xs_type(data) == XSTYPE_LIST ||
|
|
|
|
xs_type(data) == XSTYPE_DICT ||
|
|
|
|
xs_type(data) == XSTYPE_DATA)
|
2022-12-06 18:12:26 +00:00
|
|
|
_xs_put_24b(data + 1, sz);
|
|
|
|
|
2022-10-07 11:48:53 +00:00
|
|
|
return xs_realloc(data, _xs_blk_size(sz));
|
2022-09-19 18:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_val *xs_insert_m(xs_val *data, int offset, const char *mem, int size)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* inserts a memory block */
|
|
|
|
{
|
|
|
|
data = xs_expand(data, offset, size);
|
|
|
|
memcpy(data + offset, mem, size);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** strings **/
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_str *xs_str_new(const char *str)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* creates a new string */
|
|
|
|
{
|
|
|
|
return xs_insert(NULL, 0, str ? str : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_str *xs_str_wrap_i(const char *prefix, xs_str *str, const char *suffix)
|
2023-01-17 08:50:16 +00:00
|
|
|
/* wraps str with prefix and suffix */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE(str, XSTYPE_STRING);
|
|
|
|
|
2023-01-17 08:50:16 +00:00
|
|
|
if (prefix)
|
|
|
|
str = xs_insert_m(str, 0, prefix, strlen(prefix));
|
|
|
|
|
|
|
|
if (suffix)
|
|
|
|
str = xs_insert_m(str, xs_size(str) - 1, suffix, xs_size(suffix));
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_str *xs_replace_i(xs_str *str, const char *sfrom, const char *sto)
|
2022-09-27 08:14:29 +00:00
|
|
|
/* replaces inline all sfrom with sto */
|
2022-09-19 18:41:11 +00:00
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE(str, XSTYPE_STRING);
|
|
|
|
|
2022-09-27 08:14:29 +00:00
|
|
|
int sfsz = strlen(sfrom);
|
|
|
|
int stsz = strlen(sto);
|
2022-09-19 18:41:11 +00:00
|
|
|
char *ss;
|
2022-09-27 08:14:29 +00:00
|
|
|
int offset = 0;
|
2022-09-19 18:41:11 +00:00
|
|
|
|
2022-09-27 08:14:29 +00:00
|
|
|
while ((ss = strstr(str + offset, sfrom)) != NULL) {
|
|
|
|
int n_offset = ss - str;
|
2022-09-19 18:41:11 +00:00
|
|
|
|
2022-09-27 08:14:29 +00:00
|
|
|
str = xs_collapse(str, n_offset, sfsz);
|
|
|
|
str = xs_expand(str, n_offset, stsz);
|
|
|
|
memcpy(str + n_offset, sto, stsz);
|
2022-09-19 18:41:11 +00:00
|
|
|
|
2022-09-27 08:50:32 +00:00
|
|
|
offset = n_offset + stsz;
|
2022-09-19 18:41:11 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 08:14:29 +00:00
|
|
|
return str;
|
2022-09-19 18:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_str *xs_fmt(const char *fmt, ...)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* formats a string with printf()-like marks */
|
|
|
|
{
|
|
|
|
int n;
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_str *s = NULL;
|
2022-09-19 18:41:11 +00:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
n = vsnprintf(s, 0, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (n > 0) {
|
2022-10-25 07:32:41 +00:00
|
|
|
s = xs_realloc(NULL, _xs_blk_size(n + 1));
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2022-10-25 07:32:41 +00:00
|
|
|
vsnprintf(s, n + 1, fmt, ap);
|
2022-09-19 18:41:11 +00:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-08 05:00:05 +00:00
|
|
|
int xs_str_in(const char *haystack, const char *needle)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* finds needle in haystack and returns the offset or -1 */
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int r = -1;
|
|
|
|
|
|
|
|
if ((s = strstr(haystack, needle)) != NULL)
|
|
|
|
r = s - haystack;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-08 05:00:05 +00:00
|
|
|
int xs_startswith(const char *str, const char *prefix)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* returns true if str starts with prefix */
|
|
|
|
{
|
|
|
|
return !!(xs_str_in(str, prefix) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-08 05:00:05 +00:00
|
|
|
int xs_endswith(const char *str, const char *postfix)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* returns true if str ends with postfix */
|
|
|
|
{
|
|
|
|
int ssz = strlen(str);
|
|
|
|
int psz = strlen(postfix);
|
|
|
|
|
|
|
|
return !!(ssz >= psz && memcmp(postfix, str + ssz - psz, psz) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_str *xs_crop_i(xs_str *str, int start, int end)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* crops the d_char to be only from start to end */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE(str, XSTYPE_STRING);
|
|
|
|
|
2022-09-19 18:41:11 +00:00
|
|
|
int sz = strlen(str);
|
|
|
|
|
|
|
|
if (end <= 0)
|
|
|
|
end = sz + end;
|
|
|
|
|
|
|
|
/* crop from the top */
|
|
|
|
str[end] = '\0';
|
|
|
|
|
|
|
|
/* crop from the bottom */
|
|
|
|
str = xs_collapse(str, 0, start);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_str *xs_strip_chars_i(xs_str *str, const char *chars)
|
2023-01-08 17:50:37 +00:00
|
|
|
/* strips the string of chars from the start and the end */
|
2022-09-19 18:41:11 +00:00
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE(str, XSTYPE_STRING);
|
|
|
|
|
2023-01-10 07:45:36 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
/* strip first from the end */
|
|
|
|
for (n = strlen(str); n > 0 && strchr(chars, str[n - 1]); n--);
|
|
|
|
str[n] = '\0';
|
2022-09-19 18:41:11 +00:00
|
|
|
|
2023-01-10 07:45:36 +00:00
|
|
|
if (str[0]) {
|
|
|
|
/* now strip from the beginning */
|
|
|
|
for (n = 0; str[n] && strchr(chars, str[n]); n++);
|
2022-09-19 18:41:11 +00:00
|
|
|
|
2023-01-10 07:45:36 +00:00
|
|
|
if (n)
|
|
|
|
str = xs_collapse(str, 0, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
2022-09-19 18:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_str *xs_tolower_i(xs_str *str)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* convert to lowercase */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE(str, XSTYPE_STRING);
|
|
|
|
|
2022-09-19 18:41:11 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
for (n = 0; str[n]; n++)
|
|
|
|
str[n] = tolower(str[n]);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** lists **/
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_list *xs_list_new(void)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* creates a new list */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_list *list;
|
2022-09-19 18:41:11 +00:00
|
|
|
|
2022-12-06 18:12:26 +00:00
|
|
|
list = xs_realloc(NULL, _xs_blk_size(5));
|
2022-09-25 20:57:18 +00:00
|
|
|
list[0] = XSTYPE_LIST;
|
2023-01-28 16:49:02 +00:00
|
|
|
list[4] = XSTYPE_EOM;
|
2022-12-06 18:12:26 +00:00
|
|
|
|
|
|
|
_xs_put_24b(list + 1, 5);
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_list *_xs_list_write_litem(xs_list *list, int offset, const char *mem, int dsz)
|
2022-11-20 09:57:05 +00:00
|
|
|
/* writes a list item */
|
2022-09-19 18:41:11 +00:00
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE(list, XSTYPE_LIST);
|
|
|
|
|
2022-09-19 18:41:11 +00:00
|
|
|
char c = XSTYPE_LITEM;
|
|
|
|
|
2022-11-20 09:57:05 +00:00
|
|
|
list = xs_insert_m(list, offset, &c, 1);
|
|
|
|
list = xs_insert_m(list, offset + 1, mem, dsz);
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_list *xs_list_append_m(xs_list *list, const char *mem, int dsz)
|
2022-11-20 09:57:05 +00:00
|
|
|
/* adds a memory block to the list */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE(list, XSTYPE_LIST);
|
|
|
|
|
2022-11-20 09:57:05 +00:00
|
|
|
return _xs_list_write_litem(list, xs_size(list) - 1, mem, dsz);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
int xs_list_iter(xs_list **list, xs_val **value)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* iterates a list value */
|
|
|
|
{
|
|
|
|
int goon = 1;
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_val *p = *list;
|
2022-09-19 18:41:11 +00:00
|
|
|
|
2022-12-06 18:12:26 +00:00
|
|
|
/* skip the start of the list */
|
2023-01-28 16:49:02 +00:00
|
|
|
if (xs_type(p) == XSTYPE_LIST)
|
2022-12-06 18:12:26 +00:00
|
|
|
p += 4;
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
/* an element? */
|
2023-01-28 16:49:02 +00:00
|
|
|
if (xs_type(p) == XSTYPE_LITEM) {
|
2022-09-19 18:41:11 +00:00
|
|
|
p++;
|
|
|
|
|
|
|
|
*value = p;
|
|
|
|
|
|
|
|
p += xs_size(*value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* end of list */
|
|
|
|
goon = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* store back the pointer */
|
|
|
|
*list = p;
|
|
|
|
|
|
|
|
return goon;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-02 02:37:17 +00:00
|
|
|
int xs_list_len(const xs_list *list)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* returns the number of elements in the list */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE_NULL(list, XSTYPE_LIST);
|
|
|
|
|
2022-09-19 18:41:11 +00:00
|
|
|
int c = 0;
|
2023-02-02 02:37:17 +00:00
|
|
|
xs_list *p = (xs_list *)list;
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_val *v;
|
2022-09-19 18:41:11 +00:00
|
|
|
|
2023-02-02 02:37:17 +00:00
|
|
|
while (xs_list_iter(&p, &v))
|
2022-09-19 18:41:11 +00:00
|
|
|
c++;
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-02 02:37:17 +00:00
|
|
|
xs_val *xs_list_get(const xs_list *list, int num)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* returns the element #num */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE(list, XSTYPE_LIST);
|
|
|
|
|
2022-09-19 18:41:11 +00:00
|
|
|
if (num < 0)
|
|
|
|
num = xs_list_len(list) + num;
|
|
|
|
|
2023-02-02 02:37:17 +00:00
|
|
|
int c = 0;
|
|
|
|
xs_list *p = (xs_list *)list;
|
|
|
|
xs_val *v;
|
|
|
|
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
2022-10-22 03:48:44 +00:00
|
|
|
if (c == num)
|
|
|
|
return v;
|
|
|
|
|
2022-09-19 18:41:11 +00:00
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
2022-10-22 03:48:44 +00:00
|
|
|
return NULL;
|
2022-09-19 18:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_list *xs_list_del(xs_list *list, int num)
|
2022-11-20 09:57:05 +00:00
|
|
|
/* deletes element #num */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE(list, XSTYPE_LIST);
|
|
|
|
|
|
|
|
xs_val *v;
|
2022-11-20 09:57:05 +00:00
|
|
|
|
|
|
|
if ((v = xs_list_get(list, num)) != NULL)
|
|
|
|
list = xs_collapse(list, v - 1 - list, xs_size(v - 1));
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_list *xs_list_insert(xs_list *list, int num, const xs_val *data)
|
2022-11-20 09:57:05 +00:00
|
|
|
/* inserts an element at #num position */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE(list, XSTYPE_LIST);
|
|
|
|
|
|
|
|
xs_val *v;
|
2022-11-20 09:57:05 +00:00
|
|
|
int offset;
|
|
|
|
|
|
|
|
if ((v = xs_list_get(list, num)) != NULL)
|
|
|
|
offset = v - list;
|
|
|
|
else
|
|
|
|
offset = xs_size(list);
|
|
|
|
|
|
|
|
return _xs_list_write_litem(list, offset - 1, data, xs_size(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_list *xs_list_insert_sorted(xs_list *list, const xs_str *str)
|
2022-11-20 09:57:05 +00:00
|
|
|
/* inserts a string in the list in its ordered position */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE(list, XSTYPE_LIST);
|
|
|
|
XS_ASSERT_TYPE(str, XSTYPE_STRING);
|
|
|
|
|
2022-11-20 09:57:05 +00:00
|
|
|
char *p, *v;
|
|
|
|
int offset = xs_size(list);
|
|
|
|
|
|
|
|
p = list;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
/* if this element is greater or equal, insert here */
|
|
|
|
if (strcmp(v, str) >= 0) {
|
|
|
|
offset = v - list;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _xs_list_write_litem(list, offset - 1, str, xs_size(str));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_list *xs_list_set(xs_list *list, int num, const xs_val *data)
|
2022-11-20 09:57:05 +00:00
|
|
|
/* sets the element at #num position */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE(list, XSTYPE_LIST);
|
|
|
|
|
2022-11-20 09:57:05 +00:00
|
|
|
list = xs_list_del(list, num);
|
|
|
|
list = xs_list_insert(list, num, data);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_list *xs_list_dequeue(xs_list *list, xs_val **data, int last)
|
2023-01-24 13:57:04 +00:00
|
|
|
/* gets a copy of the first or last element of a list, shrinking it */
|
2022-11-20 09:57:05 +00:00
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE(list, XSTYPE_LIST);
|
|
|
|
|
|
|
|
xs_list *p = list;
|
|
|
|
xs_val *v = NULL;
|
2023-01-17 08:50:16 +00:00
|
|
|
|
2023-01-24 13:57:04 +00:00
|
|
|
if (!last) {
|
|
|
|
/* get the first */
|
|
|
|
xs_list_iter(&p, &v);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* iterate to the end */
|
|
|
|
while (xs_list_iter(&p, &v));
|
|
|
|
}
|
2023-01-17 08:50:16 +00:00
|
|
|
|
|
|
|
if (v != NULL) {
|
|
|
|
*data = xs_dup(v);
|
|
|
|
|
|
|
|
/* collapse from the address of the element */
|
|
|
|
list = xs_collapse(list, v - 1 - list, xs_size(v - 1));
|
2022-11-20 09:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-02 02:37:17 +00:00
|
|
|
int xs_list_in(const xs_list *list, const xs_val *val)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* returns the position of val in list or -1 */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE_NULL(list, XSTYPE_LIST);
|
|
|
|
|
2022-09-19 18:41:11 +00:00
|
|
|
int n = 0;
|
2023-02-02 02:37:17 +00:00
|
|
|
xs_list *p = (xs_list *)list;
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_val *v;
|
2022-09-19 18:41:11 +00:00
|
|
|
int sz = xs_size(val);
|
|
|
|
|
2023-02-02 02:37:17 +00:00
|
|
|
while (xs_list_iter(&p, &v)) {
|
2022-10-22 03:48:44 +00:00
|
|
|
if (sz == xs_size(v) && memcmp(val, v, sz) == 0)
|
|
|
|
return n;
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
2022-10-22 03:48:44 +00:00
|
|
|
return -1;
|
2022-09-19 18:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-02 02:37:17 +00:00
|
|
|
xs_str *xs_join(const xs_list *list, const char *sep)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* joins a list into a string */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE(list, XSTYPE_LIST);
|
|
|
|
|
|
|
|
xs_str *s = NULL;
|
2023-02-02 02:37:17 +00:00
|
|
|
xs_list *p = (xs_list *)list;
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_val *v;
|
2022-09-19 18:41:11 +00:00
|
|
|
int c = 0;
|
2023-01-08 09:39:11 +00:00
|
|
|
int offset = 0;
|
|
|
|
int ssz = strlen(sep);
|
2022-09-19 18:41:11 +00:00
|
|
|
|
2023-02-02 02:37:17 +00:00
|
|
|
while (xs_list_iter(&p, &v)) {
|
2022-09-19 18:41:11 +00:00
|
|
|
/* refuse to join non-string values */
|
|
|
|
if (xs_type(v) == XSTYPE_STRING) {
|
2023-01-08 09:39:11 +00:00
|
|
|
int sz;
|
|
|
|
|
2022-09-19 18:41:11 +00:00
|
|
|
/* add the separator */
|
2023-01-08 09:39:11 +00:00
|
|
|
if (c != 0) {
|
|
|
|
s = xs_realloc(s, offset + ssz);
|
|
|
|
memcpy(s + offset, sep, ssz);
|
|
|
|
offset += ssz;
|
|
|
|
}
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
/* add the element */
|
2023-01-08 09:39:11 +00:00
|
|
|
sz = strlen(v);
|
|
|
|
s = xs_realloc(s, offset + sz);
|
|
|
|
memcpy(s + offset, v, sz);
|
|
|
|
offset += sz;
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-08 09:39:11 +00:00
|
|
|
/* null-terminate */
|
|
|
|
s = xs_realloc(s, _xs_blk_size(offset + 1));
|
|
|
|
s[offset] = '\0';
|
|
|
|
|
2022-09-19 18:41:11 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_list *xs_split_n(const char *str, const char *sep, int times)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* splits a string into a list upto n times */
|
|
|
|
{
|
|
|
|
int sz = strlen(sep);
|
|
|
|
char *ss;
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_list *list;
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
list = xs_list_new();
|
|
|
|
|
|
|
|
while (times > 0 && (ss = strstr(str, sep)) != NULL) {
|
|
|
|
/* add the first part (without the asciiz) */
|
|
|
|
list = xs_list_append_m(list, str, ss - str);
|
|
|
|
|
|
|
|
/* add the asciiz */
|
2023-01-28 16:49:02 +00:00
|
|
|
list = xs_insert_m(list, xs_size(list) - 1, "", 1);
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
/* skip past the separator */
|
|
|
|
str = ss + sz;
|
|
|
|
|
|
|
|
times--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add the rest of the string */
|
|
|
|
list = xs_list_append(list, str);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** dicts **/
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_dict *xs_dict_new(void)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* creates a new dict */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_dict *dict;
|
2022-09-19 18:41:11 +00:00
|
|
|
|
2022-12-06 18:12:26 +00:00
|
|
|
dict = xs_realloc(NULL, _xs_blk_size(5));
|
2022-09-25 20:57:18 +00:00
|
|
|
dict[0] = XSTYPE_DICT;
|
2023-01-28 16:49:02 +00:00
|
|
|
dict[4] = XSTYPE_EOM;
|
2022-12-06 18:12:26 +00:00
|
|
|
|
|
|
|
_xs_put_24b(dict + 1, 5);
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-16 08:32:42 +00:00
|
|
|
xs_dict *xs_dict_insert_m(xs_dict *dict, int offset, const xs_str *key,
|
|
|
|
const xs_val *data, int dsz)
|
|
|
|
/* inserts a memory block into the dict */
|
2022-09-19 18:41:11 +00:00
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE(dict, XSTYPE_DICT);
|
|
|
|
XS_ASSERT_TYPE(key, XSTYPE_STRING);
|
|
|
|
|
2022-09-19 18:41:11 +00:00
|
|
|
int ksz = xs_size(key);
|
|
|
|
|
2023-02-16 08:32:42 +00:00
|
|
|
dict = xs_expand(dict, offset, 1 + ksz + dsz);
|
|
|
|
|
|
|
|
dict[offset] = XSTYPE_DITEM;
|
|
|
|
memcpy(&dict[offset + 1], key, ksz);
|
|
|
|
memcpy(&dict[offset + 1 + ksz], data, dsz);
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-16 08:32:42 +00:00
|
|
|
xs_dict *xs_dict_append_m(xs_dict *dict, const xs_str *key, const xs_val *mem, int dsz)
|
|
|
|
/* appends a memory block to the dict */
|
|
|
|
{
|
|
|
|
return xs_dict_insert_m(dict, xs_size(dict) - 1, key, mem, dsz);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
xs_dict *xs_dict_prepend_m(xs_dict *dict, const xs_str *key, const xs_val *mem, int dsz)
|
|
|
|
/* prepends a memory block to the dict */
|
|
|
|
{
|
|
|
|
return xs_dict_insert_m(dict, 4, key, mem, dsz);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
int xs_dict_iter(xs_dict **dict, xs_str **key, xs_val **value)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* iterates a dict value */
|
|
|
|
{
|
|
|
|
int goon = 1;
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_val *p = *dict;
|
2022-09-19 18:41:11 +00:00
|
|
|
|
2022-12-06 18:12:26 +00:00
|
|
|
/* skip the start of the list */
|
2023-01-28 16:49:02 +00:00
|
|
|
if (xs_type(p) == XSTYPE_DICT)
|
2022-12-06 18:12:26 +00:00
|
|
|
p += 4;
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
/* an element? */
|
2023-01-28 16:49:02 +00:00
|
|
|
if (xs_type(p) == XSTYPE_DITEM) {
|
2022-09-19 18:41:11 +00:00
|
|
|
p++;
|
|
|
|
|
|
|
|
*key = p;
|
|
|
|
p += xs_size(*key);
|
|
|
|
|
|
|
|
*value = p;
|
|
|
|
p += xs_size(*value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* end of list */
|
|
|
|
goon = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* store back the pointer */
|
|
|
|
*dict = p;
|
|
|
|
|
|
|
|
return goon;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-02 02:37:17 +00:00
|
|
|
xs_val *xs_dict_get(const xs_dict *dict, const xs_str *key)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* returns the value directed by key */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE(dict, XSTYPE_DICT);
|
|
|
|
XS_ASSERT_TYPE(key, XSTYPE_STRING);
|
|
|
|
|
2023-02-02 02:37:17 +00:00
|
|
|
xs_dict *p = (xs_dict *)dict;
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_str *k;
|
|
|
|
xs_val *v;
|
2022-09-19 18:41:11 +00:00
|
|
|
|
2023-02-02 02:37:17 +00:00
|
|
|
while (xs_dict_iter(&p, &k, &v)) {
|
2022-10-22 03:48:44 +00:00
|
|
|
if (strcmp(k, key) == 0)
|
|
|
|
return v;
|
2022-09-19 18:41:11 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 03:48:44 +00:00
|
|
|
return NULL;
|
2022-09-19 18:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_dict *xs_dict_del(xs_dict *dict, const xs_str *key)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* deletes a key */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE(dict, XSTYPE_DICT);
|
|
|
|
XS_ASSERT_TYPE(key, XSTYPE_STRING);
|
|
|
|
|
|
|
|
xs_str *k;
|
|
|
|
xs_val *v;
|
|
|
|
xs_dict *p = dict;
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
while (xs_dict_iter(&p, &k, &v)) {
|
|
|
|
if (strcmp(k, key) == 0) {
|
|
|
|
/* the address of the item is just behind the key */
|
|
|
|
char *i = k - 1;
|
|
|
|
|
|
|
|
dict = xs_collapse(dict, i - dict, xs_size(i));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_dict *xs_dict_set(xs_dict *dict, const xs_str *key, const xs_val *data)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* sets (replaces) a key */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
XS_ASSERT_TYPE(dict, XSTYPE_DICT);
|
|
|
|
XS_ASSERT_TYPE(key, XSTYPE_STRING);
|
|
|
|
|
2022-09-19 18:41:11 +00:00
|
|
|
/* delete the possibly existing key */
|
|
|
|
dict = xs_dict_del(dict, key);
|
|
|
|
|
2023-02-16 08:32:42 +00:00
|
|
|
/* add the data */
|
|
|
|
dict = xs_dict_prepend(dict, key, data);
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** other values **/
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_val *xs_val_new(xstype t)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* adds a new special value */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_val *v = xs_realloc(NULL, _xs_blk_size(1));
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
v[0] = t;
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-07 13:06:17 +00:00
|
|
|
/** numbers */
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_number *xs_number_new(double f)
|
2022-09-19 18:41:11 +00:00
|
|
|
/* adds a new number value */
|
|
|
|
{
|
2023-01-28 16:49:02 +00:00
|
|
|
xs_number *v;
|
2022-10-07 13:06:17 +00:00
|
|
|
char tmp[64];
|
|
|
|
|
|
|
|
snprintf(tmp, sizeof(tmp), "%.15lf", f);
|
|
|
|
|
|
|
|
/* strip useless zeros */
|
|
|
|
if (strchr(tmp, '.') != NULL) {
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
for (ptr = tmp + strlen(tmp) - 1; *ptr == '0'; ptr--);
|
|
|
|
|
|
|
|
if (*ptr != '.')
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
*ptr = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* alloc for the marker and the full string */
|
|
|
|
v = xs_realloc(NULL, _xs_blk_size(1 + xs_size(tmp)));
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
v[0] = XSTYPE_NUMBER;
|
2022-10-07 13:06:17 +00:00
|
|
|
memcpy(&v[1], tmp, xs_size(tmp));
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
double xs_number_get(const xs_number *v)
|
2022-10-07 13:06:17 +00:00
|
|
|
/* gets the number as a double */
|
2022-09-19 18:41:11 +00:00
|
|
|
{
|
2022-10-07 13:06:17 +00:00
|
|
|
double f = 0.0;
|
2022-09-19 18:41:11 +00:00
|
|
|
|
2022-10-16 16:03:28 +00:00
|
|
|
if (v != NULL && v[0] == XSTYPE_NUMBER)
|
2022-10-07 13:06:17 +00:00
|
|
|
f = atof(&v[1]);
|
2022-09-19 18:41:11 +00:00
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 16:49:02 +00:00
|
|
|
const char *xs_number_str(const xs_number *v)
|
2022-10-07 13:06:17 +00:00
|
|
|
/* gets the number as a string */
|
|
|
|
{
|
2022-10-08 05:00:05 +00:00
|
|
|
const char *p = NULL;
|
2022-10-07 13:06:17 +00:00
|
|
|
|
2022-10-16 16:03:28 +00:00
|
|
|
if (v != NULL && v[0] == XSTYPE_NUMBER)
|
2022-10-07 13:06:17 +00:00
|
|
|
p = &v[1];
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-06 10:09:11 +00:00
|
|
|
/* raw data blocks */
|
|
|
|
|
|
|
|
xs_data *xs_data_new(const void *data, int size)
|
|
|
|
/* returns a new raw data value */
|
|
|
|
{
|
|
|
|
xs_data *v;
|
|
|
|
|
|
|
|
/* add the overhead (data type + 24bit size) */
|
|
|
|
size += 4;
|
|
|
|
|
|
|
|
v = xs_realloc(NULL, _xs_blk_size(size));
|
|
|
|
v[0] = XSTYPE_DATA;
|
|
|
|
|
|
|
|
_xs_put_24b(v + 1, size);
|
|
|
|
|
|
|
|
memcpy(&v[4], data, size);
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int xs_data_size(const xs_data *value)
|
|
|
|
/* returns the size of the data stored inside value */
|
|
|
|
{
|
|
|
|
return _xs_get_24b(value + 1) - 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void xs_data_get(const xs_data *value, void *data)
|
|
|
|
/* copies the raw data stored inside value into data */
|
|
|
|
{
|
|
|
|
int size = _xs_get_24b(value + 1) - 4;
|
|
|
|
memcpy(data, &value[4], size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-11 08:46:27 +00:00
|
|
|
void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size)
|
|
|
|
/* clone of memmem */
|
|
|
|
{
|
|
|
|
char *p, *r = NULL;
|
|
|
|
int offset = 0;
|
|
|
|
|
2022-12-11 09:33:01 +00:00
|
|
|
while (!r && h_size - offset > n_size &&
|
|
|
|
(p = memchr(haystack + offset, *needle, h_size - offset))) {
|
2022-12-11 08:46:27 +00:00
|
|
|
if (memcmp(p, needle, n_size) == 0)
|
|
|
|
r = p;
|
|
|
|
else
|
|
|
|
offset = p - haystack + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-19 18:41:11 +00:00
|
|
|
#endif /* XS_IMPLEMENTATION */
|
|
|
|
|
|
|
|
#endif /* _XS_H */
|