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 = av_dict_get(m, key, NULL, flags);
74 char *oldval = NULL, *copy_key = NULL, *copy_value = NULL;
76 if (flags & AV_DICT_DONT_STRDUP_KEY)
77 copy_key = (void *)key;
79 copy_key = av_strdup(key);
80 if (flags & AV_DICT_DONT_STRDUP_VAL)
81 copy_value = (void *)value;
83 copy_value = av_strdup(value);
85 m = *pm = av_mallocz(sizeof(*m));
86 if (!m || (key && !copy_key) || (value && !copy_value))
90 if (flags & AV_DICT_DONT_OVERWRITE) {
95 if (flags & AV_DICT_APPEND)
100 *tag = m->elems[--m->count];
102 AVDictionaryEntry *tmp = av_realloc(m->elems,
103 (m->count + 1) * sizeof(*m->elems));
109 m->elems[m->count].key = copy_key;
110 m->elems[m->count].value = copy_value;
111 if (oldval && flags & AV_DICT_APPEND) {
112 size_t len = strlen(oldval) + strlen(copy_value) + 1;
113 char *newval = av_mallocz(len);
116 av_strlcat(newval, oldval, len);
118 av_strlcat(newval, copy_value, len);
119 m->elems[m->count].value = newval;
120 av_freep(©_value);
134 if (m && !m->count) {
140 return AVERROR(ENOMEM);
143 int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value,
147 snprintf(valuestr, sizeof(valuestr), "%"PRId64, value);
148 flags &= ~AV_DICT_DONT_STRDUP_VAL;
149 return av_dict_set(pm, key, valuestr, flags);
152 static int parse_key_value_pair(AVDictionary **pm, const char **buf,
153 const char *key_val_sep, const char *pairs_sep,
156 char *key = av_get_token(buf, key_val_sep);
160 if (key && *key && strspn(*buf, key_val_sep)) {
162 val = av_get_token(buf, pairs_sep);
165 if (key && *key && val && *val)
166 ret = av_dict_set(pm, key, val, flags);
168 ret = AVERROR(EINVAL);
176 int av_dict_parse_string(AVDictionary **pm, const char *str,
177 const char *key_val_sep, const char *pairs_sep,
185 /* ignore STRDUP flags */
186 flags &= ~(AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
189 if ((ret = parse_key_value_pair(pm, &str, key_val_sep, pairs_sep, flags)) < 0)
199 void av_dict_free(AVDictionary **pm)
201 AVDictionary *m = *pm;
205 av_freep(&m->elems[m->count].key);
206 av_freep(&m->elems[m->count].value);
213 void av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
215 AVDictionaryEntry *t = NULL;
217 while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX)))
218 av_dict_set(dst, t->key, t->value, flags);
221 int av_dict_get_string(const AVDictionary *m, char **buffer,
222 const char key_val_sep, const char pairs_sep)
224 AVDictionaryEntry *t = NULL;
227 char special_chars[] = {pairs_sep, key_val_sep, '\0'};
229 if (!buffer || pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
230 pairs_sep == '\\' || key_val_sep == '\\')
231 return AVERROR(EINVAL);
233 if (!av_dict_count(m)) {
234 *buffer = av_strdup("");
235 return *buffer ? 0 : AVERROR(ENOMEM);
238 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
239 while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) {
241 av_bprint_append_data(&bprint, &pairs_sep, 1);
242 av_bprint_escape(&bprint, t->key, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
243 av_bprint_append_data(&bprint, &key_val_sep, 1);
244 av_bprint_escape(&bprint, t->value, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
246 return av_bprint_finalize(&bprint, buffer);
250 static void print_dict(const AVDictionary *m)
252 AVDictionaryEntry *t = NULL;
253 while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
254 printf("%s %s ", t->key, t->value);
258 static void test_separators(const AVDictionary *m, const char pair, const char val)
260 AVDictionary *dict = NULL;
261 char pairs[] = {pair , '\0'};
262 char vals[] = {val, '\0'};
265 av_dict_copy(&dict, m, 0);
267 av_dict_get_string(dict, &buffer, val, pair);
268 printf("%s\n", buffer);
270 av_dict_parse_string(&dict, buffer, vals, pairs, 0);
278 AVDictionary *dict = NULL;
279 AVDictionaryEntry *e;
282 printf("Testing av_dict_get_string() and av_dict_parse_string()\n");
283 av_dict_get_string(dict, &buffer, '=', ',');
284 printf("%s\n", buffer);
286 av_dict_set(&dict, "aaa", "aaa", 0);
287 av_dict_set(&dict, "b,b", "bbb", 0);
288 av_dict_set(&dict, "c=c", "ccc", 0);
289 av_dict_set(&dict, "ddd", "d,d", 0);
290 av_dict_set(&dict, "eee", "e=e", 0);
291 av_dict_set(&dict, "f,f", "f=f", 0);
292 av_dict_set(&dict, "g=g", "g,g", 0);
293 test_separators(dict, ',', '=');
295 av_dict_set(&dict, "aaa", "aaa", 0);
296 av_dict_set(&dict, "bbb", "bbb", 0);
297 av_dict_set(&dict, "ccc", "ccc", 0);
298 av_dict_set(&dict, "\\,=\'\"", "\\,=\'\"", 0);
299 test_separators(dict, '"', '=');
300 test_separators(dict, '\'', '=');
301 test_separators(dict, ',', '"');
302 test_separators(dict, ',', '\'');
303 test_separators(dict, '\'', '"');
304 test_separators(dict, '"', '\'');
307 printf("\nTesting av_dict_set()\n");
308 av_dict_set(&dict, "a", "a", 0);
309 av_dict_set(&dict, "b", av_strdup("b"), AV_DICT_DONT_STRDUP_VAL);
310 av_dict_set(&dict, av_strdup("c"), "c", AV_DICT_DONT_STRDUP_KEY);
311 av_dict_set(&dict, av_strdup("d"), av_strdup("d"), AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
312 av_dict_set(&dict, "e", "e", AV_DICT_DONT_OVERWRITE);
313 av_dict_set(&dict, "e", "f", AV_DICT_DONT_OVERWRITE);
314 av_dict_set(&dict, "f", "f", 0);
315 av_dict_set(&dict, "f", NULL, 0);
316 av_dict_set(&dict, "ff", "f", 0);
317 av_dict_set(&dict, "ff", "f", AV_DICT_APPEND);
319 while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
320 printf("%s %s\n", e->key, e->value);
323 av_dict_set(&dict, NULL, "a", 0);
324 av_dict_set(&dict, NULL, "b", 0);
325 av_dict_get(dict, NULL, NULL, 0);
327 while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
328 printf("'%s' '%s'\n", e->key, e->value);
332 //valgrind sensible test
333 printf("\nTesting av_dict_set_int()\n");
334 av_dict_set_int(&dict, "1", 1, AV_DICT_DONT_STRDUP_VAL);
335 av_dict_set_int(&dict, av_strdup("2"), 2, AV_DICT_DONT_STRDUP_KEY);
336 av_dict_set_int(&dict, av_strdup("3"), 3, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
337 av_dict_set_int(&dict, "4", 4, 0);
338 av_dict_set_int(&dict, "5", 5, AV_DICT_DONT_OVERWRITE);
339 av_dict_set_int(&dict, "5", 6, AV_DICT_DONT_OVERWRITE);
340 av_dict_set_int(&dict, "12", 1, 0);
341 av_dict_set_int(&dict, "12", 2, AV_DICT_APPEND);
343 while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
344 printf("%s %s\n", e->key, e->value);
347 //valgrind sensible test
348 printf("\nTesting av_dict_set() with existing AVDictionaryEntry.key as key\n");
349 av_dict_set(&dict, "key", "old", 0);
350 e = av_dict_get(dict, "key", NULL, 0);
351 av_dict_set(&dict, e->key, "new val OK", 0);
352 e = av_dict_get(dict, "key", NULL, 0);
353 printf("%s\n", e->value);
354 av_dict_set(&dict, e->key, e->value, 0);
355 e = av_dict_get(dict, "key", NULL, 0);
356 printf("%s\n", e->value);