FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dict.c
Go to the documentation of this file.
1 /*
2  * copyright (c) 2009 Michael Niedermayer
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include <string.h>
22 
23 #include "avstring.h"
24 #include "dict.h"
25 #include "internal.h"
26 #include "mem.h"
27 #include "time_internal.h"
28 #include "bprint.h"
29 
30 struct AVDictionary {
31  int count;
33 };
34 
36 {
37  return m ? m->count : 0;
38 }
39 
40 AVDictionaryEntry *av_dict_get(const AVDictionary *m, const char *key,
41  const AVDictionaryEntry *prev, int flags)
42 {
43  unsigned int i, j;
44 
45  if (!m)
46  return NULL;
47 
48  if (prev)
49  i = prev - m->elems + 1;
50  else
51  i = 0;
52 
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++)
57  ;
58  else
59  for (j = 0; av_toupper(s[j]) == av_toupper(key[j]) && key[j]; j++)
60  ;
61  if (key[j])
62  continue;
63  if (s[j] && !(flags & AV_DICT_IGNORE_SUFFIX))
64  continue;
65  return &m->elems[i];
66  }
67  return NULL;
68 }
69 
70 int av_dict_set(AVDictionary **pm, const char *key, const char *value,
71  int flags)
72 {
73  AVDictionary *m = *pm;
75  char *oldval = NULL, *copy_key = NULL, *copy_value = NULL;
76 
77  if (!(flags & AV_DICT_MULTIKEY)) {
78  tag = av_dict_get(m, key, NULL, flags);
79  }
80  if (flags & AV_DICT_DONT_STRDUP_KEY)
81  copy_key = (void *)key;
82  else
83  copy_key = av_strdup(key);
84  if (flags & AV_DICT_DONT_STRDUP_VAL)
85  copy_value = (void *)value;
86  else if (copy_key)
87  copy_value = av_strdup(value);
88  if (!m)
89  m = *pm = av_mallocz(sizeof(*m));
90  if (!m || (key && !copy_key) || (value && !copy_value))
91  goto err_out;
92 
93  if (tag) {
94  if (flags & AV_DICT_DONT_OVERWRITE) {
95  av_free(copy_key);
96  av_free(copy_value);
97  return 0;
98  }
99  if (flags & AV_DICT_APPEND)
100  oldval = tag->value;
101  else
102  av_free(tag->value);
103  av_free(tag->key);
104  *tag = m->elems[--m->count];
105  } else if (copy_value) {
107  (m->count + 1) * sizeof(*m->elems));
108  if (!tmp)
109  goto err_out;
110  m->elems = tmp;
111  }
112  if (copy_value) {
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);
118  if (!newval)
119  goto err_out;
120  av_strlcat(newval, oldval, len);
121  av_freep(&oldval);
122  av_strlcat(newval, copy_value, len);
123  m->elems[m->count].value = newval;
124  av_freep(&copy_value);
125  }
126  m->count++;
127  } else {
128  av_freep(&copy_key);
129  }
130  if (!m->count) {
131  av_freep(&m->elems);
132  av_freep(pm);
133  }
134 
135  return 0;
136 
137 err_out:
138  if (m && !m->count) {
139  av_freep(&m->elems);
140  av_freep(pm);
141  }
142  av_free(copy_key);
143  av_free(copy_value);
144  return AVERROR(ENOMEM);
145 }
146 
147 int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value,
148  int flags)
149 {
150  char valuestr[22];
151  snprintf(valuestr, sizeof(valuestr), "%"PRId64, value);
152  flags &= ~AV_DICT_DONT_STRDUP_VAL;
153  return av_dict_set(pm, key, valuestr, flags);
154 }
155 
156 static int parse_key_value_pair(AVDictionary **pm, const char **buf,
157  const char *key_val_sep, const char *pairs_sep,
158  int flags)
159 {
160  char *key = av_get_token(buf, key_val_sep);
161  char *val = NULL;
162  int ret;
163 
164  if (key && *key && strspn(*buf, key_val_sep)) {
165  (*buf)++;
166  val = av_get_token(buf, pairs_sep);
167  }
168 
169  if (key && *key && val && *val)
170  ret = av_dict_set(pm, key, val, flags);
171  else
172  ret = AVERROR(EINVAL);
173 
174  av_freep(&key);
175  av_freep(&val);
176 
177  return ret;
178 }
179 
180 int av_dict_parse_string(AVDictionary **pm, const char *str,
181  const char *key_val_sep, const char *pairs_sep,
182  int flags)
183 {
184  int ret;
185 
186  if (!str)
187  return 0;
188 
189  /* ignore STRDUP flags */
191 
192  while (*str) {
193  if ((ret = parse_key_value_pair(pm, &str, key_val_sep, pairs_sep, flags)) < 0)
194  return ret;
195 
196  if (*str)
197  str++;
198  }
199 
200  return 0;
201 }
202 
204 {
205  AVDictionary *m = *pm;
206 
207  if (m) {
208  while (m->count--) {
209  av_freep(&m->elems[m->count].key);
210  av_freep(&m->elems[m->count].value);
211  }
212  av_freep(&m->elems);
213  }
214  av_freep(pm);
215 }
216 
218 {
219  AVDictionaryEntry *t = NULL;
220 
221  while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX))) {
222  int ret = av_dict_set(dst, t->key, t->value, flags);
223  if (ret < 0)
224  return ret;
225  }
226 
227  return 0;
228 }
229 
231  const char key_val_sep, const char pairs_sep)
232 {
233  AVDictionaryEntry *t = NULL;
234  AVBPrint bprint;
235  int cnt = 0;
236  char special_chars[] = {pairs_sep, key_val_sep, '\0'};
237 
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);
241 
242  if (!av_dict_count(m)) {
243  *buffer = av_strdup("");
244  return *buffer ? 0 : AVERROR(ENOMEM);
245  }
246 
248  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) {
249  if (cnt++)
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);
254  }
255  return av_bprint_finalize(&bprint, buffer);
256 }
257 
258 int avpriv_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestamp)
259 {
260  time_t seconds = timestamp / 1000000;
261  struct tm *ptm, tmbuf;
262  ptm = gmtime_r(&seconds, &tmbuf);
263  if (ptm) {
264  char buf[32];
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);
269  } else {
270  return AVERROR_EXTERNAL;
271  }
272 }
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
int count
Definition: dict.c:31
const char * s
Definition: avisynth_c.h:768
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:145
Memory handling functions.
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
Definition: dict.h:79
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition: dict.c:35
int av_dict_get_string(const AVDictionary *m, char **buffer, const char key_val_sep, const char pairs_sep)
Get dictionary entries as a string.
Definition: dict.c:230
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:217
Use backslash escaping.
Definition: avstring.h:310
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:252
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that's been allocated with av_malloc() or another memory allocation function...
Definition: dict.h:73
Public dictionary API.
int avpriv_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestamp)
Set a dictionary value to an ISO-8601 compliant timestamp string.
Definition: dict.c:258
#define AV_DICT_MULTIKEY
Allow to store several equal keys in the dictionary.
Definition: dict.h:83
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
Definition: bprint.c:158
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
uint32_t tag
Definition: movenc.c:1376
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
Definition: dict.h:69
#define AV_BPRINT_SIZE_UNLIMITED
#define AVERROR(e)
Definition: error.h:43
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
Definition: avstring.c:149
static struct tm * gmtime_r(const time_t *clock, struct tm *result)
Definition: time_internal.h:26
common internal API header
AVDictionaryEntry * elems
Definition: dict.c:32
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:76
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
#define AV_DICT_APPEND
If the entry already exists, append to it.
Definition: dict.h:80
#define src
Definition: vp9dsp.c:530
int av_dict_parse_string(AVDictionary **pm, const char *str, const char *key_val_sep, const char *pairs_sep, int flags)
Parse the key/value pairs list and add the parsed entries to a dictionary.
Definition: dict.c:180
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:267
void * buf
Definition: avisynth_c.h:690
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
static av_const int av_toupper(int c)
Locale-independent conversion of ASCII characters to uppercase.
Definition: avstring.h:231
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:101
#define snprintf
Definition: snprintf.h:34
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
Definition: avstring.c:93
static int flags
Definition: cpu.c:47
void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_chars, enum AVEscapeMode mode, int flags)
Escape the content in src and append it to dstbuf.
Definition: bprint.c:265
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set that converts the value to a string and stores it...
Definition: dict.c:147
char * key
Definition: dict.h:86
#define av_free(p)
char * value
Definition: dict.h:87
int len
static uint8_t tmp[8]
Definition: des.c:38
#define av_freep(p)
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:70
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
static int parse_key_value_pair(AVDictionary **pm, const char **buf, const char *key_val_sep, const char *pairs_sep, int flags)
Definition: dict.c:156
GLuint buffer
Definition: opengl_enc.c:102