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
27 #include "time_internal.h"
32 AVDictionaryEntry *elems;
35 int av_dict_count(const AVDictionary *m)
37 return m ? m->count : 0;
40 AVDictionaryEntry *av_dict_get(const AVDictionary *m, const char *key,
41 const AVDictionaryEntry *prev, int flags)
49 i = prev - m->elems + 1;
53 for (; i < m->count; i++) {
54 const char *s = m->elems[i].key;
55 if (flags & AV_DICT_MATCH_CASE)
56 for (j = 0; s[j] == key[j] && key[j]; j++)
59 for (j = 0; av_toupper(s[j]) == av_toupper(key[j]) && key[j]; j++)
63 if (s[j] && !(flags & AV_DICT_IGNORE_SUFFIX))
70 int av_dict_set(AVDictionary **pm, const char *key, const char *value,
73 AVDictionary *m = *pm;
74 AVDictionaryEntry *tag = NULL;
75 char *oldval = NULL, *copy_key = NULL, *copy_value = NULL;
77 if (!(flags & AV_DICT_MULTIKEY)) {
78 tag = av_dict_get(m, key, NULL, flags);
80 if (flags & AV_DICT_DONT_STRDUP_KEY)
81 copy_key = (void *)key;
83 copy_key = av_strdup(key);
84 if (flags & AV_DICT_DONT_STRDUP_VAL)
85 copy_value = (void *)value;
87 copy_value = av_strdup(value);
89 m = *pm = av_mallocz(sizeof(*m));
90 if (!m || (key && !copy_key) || (value && !copy_value))
94 if (flags & AV_DICT_DONT_OVERWRITE) {
99 if (flags & AV_DICT_APPEND)
104 *tag = m->elems[--m->count];
105 } else if (copy_value) {
106 AVDictionaryEntry *tmp = av_realloc(m->elems,
107 (m->count + 1) * sizeof(*m->elems));
113 m->elems[m->count].key = copy_key;
114 m->elems[m->count].value = copy_value;
115 if (oldval && flags & AV_DICT_APPEND) {
116 size_t len = strlen(oldval) + strlen(copy_value) + 1;
117 char *newval = av_mallocz(len);
120 av_strlcat(newval, oldval, len);
122 av_strlcat(newval, copy_value, len);
123 m->elems[m->count].value = newval;
124 av_freep(©_value);
138 if (m && !m->count) {
144 return AVERROR(ENOMEM);
147 int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value,
151 snprintf(valuestr, sizeof(valuestr), "%"PRId64, value);
152 flags &= ~AV_DICT_DONT_STRDUP_VAL;
153 return av_dict_set(pm, key, valuestr, flags);
156 static int parse_key_value_pair(AVDictionary **pm, const char **buf,
157 const char *key_val_sep, const char *pairs_sep,
160 char *key = av_get_token(buf, key_val_sep);
164 if (key && *key && strspn(*buf, key_val_sep)) {
166 val = av_get_token(buf, pairs_sep);
169 if (key && *key && val && *val)
170 ret = av_dict_set(pm, key, val, flags);
172 ret = AVERROR(EINVAL);
180 int av_dict_parse_string(AVDictionary **pm, const char *str,
181 const char *key_val_sep, const char *pairs_sep,
189 /* ignore STRDUP flags */
190 flags &= ~(AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
193 if ((ret = parse_key_value_pair(pm, &str, key_val_sep, pairs_sep, flags)) < 0)
203 void av_dict_free(AVDictionary **pm)
205 AVDictionary *m = *pm;
209 av_freep(&m->elems[m->count].key);
210 av_freep(&m->elems[m->count].value);
217 int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
219 AVDictionaryEntry *t = NULL;
221 while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX))) {
222 int ret = av_dict_set(dst, t->key, t->value, flags);
230 int av_dict_get_string(const AVDictionary *m, char **buffer,
231 const char key_val_sep, const char pairs_sep)
233 AVDictionaryEntry *t = NULL;
236 char special_chars[] = {pairs_sep, key_val_sep, '\0'};
238 if (!buffer || pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
239 pairs_sep == '\\' || key_val_sep == '\\')
240 return AVERROR(EINVAL);
242 if (!av_dict_count(m)) {
243 *buffer = av_strdup("");
244 return *buffer ? 0 : AVERROR(ENOMEM);
247 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
248 while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) {
250 av_bprint_append_data(&bprint, &pairs_sep, 1);
251 av_bprint_escape(&bprint, t->key, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
252 av_bprint_append_data(&bprint, &key_val_sep, 1);
253 av_bprint_escape(&bprint, t->value, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
255 return av_bprint_finalize(&bprint, buffer);
258 int avpriv_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestamp)
260 time_t seconds = timestamp / 1000000;
261 struct tm *ptm, tmbuf;
262 ptm = gmtime_r(&seconds, &tmbuf);
265 if (!strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", ptm))
266 return AVERROR_EXTERNAL;
267 av_strlcatf(buf, sizeof(buf), ".%06dZ", (int)(timestamp % 1000000));
268 return av_dict_set(dict, key, buf, 0);
270 return AVERROR_EXTERNAL;