2 * copyright (c) 2009 Michael Niedermayer
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 AVDictionaryEntry *elems;
34 int av_dict_count(const AVDictionary *m)
36 return m ? m->count : 0;
39 AVDictionaryEntry *av_dict_get(const AVDictionary *m, const char *key,
40 const AVDictionaryEntry *prev, int flags)
48 i = prev - m->elems + 1;
52 for (; i < m->count; i++) {
53 const char *s = m->elems[i].key;
54 if (flags & AV_DICT_MATCH_CASE)
55 for (j = 0; s[j] == key[j] && key[j]; j++)
58 for (j = 0; av_toupper(s[j]) == av_toupper(key[j]) && key[j]; j++)
62 if (s[j] && !(flags & AV_DICT_IGNORE_SUFFIX))
69 int av_dict_set(AVDictionary **pm, const char *key, const char *value,
72 AVDictionary *m = *pm;
73 AVDictionaryEntry *tag = NULL;
74 char *oldval = NULL, *copy_key = NULL, *copy_value = NULL;
76 if (!(flags & AV_DICT_MULTIKEY)) {
77 tag = av_dict_get(m, key, NULL, flags);
79 if (flags & AV_DICT_DONT_STRDUP_KEY)
80 copy_key = (void *)key;
82 copy_key = av_strdup(key);
83 if (flags & AV_DICT_DONT_STRDUP_VAL)
84 copy_value = (void *)value;
86 copy_value = av_strdup(value);
88 m = *pm = av_mallocz(sizeof(*m));
89 if (!m || (key && !copy_key) || (value && !copy_value))
93 if (flags & AV_DICT_DONT_OVERWRITE) {
98 if (flags & AV_DICT_APPEND)
103 *tag = m->elems[--m->count];
104 } else if (copy_value) {
105 AVDictionaryEntry *tmp = av_realloc(m->elems,
106 (m->count + 1) * sizeof(*m->elems));
112 m->elems[m->count].key = copy_key;
113 m->elems[m->count].value = copy_value;
114 if (oldval && flags & AV_DICT_APPEND) {
115 size_t len = strlen(oldval) + strlen(copy_value) + 1;
116 char *newval = av_mallocz(len);
119 av_strlcat(newval, oldval, len);
121 av_strlcat(newval, copy_value, len);
122 m->elems[m->count].value = newval;
123 av_freep(©_value);
137 if (m && !m->count) {
143 return AVERROR(ENOMEM);
146 int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value,
150 snprintf(valuestr, sizeof(valuestr), "%"PRId64, value);
151 flags &= ~AV_DICT_DONT_STRDUP_VAL;
152 return av_dict_set(pm, key, valuestr, flags);
155 static int parse_key_value_pair(AVDictionary **pm, const char **buf,
156 const char *key_val_sep, const char *pairs_sep,
159 char *key = av_get_token(buf, key_val_sep);
163 if (key && *key && strspn(*buf, key_val_sep)) {
165 val = av_get_token(buf, pairs_sep);
168 if (key && *key && val && *val)
169 ret = av_dict_set(pm, key, val, flags);
171 ret = AVERROR(EINVAL);
179 int av_dict_parse_string(AVDictionary **pm, const char *str,
180 const char *key_val_sep, const char *pairs_sep,
188 /* ignore STRDUP flags */
189 flags &= ~(AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
192 if ((ret = parse_key_value_pair(pm, &str, key_val_sep, pairs_sep, flags)) < 0)
202 void av_dict_free(AVDictionary **pm)
204 AVDictionary *m = *pm;
208 av_freep(&m->elems[m->count].key);
209 av_freep(&m->elems[m->count].value);
216 int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
218 AVDictionaryEntry *t = NULL;
220 while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX))) {
221 int ret = av_dict_set(dst, t->key, t->value, flags);
229 int av_dict_get_string(const AVDictionary *m, char **buffer,
230 const char key_val_sep, const char pairs_sep)
232 AVDictionaryEntry *t = NULL;
235 char special_chars[] = {pairs_sep, key_val_sep, '\0'};
237 if (!buffer || pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
238 pairs_sep == '\\' || key_val_sep == '\\')
239 return AVERROR(EINVAL);
241 if (!av_dict_count(m)) {
242 *buffer = av_strdup("");
243 return *buffer ? 0 : AVERROR(ENOMEM);
246 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
247 while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) {
249 av_bprint_append_data(&bprint, &pairs_sep, 1);
250 av_bprint_escape(&bprint, t->key, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
251 av_bprint_append_data(&bprint, &key_val_sep, 1);
252 av_bprint_escape(&bprint, t->value, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
254 return av_bprint_finalize(&bprint, buffer);