summaryrefslogtreecommitdiffstats
path: root/misc
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2016-08-28 19:33:52 +0200
committerwm4 <wm4@nowhere>2016-08-28 19:39:05 +0200
commita9a55ea7f28894d9e72be5e8b2d5a9331f1e7be4 (patch)
treec4e840511799135f85ce4969efc7465b26c83c8d /misc
parent5e56c07417934d590665ad8330f04db7dd164229 (diff)
downloadmpv-a9a55ea7f28894d9e72be5e8b2d5a9331f1e7be4.tar.bz2
mpv-a9a55ea7f28894d9e72be5e8b2d5a9331f1e7be4.tar.xz
misc: add some annoying mpv_node helpers
Sigh. Some parts of mpv essentially duplicate this code (with varrying levels of triviality) - this can be fixed "later".
Diffstat (limited to 'misc')
-rw-r--r--misc/node.c65
-rw-r--r--misc/node.h11
2 files changed, 76 insertions, 0 deletions
diff --git a/misc/node.c b/misc/node.c
new file mode 100644
index 0000000000..1ea8ea7da5
--- /dev/null
+++ b/misc/node.c
@@ -0,0 +1,65 @@
+#include "common/common.h"
+
+#include "node.h"
+
+// Init a node with the given format. If parent is not NULL, it is set as
+// parent allocation according to m_option_type_node rules (which means
+// the mpv_node_list allocs are used for chaining the TA allocations).
+// format == MPV_FORMAT_NONE will simply initialize it with all-0.
+void node_init(struct mpv_node *dst, int format, struct mpv_node *parent)
+{
+ // Other formats need to be initialized manually.
+ assert(format == MPV_FORMAT_NODE_MAP || format == MPV_FORMAT_NODE_ARRAY ||
+ format == MPV_FORMAT_FLAG || format == MPV_FORMAT_INT64 ||
+ format == MPV_FORMAT_DOUBLE || format == MPV_FORMAT_NONE);
+
+ void *ta_parent = NULL;
+ if (parent) {
+ assert(parent->format == MPV_FORMAT_NODE_MAP ||
+ parent->format == MPV_FORMAT_NODE_ARRAY);
+ ta_parent = parent->u.list;
+ }
+
+ *dst = (struct mpv_node){ .format = format };
+ if (format == MPV_FORMAT_NODE_MAP || format == MPV_FORMAT_NODE_ARRAY)
+ dst->u.list = talloc_zero(ta_parent, struct mpv_node_list);
+}
+
+// Add an entry to a MPV_FORMAT_NODE_ARRAY.
+// m_option_type_node memory management rules apply.
+struct mpv_node *node_array_add(struct mpv_node *dst, int format)
+{
+ struct mpv_node_list *list = dst->u.list;
+ assert(dst->format == MPV_FORMAT_NODE_ARRAY && dst->u.list);
+ MP_TARRAY_GROW(list, list->values, list->num);
+ node_init(&list->values[list->num], format, dst);
+ return &list->values[list->num++];
+}
+
+// Add an entry to a MPV_FORMAT_NODE_MAP. Keep in mind that this does
+// not check for already existing entries under the same key.
+// m_option_type_node memory management rules apply.
+struct mpv_node *node_map_add(struct mpv_node *dst, const char *key, int format)
+{
+ assert(key);
+
+ struct mpv_node_list *list = dst->u.list;
+ assert(dst->format == MPV_FORMAT_NODE_MAP && dst->u.list);
+ MP_TARRAY_GROW(list, list->values, list->num);
+ MP_TARRAY_GROW(list, list->keys, list->num);
+ list->keys[list->num] = talloc_strdup(list, key);
+ node_init(&list->values[list->num], format, dst);
+ return &list->values[list->num++];
+}
+
+// Add a string entry to a MPV_FORMAT_NODE_MAP. Keep in mind that this does
+// not check for already existing entries under the same key.
+// m_option_type_node memory management rules apply.
+void node_map_add_string(struct mpv_node *dst, const char *key, const char *val)
+{
+ assert(val);
+
+ struct mpv_node *entry = node_map_add(dst, key, MPV_FORMAT_NONE);
+ entry->format = MPV_FORMAT_STRING;
+ entry->u.string = talloc_strdup(dst->u.list, val);
+}
diff --git a/misc/node.h b/misc/node.h
new file mode 100644
index 0000000000..c3b4501dc8
--- /dev/null
+++ b/misc/node.h
@@ -0,0 +1,11 @@
+#ifndef MP_MISC_NODE_H_
+#define MP_MISC_NODE_H_
+
+#include "libmpv/client.h"
+
+void node_init(struct mpv_node *dst, int format, struct mpv_node *parent);
+struct mpv_node *node_array_add(struct mpv_node *dst, int format);
+struct mpv_node *node_map_add(struct mpv_node *dst, const char *key, int format);
+void node_map_add_string(struct mpv_node *dst, const char *key, const char *val);
+
+#endif