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 "bprint.h"
28 
29 struct AVDictionary {
30  int count;
32 };
33 
35 {
36  return m ? m->count : 0;
37 }
38 
39 AVDictionaryEntry *av_dict_get(const AVDictionary *m, const char *key,
40  const AVDictionaryEntry *prev, int flags)
41 {
42  unsigned int i, j;
43 
44  if (!m)
45  return NULL;
46 
47  if (prev)
48  i = prev - m->elems + 1;
49  else
50  i = 0;
51 
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++)
56  ;
57  else
58  for (j = 0; av_toupper(s[j]) == av_toupper(key[j]) && key[j]; j++)
59  ;
60  if (key[j])
61  continue;
62  if (s[j] && !(flags & AV_DICT_IGNORE_SUFFIX))
63  continue;
64  return &m->elems[i];
65  }
66  return NULL;
67 }
68 
69 int av_dict_set(AVDictionary **pm, const char *key, const char *value,
70  int flags)
71 {
72  AVDictionary *m = *pm;
73  AVDictionaryEntry *tag = av_dict_get(m, key, NULL, flags);
74  char *oldval = NULL, *copy_key = NULL, *copy_value = NULL;
75 
76  if (flags & AV_DICT_DONT_STRDUP_KEY)
77  copy_key = (void *)key;
78  else
79  copy_key = av_strdup(key);
80  if (flags & AV_DICT_DONT_STRDUP_VAL)
81  copy_value = (void *)value;
82  else if (copy_key)
83  copy_value = av_strdup(value);
84  if (!m)
85  m = *pm = av_mallocz(sizeof(*m));
86  if (!m || (key && !copy_key) || (value && !copy_value))
87  goto err_out;
88 
89  if (tag) {
90  if (flags & AV_DICT_DONT_OVERWRITE) {
91  av_free(copy_key);
92  av_free(copy_value);
93  return 0;
94  }
95  if (flags & AV_DICT_APPEND)
96  oldval = tag->value;
97  else
98  av_free(tag->value);
99  av_free(tag->key);
100  *tag = m->elems[--m->count];
101  } else {
103  (m->count + 1) * sizeof(*m->elems));
104  if (!tmp)
105  goto err_out;
106  m->elems = tmp;
107  }
108  if (copy_value) {
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);
114  if (!newval)
115  goto err_out;
116  av_strlcat(newval, oldval, len);
117  av_freep(&oldval);
118  av_strlcat(newval, copy_value, len);
119  m->elems[m->count].value = newval;
120  av_freep(&copy_value);
121  }
122  m->count++;
123  } else {
124  av_freep(&copy_key);
125  }
126  if (!m->count) {
127  av_freep(&m->elems);
128  av_freep(pm);
129  }
130 
131  return 0;
132 
133 err_out:
134  if (m && !m->count) {
135  av_freep(&m->elems);
136  av_freep(pm);
137  }
138  av_free(copy_key);
139  av_free(copy_value);
140  return AVERROR(ENOMEM);
141 }
142 
143 int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value,
144  int flags)
145 {
146  char valuestr[22];
147  snprintf(valuestr, sizeof(valuestr), "%"PRId64, value);
148  flags &= ~AV_DICT_DONT_STRDUP_VAL;
149  return av_dict_set(pm, key, valuestr, flags);
150 }
151 
152 static int parse_key_value_pair(AVDictionary **pm, const char **buf,
153  const char *key_val_sep, const char *pairs_sep,
154  int flags)
155 {
156  char *key = av_get_token(buf, key_val_sep);
157  char *val = NULL;
158  int ret;
159 
160  if (key && *key && strspn(*buf, key_val_sep)) {
161  (*buf)++;
162  val = av_get_token(buf, pairs_sep);
163  }
164 
165  if (key && *key && val && *val)
166  ret = av_dict_set(pm, key, val, flags);
167  else
168  ret = AVERROR(EINVAL);
169 
170  av_freep(&key);
171  av_freep(&val);
172 
173  return ret;
174 }
175 
176 int av_dict_parse_string(AVDictionary **pm, const char *str,
177  const char *key_val_sep, const char *pairs_sep,
178  int flags)
179 {
180  int ret;
181 
182  if (!str)
183  return 0;
184 
185  /* ignore STRDUP flags */
187 
188  while (*str) {
189  if ((ret = parse_key_value_pair(pm, &str, key_val_sep, pairs_sep, flags)) < 0)
190  return ret;
191 
192  if (*str)
193  str++;
194  }
195 
196  return 0;
197 }
198 
200 {
201  AVDictionary *m = *pm;
202 
203  if (m) {
204  while (m->count--) {
205  av_freep(&m->elems[m->count].key);
206  av_freep(&m->elems[m->count].value);
207  }
208  av_freep(&m->elems);
209  }
210  av_freep(pm);
211 }
212 
214 {
215  AVDictionaryEntry *t = NULL;
216 
217  while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX))) {
218  int ret = av_dict_set(dst, t->key, t->value, flags);
219  if (ret < 0)
220  return ret;
221  }
222 
223  return 0;
224 }
225 
227  const char key_val_sep, const char pairs_sep)
228 {
229  AVDictionaryEntry *t = NULL;
230  AVBPrint bprint;
231  int cnt = 0;
232  char special_chars[] = {pairs_sep, key_val_sep, '\0'};
233 
234  if (!buffer || pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
235  pairs_sep == '\\' || key_val_sep == '\\')
236  return AVERROR(EINVAL);
237 
238  if (!av_dict_count(m)) {
239  *buffer = av_strdup("");
240  return *buffer ? 0 : AVERROR(ENOMEM);
241  }
242 
244  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) {
245  if (cnt++)
246  av_bprint_append_data(&bprint, &pairs_sep, 1);
247  av_bprint_escape(&bprint, t->key, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
248  av_bprint_append_data(&bprint, &key_val_sep, 1);
249  av_bprint_escape(&bprint, t->value, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
250  }
251  return av_bprint_finalize(&bprint, buffer);
252 }
253 
254 #ifdef TEST
255 static void print_dict(const AVDictionary *m)
256 {
257  AVDictionaryEntry *t = NULL;
258  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
259  printf("%s %s ", t->key, t->value);
260  printf("\n");
261 }
262 
263 static void test_separators(const AVDictionary *m, const char pair, const char val)
264 {
265  AVDictionary *dict = NULL;
266  char pairs[] = {pair , '\0'};
267  char vals[] = {val, '\0'};
268 
269  char *buffer = NULL;
270  av_dict_copy(&dict, m, 0);
271  print_dict(dict);
272  av_dict_get_string(dict, &buffer, val, pair);
273  printf("%s\n", buffer);
274  av_dict_free(&dict);
275  av_dict_parse_string(&dict, buffer, vals, pairs, 0);
276  av_freep(&buffer);
277  print_dict(dict);
278  av_dict_free(&dict);
279 }
280 
281 int main(void)
282 {
283  AVDictionary *dict = NULL;
285  char *buffer = NULL;
286 
287  printf("Testing av_dict_get_string() and av_dict_parse_string()\n");
288  av_dict_get_string(dict, &buffer, '=', ',');
289  printf("%s\n", buffer);
290  av_freep(&buffer);
291  av_dict_set(&dict, "aaa", "aaa", 0);
292  av_dict_set(&dict, "b,b", "bbb", 0);
293  av_dict_set(&dict, "c=c", "ccc", 0);
294  av_dict_set(&dict, "ddd", "d,d", 0);
295  av_dict_set(&dict, "eee", "e=e", 0);
296  av_dict_set(&dict, "f,f", "f=f", 0);
297  av_dict_set(&dict, "g=g", "g,g", 0);
298  test_separators(dict, ',', '=');
299  av_dict_free(&dict);
300  av_dict_set(&dict, "aaa", "aaa", 0);
301  av_dict_set(&dict, "bbb", "bbb", 0);
302  av_dict_set(&dict, "ccc", "ccc", 0);
303  av_dict_set(&dict, "\\,=\'\"", "\\,=\'\"", 0);
304  test_separators(dict, '"', '=');
305  test_separators(dict, '\'', '=');
306  test_separators(dict, ',', '"');
307  test_separators(dict, ',', '\'');
308  test_separators(dict, '\'', '"');
309  test_separators(dict, '"', '\'');
310  av_dict_free(&dict);
311 
312  printf("\nTesting av_dict_set()\n");
313  av_dict_set(&dict, "a", "a", 0);
314  av_dict_set(&dict, "b", av_strdup("b"), AV_DICT_DONT_STRDUP_VAL);
315  av_dict_set(&dict, av_strdup("c"), "c", AV_DICT_DONT_STRDUP_KEY);
317  av_dict_set(&dict, "e", "e", AV_DICT_DONT_OVERWRITE);
318  av_dict_set(&dict, "e", "f", AV_DICT_DONT_OVERWRITE);
319  av_dict_set(&dict, "f", "f", 0);
320  av_dict_set(&dict, "f", NULL, 0);
321  av_dict_set(&dict, "ff", "f", 0);
322  av_dict_set(&dict, "ff", "f", AV_DICT_APPEND);
323  e = NULL;
324  while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
325  printf("%s %s\n", e->key, e->value);
326  av_dict_free(&dict);
327 
328  av_dict_set(&dict, NULL, "a", 0);
329  av_dict_set(&dict, NULL, "b", 0);
330  av_dict_get(dict, NULL, NULL, 0);
331  e = NULL;
332  while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
333  printf("'%s' '%s'\n", e->key, e->value);
334  av_dict_free(&dict);
335 
336 
337  //valgrind sensible test
338  printf("\nTesting av_dict_set_int()\n");
342  av_dict_set_int(&dict, "4", 4, 0);
343  av_dict_set_int(&dict, "5", 5, AV_DICT_DONT_OVERWRITE);
344  av_dict_set_int(&dict, "5", 6, AV_DICT_DONT_OVERWRITE);
345  av_dict_set_int(&dict, "12", 1, 0);
346  av_dict_set_int(&dict, "12", 2, AV_DICT_APPEND);
347  e = NULL;
348  while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
349  printf("%s %s\n", e->key, e->value);
350  av_dict_free(&dict);
351 
352  //valgrind sensible test
353  printf("\nTesting av_dict_set() with existing AVDictionaryEntry.key as key\n");
354  av_dict_set(&dict, "key", "old", 0);
355  e = av_dict_get(dict, "key", NULL, 0);
356  av_dict_set(&dict, e->key, "new val OK", 0);
357  e = av_dict_get(dict, "key", NULL, 0);
358  printf("%s\n", e->value);
359  av_dict_set(&dict, e->key, e->value, 0);
360  e = av_dict_get(dict, "key", NULL, 0);
361  printf("%s\n", e->value);
362  av_dict_free(&dict);
363 
364  return 0;
365 }
366 #endif
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:634
int count
Definition: dict.c:30
const char * s
Definition: avisynth_c.h:631
memory handling functions
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
Definition: dict.h:81
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition: dict.c:34
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:226
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:213
Use backslash escaping.
Definition: avstring.h:310
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:75
Public dictionary API.
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:39
uint32_t tag
Definition: movenc.c:1348
unsigned m
Definition: audioconvert.c:187
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
Definition: dict.h:71
#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:199
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
common internal API header
AVDictionaryEntry * elems
Definition: dict.c:31
#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:78
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:82
#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:176
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:267
void * buf
Definition: avisynth_c.h:553
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:69
static av_const int av_toupper(int c)
Locale-independent conversion of ASCII characters to uppercase.
Definition: avstring.h:231
#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
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:145
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:143
char * key
Definition: dict.h:87
#define av_free(p)
char * value
Definition: dict.h:88
int len
#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:72
int main(int argc, char **argv)
Definition: main.c:22
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
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:152
GLuint buffer
Definition: opengl_enc.c:102