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);
77 m = *pm = av_mallocz(sizeof(*m));
82 if (flags & AV_DICT_DONT_OVERWRITE) {
83 if (flags & AV_DICT_DONT_STRDUP_KEY) av_free((void*)key);
84 if (flags & AV_DICT_DONT_STRDUP_VAL) av_free((void*)value);
87 if (flags & AV_DICT_APPEND)
92 *tag = m->elems[--m->count];
94 AVDictionaryEntry *tmp = av_realloc(m->elems,
95 (m->count + 1) * sizeof(*m->elems));
101 if (flags & AV_DICT_DONT_STRDUP_KEY)
102 m->elems[m->count].key = (char*)(intptr_t)key;
104 m->elems[m->count].key = av_strdup(key);
105 if (!m->elems[m->count].key)
107 if (flags & AV_DICT_DONT_STRDUP_VAL) {
108 m->elems[m->count].value = (char*)(intptr_t)value;
109 } else if (oldval && flags & AV_DICT_APPEND) {
110 int len = strlen(oldval) + strlen(value) + 1;
111 char *newval = av_mallocz(len);
114 av_strlcat(newval, oldval, len);
116 av_strlcat(newval, value, len);
117 m->elems[m->count].value = newval;
119 m->elems[m->count].value = av_strdup(value);
130 if (m && !m->count) {
134 if (flags & AV_DICT_DONT_STRDUP_KEY) av_free((void*)key);
135 if (flags & AV_DICT_DONT_STRDUP_VAL) av_free((void*)value);
136 return AVERROR(ENOMEM);
139 int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value,
143 snprintf(valuestr, sizeof(valuestr), "%"PRId64, value);
144 return av_dict_set(pm, key, valuestr, flags);
147 static int parse_key_value_pair(AVDictionary **pm, const char **buf,
148 const char *key_val_sep, const char *pairs_sep,
151 char *key = av_get_token(buf, key_val_sep);
155 if (key && *key && strspn(*buf, key_val_sep)) {
157 val = av_get_token(buf, pairs_sep);
160 if (key && *key && val && *val)
161 ret = av_dict_set(pm, key, val, flags);
163 ret = AVERROR(EINVAL);
171 int av_dict_parse_string(AVDictionary **pm, const char *str,
172 const char *key_val_sep, const char *pairs_sep,
180 /* ignore STRDUP flags */
181 flags &= ~(AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
184 if ((ret = parse_key_value_pair(pm, &str, key_val_sep, pairs_sep, flags)) < 0)
194 void av_dict_free(AVDictionary **pm)
196 AVDictionary *m = *pm;
200 av_freep(&m->elems[m->count].key);
201 av_freep(&m->elems[m->count].value);
208 void av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
210 AVDictionaryEntry *t = NULL;
212 while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX)))
213 av_dict_set(dst, t->key, t->value, flags);
216 int av_dict_get_string(const AVDictionary *m, char **buffer,
217 const char key_val_sep, const char pairs_sep)
219 AVDictionaryEntry *t = NULL;
222 char special_chars[] = {pairs_sep, key_val_sep, '\0'};
224 if (!buffer || pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
225 pairs_sep == '\\' || key_val_sep == '\\')
226 return AVERROR(EINVAL);
228 if (!av_dict_count(m)) {
229 *buffer = av_strdup("");
230 return *buffer ? 0 : AVERROR(ENOMEM);
233 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
234 while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) {
236 av_bprint_append_data(&bprint, &pairs_sep, 1);
237 av_bprint_escape(&bprint, t->key, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
238 av_bprint_append_data(&bprint, &key_val_sep, 1);
239 av_bprint_escape(&bprint, t->value, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
241 return av_bprint_finalize(&bprint, buffer);
245 static void print_dict(const AVDictionary *m)
247 AVDictionaryEntry *t = NULL;
248 while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
249 printf("%s %s ", t->key, t->value);
253 static void test_separators(const AVDictionary *m, const char pair, const char val)
255 AVDictionary *dict = NULL;
256 char pairs[] = {pair , '\0'};
257 char vals[] = {val, '\0'};
260 av_dict_copy(&dict, m, 0);
262 av_dict_get_string(dict, &buffer, val, pair);
263 printf("%s\n", buffer);
265 av_dict_parse_string(&dict, buffer, vals, pairs, 0);
273 AVDictionary *dict = NULL;
276 printf("Testing av_dict_get_string() and av_dict_parse_string()\n");
277 av_dict_get_string(dict, &buffer, '=', ',');
278 printf("%s\n", buffer);
280 av_dict_set(&dict, "aaa", "aaa", 0);
281 av_dict_set(&dict, "b,b", "bbb", 0);
282 av_dict_set(&dict, "c=c", "ccc", 0);
283 av_dict_set(&dict, "ddd", "d,d", 0);
284 av_dict_set(&dict, "eee", "e=e", 0);
285 av_dict_set(&dict, "f,f", "f=f", 0);
286 av_dict_set(&dict, "g=g", "g,g", 0);
287 test_separators(dict, ',', '=');
289 av_dict_set(&dict, "aaa", "aaa", 0);
290 av_dict_set(&dict, "bbb", "bbb", 0);
291 av_dict_set(&dict, "ccc", "ccc", 0);
292 av_dict_set(&dict, "\\,=\'\"", "\\,=\'\"", 0);
293 test_separators(dict, '"', '=');
294 test_separators(dict, '\'', '=');
295 test_separators(dict, ',', '"');
296 test_separators(dict, ',', '\'');
297 test_separators(dict, '\'', '"');
298 test_separators(dict, '"', '\'');