summaryrefslogtreecommitdiffstats
path: root/common/tags.c
blob: 04dabd8cdf6c835fc5b525058b14f87bc35fc0fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * This file is part of mpv.
 *
 * mpv is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * mpv is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with mpv.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stddef.h>
#include <limits.h>
#include <strings.h>
#include <libavutil/dict.h>
#include "tags.h"
#include "misc/bstr.h"

void mp_tags_set_str(struct mp_tags *tags, const char *key, const char *value)
{
    mp_tags_set_bstr(tags, bstr0(key), bstr0(value));
}

void mp_tags_set_bstr(struct mp_tags *tags, bstr key, bstr value)
{
    for (int n = 0; n < tags->num_keys; n++) {
        if (bstrcasecmp0(key, tags->keys[n]) == 0) {
            talloc_free(tags->values[n]);
            tags->values[n] = talloc_strndup(tags, value.start, value.len);
            return;
        }
    }

    MP_RESIZE_ARRAY(tags, tags->keys,   tags->num_keys + 1);
    MP_RESIZE_ARRAY(tags, tags->values, tags->num_keys + 1);
    tags->keys[tags->num_keys]   = talloc_strndup(tags, key.start,   key.len);
    tags->values[tags->num_keys] = talloc_strndup(tags, value.start, value.len);
    tags->num_keys++;
}

char *mp_tags_get_str(struct mp_tags *tags, const char *key)
{
    return mp_tags_get_bstr(tags, bstr0(key));
}

char *mp_tags_get_bstr(struct mp_tags *tags, bstr key)
{
    for (int n = 0; n < tags->num_keys; n++) {
        if (bstrcasecmp0(key, tags->keys[n]) == 0)
            return tags->values[n];
    }
    return NULL;
}

void mp_tags_clear(struct mp_tags *tags)
{
    *tags = (struct mp_tags){0};
    talloc_free_children(tags);
}

struct mp_tags *mp_tags_dup(void *tparent, struct mp_tags *tags)
{
    struct mp_tags *new = talloc_zero(tparent, struct mp_tags);
    MP_RESIZE_ARRAY(new, new->keys,   tags->num_keys);
    MP_RESIZE_ARRAY(new, new->values, tags->num_keys);
    new->num_keys = tags->num_keys;
    for (int n = 0; n < tags->num_keys; n++) {
        new->keys[n] = talloc_strdup(new, tags->keys[n]);
        new->values[n] = talloc_strdup(new, tags->values[n]);
    }
    return new;
}

// Return a copy of the tags, but containing only keys in list. Also forces
// the order and casing of the keys (for cosmetic reasons).
// A trailing '*' matches the rest.
struct mp_tags *mp_tags_filtered(void *tparent, struct mp_tags *tags, char **list)
{
    struct mp_tags *new = talloc_zero(tparent, struct mp_tags);
    for (int n = 0; list && list[n]; n++) {
        char *key = list[n];
        size_t keylen = strlen(key);
        if (keylen >= INT_MAX)
            continue;
        bool prefix = keylen && key[keylen - 1] == '*';
        int matchlen = prefix ? keylen - 1 : keylen + 1;
        for (int x = 0; x < tags->num_keys; x++) {
            if (strncasecmp(tags->keys[x], key, matchlen) == 0) {
                char skey[320];
                snprintf(skey, sizeof(skey), "%.*s%s", matchlen, key,
                         prefix ? tags->keys[x] + keylen - 1 : "");
                mp_tags_set_str(new, skey, tags->values[x]);
            }
        }
    }
    return new;
}

void mp_tags_merge(struct mp_tags *tags, struct mp_tags *src)
{
    for (int n = 0; n < src->num_keys; n++)
        mp_tags_set_str(tags, src->keys[n], src->values[n]);
}

void mp_tags_copy_from_av_dictionary(struct mp_tags *tags,
                                     struct AVDictionary *av_dict)
{
    AVDictionaryEntry *entry = NULL;
    while ((entry = av_dict_get(av_dict, "", entry, AV_DICT_IGNORE_SUFFIX)))
        mp_tags_set_str(tags, entry->key, entry->value);
}