summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar default2023-02-06 11:09:11 +0100
committerGravatar default2023-02-06 11:09:11 +0100
commit2ee6bdc745735e11320dc248982c8d4ee92dcb71 (patch)
tree28c59272d3fd9d39326e5cd82aaad80044b66151
parentStarted work towards the pool of threads. (diff)
downloadsnac2-2ee6bdc745735e11320dc248982c8d4ee92dcb71.tar.gz
snac2-2ee6bdc745735e11320dc248982c8d4ee92dcb71.tar.xz
snac2-2ee6bdc745735e11320dc248982c8d4ee92dcb71.zip
Backport from xs.
-rw-r--r--xs.h60
-rw-r--r--xs_version.h2
2 files changed, 53 insertions, 9 deletions
diff --git a/xs.h b/xs.h
index 2be297b..ea3df1c 100644
--- a/xs.h
+++ b/xs.h
@@ -15,7 +15,7 @@
15 15
16typedef enum { 16typedef enum {
17 XSTYPE_STRING = 0x02, /* C string (\0 delimited) (NOT STORED) */ 17 XSTYPE_STRING = 0x02, /* C string (\0 delimited) (NOT STORED) */
18 XSTYPE_NUMBER = 0x17, /* C string (\0 delimited) */ 18 XSTYPE_NUMBER = 0x17, /* double in spirit, stored as a C string (\0 delimited) */
19 XSTYPE_NULL = 0x18, /* Special NULL value */ 19 XSTYPE_NULL = 0x18, /* Special NULL value */
20 XSTYPE_TRUE = 0x06, /* Boolean */ 20 XSTYPE_TRUE = 0x06, /* Boolean */
21 XSTYPE_FALSE = 0x15, /* Boolean */ 21 XSTYPE_FALSE = 0x15, /* Boolean */
@@ -23,7 +23,8 @@ typedef enum {
23 XSTYPE_LITEM = 0x1f, /* Element of a list (any type) */ 23 XSTYPE_LITEM = 0x1f, /* Element of a list (any type) */
24 XSTYPE_DICT = 0x1c, /* Sequence of DITEMs up to EOM (with 24bit size) */ 24 XSTYPE_DICT = 0x1c, /* Sequence of DITEMs up to EOM (with 24bit size) */
25 XSTYPE_DITEM = 0x1e, /* Element of a dict (STRING key + any type) */ 25 XSTYPE_DITEM = 0x1e, /* Element of a dict (STRING key + any type) */
26 XSTYPE_EOM = 0x19 /* End of Multiple (LIST or DICT) */ 26 XSTYPE_EOM = 0x19, /* End of Multiple (LIST or DICT) */
27 XSTYPE_DATA = 0x10 /* A block of anonymous data */
27} xstype; 28} xstype;
28 29
29 30
@@ -36,6 +37,7 @@ typedef char xs_str;
36typedef char xs_list; 37typedef char xs_list;
37typedef char xs_dict; 38typedef char xs_dict;
38typedef char xs_number; 39typedef char xs_number;
40typedef char xs_data;
39 41
40/* auto-destroyable strings */ 42/* auto-destroyable strings */
41#define xs __attribute__ ((__cleanup__ (_xs_destroy))) xs_val 43#define xs __attribute__ ((__cleanup__ (_xs_destroy))) xs_val
@@ -105,6 +107,10 @@ xs_number *xs_number_new(double f);
105double xs_number_get(const xs_number *v); 107double xs_number_get(const xs_number *v);
106const char *xs_number_str(const xs_number *v); 108const char *xs_number_str(const xs_number *v);
107 109
110xs_data *xs_data_new(const void *data, int size);
111int xs_data_size(const xs_data *value);
112void xs_data_get(const xs_data *value, void *data);
113
108void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size); 114void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size);
109 115
110 116
@@ -222,6 +228,7 @@ xstype xs_type(const xs_val *data)
222 case XSTYPE_DITEM: 228 case XSTYPE_DITEM:
223 case XSTYPE_NUMBER: 229 case XSTYPE_NUMBER:
224 case XSTYPE_EOM: 230 case XSTYPE_EOM:
231 case XSTYPE_DATA:
225 t = data[0]; 232 t = data[0];
226 break; 233 break;
227 default: 234 default:
@@ -268,11 +275,8 @@ int xs_size(const xs_val *data)
268 break; 275 break;
269 276
270 case XSTYPE_LIST: 277 case XSTYPE_LIST:
271 len = _xs_get_24b(data + 1);
272
273 break;
274
275 case XSTYPE_DICT: 278 case XSTYPE_DICT:
279 case XSTYPE_DATA:
276 len = _xs_get_24b(data + 1); 280 len = _xs_get_24b(data + 1);
277 281
278 break; 282 break;
@@ -341,7 +345,9 @@ xs_val *xs_expand(xs_val *data, int offset, int size)
341 if (data != NULL) 345 if (data != NULL)
342 memmove(data + offset + size, data + offset, sz - offset); 346 memmove(data + offset + size, data + offset, sz - offset);
343 347
344 if (xs_type(data) == XSTYPE_LIST || xs_type(data) == XSTYPE_DICT) 348 if (xs_type(data) == XSTYPE_LIST ||
349 xs_type(data) == XSTYPE_DICT ||
350 xs_type(data) == XSTYPE_DATA)
345 _xs_put_24b(data + 1, sz + size); 351 _xs_put_24b(data + 1, sz + size);
346 352
347 return data; 353 return data;
@@ -364,7 +370,9 @@ xs_val *xs_collapse(xs_val *data, int offset, int size)
364 for (n = offset; n < sz; n++) 370 for (n = offset; n < sz; n++)
365 data[n] = data[n + size]; 371 data[n] = data[n + size];
366 372
367 if (xs_type(data) == XSTYPE_LIST || xs_type(data) == XSTYPE_DICT) 373 if (xs_type(data) == XSTYPE_LIST ||
374 xs_type(data) == XSTYPE_DICT ||
375 xs_type(data) == XSTYPE_DATA)
368 _xs_put_24b(data + 1, sz); 376 _xs_put_24b(data + 1, sz);
369 377
370 return xs_realloc(data, _xs_blk_size(sz)); 378 return xs_realloc(data, _xs_blk_size(sz));
@@ -1030,6 +1038,42 @@ const char *xs_number_str(const xs_number *v)
1030} 1038}
1031 1039
1032 1040
1041/* raw data blocks */
1042
1043xs_data *xs_data_new(const void *data, int size)
1044/* returns a new raw data value */
1045{
1046 xs_data *v;
1047
1048 /* add the overhead (data type + 24bit size) */
1049 size += 4;
1050
1051 v = xs_realloc(NULL, _xs_blk_size(size));
1052 v[0] = XSTYPE_DATA;
1053
1054 _xs_put_24b(v + 1, size);
1055
1056 memcpy(&v[4], data, size);
1057
1058 return v;
1059}
1060
1061
1062int xs_data_size(const xs_data *value)
1063/* returns the size of the data stored inside value */
1064{
1065 return _xs_get_24b(value + 1) - 4;
1066}
1067
1068
1069void xs_data_get(const xs_data *value, void *data)
1070/* copies the raw data stored inside value into data */
1071{
1072 int size = _xs_get_24b(value + 1) - 4;
1073 memcpy(data, &value[4], size);
1074}
1075
1076
1033void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size) 1077void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size)
1034/* clone of memmem */ 1078/* clone of memmem */
1035{ 1079{
diff --git a/xs_version.h b/xs_version.h
index 0429b2e..c6cd0f4 100644
--- a/xs_version.h
+++ b/xs_version.h
@@ -1 +1 @@
/* 452a86b01d695705a3f61a9b26208855c11118b1 */ /* 79e4ff3403fbbd78f57482122a2478c9453483a8 */