summaryrefslogtreecommitdiff
path: root/xs.h
diff options
context:
space:
mode:
authorGravatar default2024-08-05 06:01:21 +0200
committerGravatar default2024-08-05 06:01:21 +0200
commit972c3dc5d43a114ae59386d4edfdfb8ce0f3793e (patch)
tree2568b6190dc0ca34478ccabc1191e504d1e8cfcf /xs.h
parentMinor logging tweaks. (diff)
downloadpenes-snac2-972c3dc5d43a114ae59386d4edfdfb8ce0f3793e.tar.gz
penes-snac2-972c3dc5d43a114ae59386d4edfdfb8ce0f3793e.tar.xz
penes-snac2-972c3dc5d43a114ae59386d4edfdfb8ce0f3793e.zip
Added support for listening on unix sockets.
Diffstat (limited to 'xs.h')
-rw-r--r--xs.h77
1 files changed, 69 insertions, 8 deletions
diff --git a/xs.h b/xs.h
index f250764..d1a874c 100644
--- a/xs.h
+++ b/xs.h
@@ -123,7 +123,12 @@ const xs_val *xs_dict_get_def(const xs_dict *dict, const xs_str *key, const xs_v
123#define xs_dict_get(dict, key) xs_dict_get_def(dict, key, NULL) 123#define xs_dict_get(dict, key) xs_dict_get_def(dict, key, NULL)
124xs_dict *xs_dict_del(xs_dict *dict, const xs_str *key); 124xs_dict *xs_dict_del(xs_dict *dict, const xs_str *key);
125xs_dict *xs_dict_set(xs_dict *dict, const xs_str *key, const xs_val *data); 125xs_dict *xs_dict_set(xs_dict *dict, const xs_str *key, const xs_val *data);
126xs_dict *xs_dict_gc(xs_dict *dict); 126xs_dict *xs_dict_gc(const xs_dict *dict);
127
128const xs_val *xs_dict_get_path_sep(const xs_dict *dict, const char *path, const char *sep);
129#define xs_dict_get_path(dict, path) xs_dict_get_path_sep(dict, path, ".")
130xs_dict *xs_dict_set_path_sep(xs_dict *dict, const char *path, const xs_val *value, const char *sep);
131#define xs_dict_set_path(dict, path, value) xs_dict_set_path_sep(dict, path, value, ".")
127 132
128xs_val *xs_val_new(xstype t); 133xs_val *xs_val_new(xstype t);
129xs_number *xs_number_new(double f); 134xs_number *xs_number_new(double f);
@@ -1258,24 +1263,80 @@ int xs_dict_next(const xs_dict *dict, const xs_str **key, const xs_val **value,
1258} 1263}
1259 1264
1260 1265
1261xs_dict *xs_dict_gc(xs_dict *dict) 1266xs_dict *xs_dict_gc(const xs_dict *dict)
1262/* collects garbage (leaked values) inside a dict */ 1267/* creates a copy of dict, but garbage-collected */
1263{ 1268{
1264 xs_dict *nd = xs_dict_new(); 1269 xs_dict *nd = xs_dict_new();
1265 const xs_str *k; 1270 const xs_str *k;
1266 const xs_val *v; 1271 const xs_val *v;
1267 int c = 0; 1272 int c = 0;
1268 1273
1269 /* shamelessly create a new dict with the same content */ 1274 while (xs_dict_next(dict, &k, &v, &c)) {
1270 while (xs_dict_next(dict, &k, &v, &c)) 1275 if (xs_type(v) == XSTYPE_DICT) {
1271 nd = xs_dict_set(nd, k, v); 1276 xs *sd = xs_dict_gc(v);
1272 1277 nd = xs_dict_set(nd, k, sd);
1273 xs_free(dict); 1278 }
1279 else
1280 nd = xs_dict_set(nd, k, v);
1281 }
1274 1282
1275 return nd; 1283 return nd;
1276} 1284}
1277 1285
1278 1286
1287const xs_val *xs_dict_get_path_sep(const xs_dict *dict, const char *path, const char *sep)
1288/* gets a value from dict given a path separated by sep */
1289{
1290 /* split by the separator */
1291 xs *l = xs_split_n(path, sep, 1);
1292
1293 /* only one part? just get */
1294 if (xs_list_len(l) == 1)
1295 return xs_dict_get(dict, path);
1296
1297 const char *prefix = xs_list_get(l, 0);
1298 const char *rest = xs_list_get(l, 1);
1299 const xs_dict *sd = xs_dict_get(dict, prefix);
1300
1301 if (xs_type(sd) == XSTYPE_DICT)
1302 return xs_dict_get_path_sep(sd, rest, sep);
1303
1304 return NULL;
1305}
1306
1307
1308xs_dict *xs_dict_set_path_sep(xs_dict *dict, const char *path, const xs_val *value, const char *sep)
1309/* sets a value into dict given a path separated by sep;
1310 intermediate dicts are created if needed */
1311{
1312 /* split by the separator */
1313 xs *l = xs_split_n(path, sep, 1);
1314
1315 /* only one part? just set */
1316 if (xs_list_len(l) == 1)
1317 return xs_dict_set(dict, path, value);
1318
1319 const char *prefix = xs_list_get(l, 0);
1320 const char *rest = xs_list_get(l, 1);
1321
1322 xs *nd = NULL;
1323
1324 /* does the first part of path exist? */
1325 const xs_dict *cd = xs_dict_get(dict, prefix);
1326
1327 if (xs_type(cd) == XSTYPE_DICT)
1328 nd = xs_dup(cd);
1329 else
1330 nd = xs_dict_new();
1331
1332 /* move down the path */
1333 nd = xs_dict_set_path_sep(nd, rest, value, sep);
1334
1335 /* set */
1336 return xs_dict_set(dict, prefix, nd);
1337}
1338
1339
1279/** other values **/ 1340/** other values **/
1280 1341
1281xs_val *xs_val_new(xstype t) 1342xs_val *xs_val_new(xstype t)