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.)
This commit is contained in:
wm4 2018-05-17 15:29:03 +02:00
parent ec7d1e86b6
commit cec37e98d5
3 changed files with 56 additions and 49 deletions

View File

@ -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);
}

View File

@ -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

View File

@ -31,6 +31,7 @@
#include "input/cmd.h"
#include "misc/ctype.h"
#include "misc/dispatch.h"
#include "misc/node.h"
#include "misc/rendezvous.h"
#include "misc/thread_tools.h"
#include "options/m_config.h"
@ -942,54 +943,6 @@ static bool conv_node_to_format(void *dst, mpv_format dst_fmt, mpv_node *src)
return false;
}
// Note: for MPV_FORMAT_NODE_MAP, this (incorrectly) takes the order into
// account, instead of treating it as set.
static bool compare_value(void *a, 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: {
struct mpv_node *a_n = a, *b_n = b;
if (a_n->format != b_n->format)
return false;
return compare_value(&a_n->u, &b_n->u, a_n->format);
}
case MPV_FORMAT_BYTE_ARRAY: {
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 (!compare_value(&l_a->values[n], &l_b->values[n], MPV_FORMAT_NODE))
return false;
if (format == MPV_FORMAT_NODE_MAP) {
if (strcmp(l_a->keys[n], l_b->keys[n]) != 0)
return false;
}
}
return true;
}
}
abort();
}
void mpv_free_node_contents(mpv_node *node)
{
static const struct m_option type = { .type = CONF_TYPE_NODE };
@ -1622,7 +1575,7 @@ static void update_prop(void *p)
if (prop->user_value_valid != prop->new_value_valid) {
prop->changed = true;
} else if (prop->user_value_valid && prop->new_value_valid) {
if (!compare_value(&prop->user_value, &prop->new_value, prop->format))
if (!equal_mpv_value(&prop->user_value, &prop->new_value, prop->format))
prop->changed = true;
}
if (prop->dead)