return NULL;
}
+enum ucl_safe_iter_flags {
+ UCL_ITERATE_FLAG_UNDEFINED = 0,
+ UCL_ITERATE_FLAG_INSIDE_ARRAY,
+ UCL_ITERATE_FLAG_INSIDE_OBJECT,
+ UCL_ITERATE_FLAG_IMPLICIT,
+};
+
const char safe_iter_magic[4] = {'u', 'i', 't', 'e'};
struct ucl_object_safe_iter {
char magic[4]; /* safety check */
+ uint32_t flags;
const ucl_object_t *impl_it; /* implicit object iteration */
ucl_object_iter_t expl_it; /* explicit iteration */
};
it = UCL_ALLOC (sizeof (*it));
if (it != NULL) {
memcpy (it->magic, safe_iter_magic, sizeof (it->magic));
+ it->flags = UCL_ITERATE_FLAG_UNDEFINED;
it->expl_it = NULL;
it->impl_it = obj;
}
UCL_SAFE_ITER_CHECK (rit);
if (rit->expl_it != NULL) {
- UCL_FREE (sizeof (*rit->expl_it), rit->expl_it);
+ if (rit->flags == UCL_ITERATE_FLAG_INSIDE_OBJECT) {
+ UCL_FREE (sizeof (*rit->expl_it), rit->expl_it);
+ }
}
rit->impl_it = obj;
rit->expl_it = NULL;
+ rit->flags = UCL_ITERATE_FLAG_UNDEFINED;
return it;
}
return NULL;
}
- if (rit->impl_it->type == UCL_OBJECT || rit->impl_it->type == UCL_ARRAY) {
+ if (rit->impl_it->type == UCL_OBJECT) {
+ rit->flags = UCL_ITERATE_FLAG_INSIDE_OBJECT;
+ ret = ucl_object_iterate (rit->impl_it, &rit->expl_it, true);
+
+ if (ret == NULL && (type & UCL_ITERATE_IMPLICIT)) {
+ /* Need to switch to another implicit object in chain */
+ rit->impl_it = rit->impl_it->next;
+ rit->expl_it = NULL;
+
+ return ucl_object_iterate_safe (it, type);
+ }
+ }
+ else if (rit->impl_it->type == UCL_ARRAY) {
+ rit->flags = UCL_ITERATE_FLAG_INSIDE_ARRAY;
ret = ucl_object_iterate (rit->impl_it, &rit->expl_it, true);
if (ret == NULL && (type & UCL_ITERATE_IMPLICIT)) {
}
else {
/* Just iterate over the implicit array */
+ rit->flags = UCL_ITERATE_FLAG_IMPLICIT;
ret = rit->impl_it;
rit->impl_it = rit->impl_it->next;
UCL_SAFE_ITER_CHECK (rit);
if (rit->expl_it != NULL) {
- UCL_FREE (sizeof (*rit->expl_it), rit->expl_it);
+ if (rit->flags == UCL_ITERATE_FLAG_INSIDE_OBJECT) {
+ UCL_FREE (sizeof (*rit->expl_it), rit->expl_it);
+ }
}
UCL_FREE (sizeof (*rit), it);