summaryrefslogtreecommitdiff
path: root/xs.h
diff options
context:
space:
mode:
Diffstat (limited to 'xs.h')
-rw-r--r--xs.h44
1 files changed, 40 insertions, 4 deletions
diff --git a/xs.h b/xs.h
index 7df7346..afd8245 100644
--- a/xs.h
+++ b/xs.h
@@ -109,6 +109,7 @@ xs_dict *xs_dict_append_m(xs_dict *dict, const xs_str *key, const xs_val *mem, i
109xs_dict *xs_dict_prepend_m(xs_dict *dict, const xs_str *key, const xs_val *mem, int dsz); 109xs_dict *xs_dict_prepend_m(xs_dict *dict, const xs_str *key, const xs_val *mem, int dsz);
110#define xs_dict_prepend(dict, key, data) xs_dict_prepend_m(dict, key, data, xs_size(data)) 110#define xs_dict_prepend(dict, key, data) xs_dict_prepend_m(dict, key, data, xs_size(data))
111int xs_dict_iter(xs_dict **dict, xs_str **key, xs_val **value); 111int xs_dict_iter(xs_dict **dict, xs_str **key, xs_val **value);
112int xs_dict_next(const xs_dict *dict, xs_str **key, xs_val **value, int *ctxt);
112xs_val *xs_dict_get_def(const xs_dict *dict, const xs_str *key, const xs_val *def); 113xs_val *xs_dict_get_def(const xs_dict *dict, const xs_str *key, const xs_val *def);
113#define xs_dict_get(dict, key) xs_dict_get_def(dict, key, NULL) 114#define xs_dict_get(dict, key) xs_dict_get_def(dict, key, NULL)
114xs_dict *xs_dict_del(xs_dict *dict, const xs_str *key); 115xs_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)
1024} 1025}
1025 1026
1026 1027
1028int xs_dict_next(const xs_dict *dict, xs_str **key, xs_val **value, int *ctxt)
1029/* iterates a dict, with context */
1030{
1031 int goon = 1;
1032
1033 char *p = (char *)dict;
1034
1035 /* skip the start of the list */
1036 if (*ctxt == 0)
1037 *ctxt = 1 + _XS_TYPE_SIZE;
1038
1039 p += *ctxt;
1040
1041 /* an element? */
1042 if (xs_type(p) == XSTYPE_DITEM) {
1043 p++;
1044
1045 *key = p;
1046 p += xs_size(*key);
1047
1048 *value = p;
1049 p += xs_size(*value);
1050 }
1051 else {
1052 /* end of list */
1053 goon = 0;
1054 }
1055
1056 /* store back the pointer */
1057 *ctxt = p - dict;
1058
1059 return goon;
1060}
1061
1062
1027xs_val *xs_dict_get_def(const xs_dict *dict, const xs_str *key, const xs_val *def) 1063xs_val *xs_dict_get_def(const xs_dict *dict, const xs_str *key, const xs_val *def)
1028/* returns the value directed by key, or the default value */ 1064/* returns the value directed by key, or the default value */
1029{ 1065{
1030 XS_ASSERT_TYPE(dict, XSTYPE_DICT); 1066 XS_ASSERT_TYPE(dict, XSTYPE_DICT);
1031 XS_ASSERT_TYPE(key, XSTYPE_STRING); 1067 XS_ASSERT_TYPE(key, XSTYPE_STRING);
1032 1068
1033 xs_dict *p = (xs_dict *)dict;
1034 xs_str *k; 1069 xs_str *k;
1035 xs_val *v; 1070 xs_val *v;
1071 int c = 0;
1036 1072
1037 while (xs_dict_iter(&p, &k, &v)) { 1073 while (xs_dict_next(dict, &k, &v, &c)) {
1038 if (strcmp(k, key) == 0) 1074 if (strcmp(k, key) == 0)
1039 return v; 1075 return v;
1040 } 1076 }
@@ -1051,9 +1087,9 @@ xs_dict *xs_dict_del(xs_dict *dict, const xs_str *key)
1051 1087
1052 xs_str *k; 1088 xs_str *k;
1053 xs_val *v; 1089 xs_val *v;
1054 xs_dict *p = dict; 1090 int c = 0;
1055 1091
1056 while (xs_dict_iter(&p, &k, &v)) { 1092 while (xs_dict_next(dict, &k, &v, &c)) {
1057 if (strcmp(k, key) == 0) { 1093 if (strcmp(k, key) == 0) {
1058 /* the address of the item is just behind the key */ 1094 /* the address of the item is just behind the key */
1059 char *i = k - 1; 1095 char *i = k - 1;