summaryrefslogtreecommitdiffstats
path: root/misc
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2018-05-17 15:29:03 +0200
committerwm4 <wm4@nowhere>2018-05-24 19:56:34 +0200
commitcec37e98d59af3bf91ffc4d44eedee809ab3acc7 (patch)
treea20b2c94893fe7955414a4a6e182fc895f2b2215 /misc
parentec7d1e86b60f1c684bc9778a9ce958a9ecd54735 (diff)
downloadmpv-cec37e98d59af3bf91ffc4d44eedee809ab3acc7.tar.bz2
mpv-cec37e98d59af3bf91ffc4d44eedee809ab3acc7.tar.xz
misc: move some helper code from client.c
(Slightly oddly function names, because I want to avoid starting them with mpv_*, which is reserved for public API.)
Diffstat (limited to 'misc')
-rw-r--r--misc/node.c52
-rw-r--r--misc/node.h2
2 files changed, 54 insertions, 0 deletions
diff --git a/misc/node.c b/misc/node.c
index f5fb8da0e9..9b45291a5f 100644
--- a/misc/node.c
+++ b/misc/node.c
@@ -94,3 +94,55 @@ mpv_node *node_map_get(mpv_node *src, const char *key)
return NULL;
}
+
+// Note: for MPV_FORMAT_NODE_MAP, this (incorrectly) takes the order into
+// account, instead of treating it as set.
+bool equal_mpv_value(const void *a, const void *b, mpv_format format)
+{
+ switch (format) {
+ case MPV_FORMAT_NONE:
+ return true;
+ case MPV_FORMAT_STRING:
+ case MPV_FORMAT_OSD_STRING:
+ return strcmp(*(char **)a, *(char **)b) == 0;
+ case MPV_FORMAT_FLAG:
+ return *(int *)a == *(int *)b;
+ case MPV_FORMAT_INT64:
+ return *(int64_t *)a == *(int64_t *)b;
+ case MPV_FORMAT_DOUBLE:
+ return *(double *)a == *(double *)b;
+ case MPV_FORMAT_NODE:
+ return equal_mpv_node(a, b);
+ case MPV_FORMAT_BYTE_ARRAY: {
+ const struct mpv_byte_array *a_r = a, *b_r = b;
+ if (a_r->size != b_r->size)
+ return false;
+ return memcmp(a_r->data, b_r->data, a_r->size) == 0;
+ }
+ case MPV_FORMAT_NODE_ARRAY:
+ case MPV_FORMAT_NODE_MAP:
+ {
+ mpv_node_list *l_a = *(mpv_node_list **)a, *l_b = *(mpv_node_list **)b;
+ if (l_a->num != l_b->num)
+ return false;
+ for (int n = 0; n < l_a->num; n++) {
+ if (format == MPV_FORMAT_NODE_MAP) {
+ if (strcmp(l_a->keys[n], l_b->keys[n]) != 0)
+ return false;
+ }
+ if (!equal_mpv_node(&l_a->values[n], &l_b->values[n]))
+ return false;
+ }
+ return true;
+ }
+ }
+ abort(); // supposed to be able to handle all defined types
+}
+
+// Remarks see equal_mpv_value().
+bool equal_mpv_node(const struct mpv_node *a, const struct mpv_node *b)
+{
+ if (a->format != b->format)
+ return false;
+ return equal_mpv_value(&a->u, &b->u, a->format);
+}
diff --git a/misc/node.h b/misc/node.h
index 5d8100da41..419f3fc505 100644
--- a/misc/node.h
+++ b/misc/node.h
@@ -11,5 +11,7 @@ void node_map_add_int64(struct mpv_node *dst, const char *key, int64_t v);
void node_map_add_double(struct mpv_node *dst, const char *key, double v);
void node_map_add_flag(struct mpv_node *dst, const char *key, bool v);
mpv_node *node_map_get(mpv_node *src, const char *key);
+bool equal_mpv_value(const void *a, const void *b, mpv_format format);
+bool equal_mpv_node(const struct mpv_node *a, const struct mpv_node *b);
#endif