FFmpeg
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 "libavutil/mem.h"
22 
23 #include "libavutil/dict.c"
24 
26  const AVDictionaryEntry *prev)
27 {
28  const AVDictionaryEntry *dict_get = av_dict_get(m, "", prev, AV_DICT_IGNORE_SUFFIX);
30 
31  if (dict_get != dict_iterate) {
32 #define GET(entry, mem) ((entry) ? (entry)->mem : "N/A")
33  printf("Iterating with av_dict_iterate() yields a different result "
34  "than iterating with av_dict_get() and AV_DICT_IGNORE_SUFFIX "
35  "(prev: %p, key %s; av_dict_iterate() %p, key %s, value %s; "
36  "av_dict_get() %p, key %s, value %s)\n",
37  prev, GET(prev, key),
39  dict_get, GET(dict_get, key), GET(dict_get, value));
40 #undef GET
41  }
42  return dict_iterate;
43 }
44 
45 static void print_dict(const AVDictionary *m)
46 {
47  const AVDictionaryEntry *t = NULL;
48  const char *sep = "";
49  while ((t = dict_iterate(m, t))) {
50  printf("%s%s %s", sep, t->key, t->value);
51  sep = " ";
52  }
53  printf("\n");
54 }
55 
56 static void test_separators(const AVDictionary *m, const char pair, const char val)
57 {
58  AVDictionary *dict = NULL;
59  char pairs[] = {pair , '\0'};
60  char vals[] = {val, '\0'};
61 
62  char *buffer = NULL;
63  int ret;
64 
65  av_dict_copy(&dict, m, 0);
66  print_dict(dict);
67  av_dict_get_string(dict, &buffer, val, pair);
68  printf("%s\n", buffer);
69  av_dict_free(&dict);
70  ret = av_dict_parse_string(&dict, buffer, vals, pairs, 0);
71  printf("ret %d\n", ret);
72  av_freep(&buffer);
73  print_dict(dict);
74  av_dict_free(&dict);
75 }
76 
77 int main(void)
78 {
79  AVDictionary *dict = NULL;
80  const AVDictionaryEntry *e;
81  char *buffer = NULL;
82 
83  printf("Testing av_dict_get_string() and av_dict_parse_string()\n");
84  av_dict_get_string(dict, &buffer, '=', ',');
85  printf("%s\n", buffer);
86  av_freep(&buffer);
87  av_dict_set(&dict, "aaa", "aaa", 0);
88  av_dict_set(&dict, "b,b", "bbb", 0);
89  av_dict_set(&dict, "c=c", "ccc", 0);
90  av_dict_set(&dict, "ddd", "d,d", 0);
91  av_dict_set(&dict, "eee", "e=e", 0);
92  av_dict_set(&dict, "f,f", "f=f", 0);
93  av_dict_set(&dict, "g=g", "g,g", 0);
94  test_separators(dict, ',', '=');
95  av_dict_free(&dict);
96  av_dict_set(&dict, "aaa", "aaa", 0);
97  av_dict_set(&dict, "bbb", "bbb", 0);
98  av_dict_set(&dict, "ccc", "ccc", 0);
99  av_dict_set(&dict, "\\,=\'\"", "\\,=\'\"", 0);
100  test_separators(dict, '"', '=');
101  test_separators(dict, '\'', '=');
102  test_separators(dict, ',', '"');
103  test_separators(dict, ',', '\'');
104  test_separators(dict, '\'', '"');
105  test_separators(dict, '"', '\'');
106  av_dict_free(&dict);
107 
108  printf("\nTesting av_dict_set()\n");
109  av_dict_set(&dict, "a", "a", 0);
110  av_dict_set(&dict, "b", av_strdup("b"), AV_DICT_DONT_STRDUP_VAL);
111  av_dict_set(&dict, av_strdup("c"), "c", AV_DICT_DONT_STRDUP_KEY);
113  av_dict_set(&dict, "e", "e", AV_DICT_DONT_OVERWRITE);
114  av_dict_set(&dict, "e", "f", AV_DICT_DONT_OVERWRITE);
115  av_dict_set(&dict, "f", "f", 0);
116  av_dict_set(&dict, "f", NULL, 0);
117  av_dict_set(&dict, "ff", "f", 0);
118  av_dict_set(&dict, "ff", "f", AV_DICT_APPEND);
119  if (av_dict_get(dict, NULL, NULL, 0))
120  printf("av_dict_get() does not correctly handle NULL key.\n");
121  e = NULL;
122  while ((e = dict_iterate(dict, e)))
123  printf("%s %s\n", e->key, e->value);
124  av_dict_free(&dict);
125 
126  if (av_dict_set(&dict, NULL, "a", 0) >= 0 ||
127  av_dict_set(&dict, NULL, "b", 0) >= 0 ||
128  av_dict_set(&dict, NULL, NULL, AV_DICT_DONT_STRDUP_KEY) >= 0 ||
129  av_dict_set(&dict, NULL, av_strdup("b"), AV_DICT_DONT_STRDUP_VAL) >= 0 ||
130  av_dict_count(dict))
131  printf("av_dict_set does not correctly handle NULL key\n");
132 
133  e = NULL;
134  while ((e = dict_iterate(dict, e)))
135  printf("'%s' '%s'\n", e->key, e->value);
136  av_dict_free(&dict);
137 
138 
139  //valgrind sensible test
140  printf("\nTesting av_dict_set_int()\n");
144  av_dict_set_int(&dict, "4", 4, 0);
145  av_dict_set_int(&dict, "5", 5, AV_DICT_DONT_OVERWRITE);
146  av_dict_set_int(&dict, "5", 6, AV_DICT_DONT_OVERWRITE);
147  av_dict_set_int(&dict, "12", 1, 0);
148  av_dict_set_int(&dict, "12", 2, AV_DICT_APPEND);
149  e = NULL;
150  while ((e = dict_iterate(dict, e)))
151  printf("%s %s\n", e->key, e->value);
152  av_dict_free(&dict);
153 
154  //valgrind sensible test
155  printf("\nTesting av_dict_set() with existing AVDictionaryEntry.key as key\n");
156  if (av_dict_set(&dict, "key", "old", 0) < 0)
157  return 1;
158  e = av_dict_get(dict, "key", NULL, 0);
159  if (av_dict_set(&dict, e->key, "new val OK", 0) < 0)
160  return 1;
161  e = av_dict_get(dict, "key", NULL, 0);
162  printf("%s\n", e->value);
163  if (av_dict_set(&dict, e->key, e->value, 0) < 0)
164  return 1;
165  e = av_dict_get(dict, "key", NULL, 0);
166  printf("%s\n", e->value);
167  av_dict_free(&dict);
168 
169  return 0;
170 }
GET
#define GET(entry, mem)
printf
__device__ int printf(const char *,...)
main
int main(void)
Definition: dict.c:77
av_dict_count
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition: dict.c:37
AV_DICT_APPEND
#define AV_DICT_APPEND
If the entry already exists, append to it.
Definition: dict.h:82
AV_DICT_IGNORE_SUFFIX
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition: dict.h:75
AVDictionary
Definition: dict.c:32
print_dict
static void print_dict(const AVDictionary *m)
Definition: dict.c:45
val
static double val(void *priv, double ch)
Definition: aeval.c:77
AV_DICT_DONT_STRDUP_VAL
#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:79
av_dict_get
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:60
dict_iterate
static const AVDictionaryEntry * dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Definition: dict.c:25
AVDictionaryEntry::key
char * key
Definition: dict.h:91
key
const char * key
Definition: hwcontext_opencl.c:189
NULL
#define NULL
Definition: coverity.c:32
AV_DICT_DONT_OVERWRITE
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
Definition: dict.h:81
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:233
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
test_separators
static void test_separators(const AVDictionary *m, const char pair, const char val)
Definition: dict.c:56
ret
ret
Definition: filter_design.txt:187
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
av_dict_parse_string
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:210
dict.c
av_dict_set_int
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:177
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:272
mem.h
AVDictionaryEntry
Definition: dict.h:90
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
av_dict_set
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:86
av_dict_get_string
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:260
av_dict_copy
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:247
AVDictionaryEntry::value
char * value
Definition: dict.h:92
av_dict_iterate
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition: dict.c:42
AV_DICT_DONT_STRDUP_KEY
#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:77