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/dict.c"
22 
23 static void print_dict(const AVDictionary *m)
24 {
26  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
27  printf("%s %s ", t->key, t->value);
28  printf("\n");
29 }
30 
31 static void test_separators(const AVDictionary *m, const char pair, const char val)
32 {
33  AVDictionary *dict = NULL;
34  char pairs[] = {pair , '\0'};
35  char vals[] = {val, '\0'};
36 
37  char *buffer = NULL;
38  int ret;
39 
40  av_dict_copy(&dict, m, 0);
41  print_dict(dict);
42  av_dict_get_string(dict, &buffer, val, pair);
43  printf("%s\n", buffer);
44  av_dict_free(&dict);
45  ret = av_dict_parse_string(&dict, buffer, vals, pairs, 0);
46  printf("ret %d\n", ret);
47  av_freep(&buffer);
48  print_dict(dict);
49  av_dict_free(&dict);
50 }
51 
52 int main(void)
53 {
54  AVDictionary *dict = NULL;
56  char *buffer = NULL;
57 
58  printf("Testing av_dict_get_string() and av_dict_parse_string()\n");
59  av_dict_get_string(dict, &buffer, '=', ',');
60  printf("%s\n", buffer);
61  av_freep(&buffer);
62  av_dict_set(&dict, "aaa", "aaa", 0);
63  av_dict_set(&dict, "b,b", "bbb", 0);
64  av_dict_set(&dict, "c=c", "ccc", 0);
65  av_dict_set(&dict, "ddd", "d,d", 0);
66  av_dict_set(&dict, "eee", "e=e", 0);
67  av_dict_set(&dict, "f,f", "f=f", 0);
68  av_dict_set(&dict, "g=g", "g,g", 0);
69  test_separators(dict, ',', '=');
70  av_dict_free(&dict);
71  av_dict_set(&dict, "aaa", "aaa", 0);
72  av_dict_set(&dict, "bbb", "bbb", 0);
73  av_dict_set(&dict, "ccc", "ccc", 0);
74  av_dict_set(&dict, "\\,=\'\"", "\\,=\'\"", 0);
75  test_separators(dict, '"', '=');
76  test_separators(dict, '\'', '=');
77  test_separators(dict, ',', '"');
78  test_separators(dict, ',', '\'');
79  test_separators(dict, '\'', '"');
80  test_separators(dict, '"', '\'');
81  av_dict_free(&dict);
82 
83  printf("\nTesting av_dict_set()\n");
84  av_dict_set(&dict, "a", "a", 0);
88  av_dict_set(&dict, "e", "e", AV_DICT_DONT_OVERWRITE);
89  av_dict_set(&dict, "e", "f", AV_DICT_DONT_OVERWRITE);
90  av_dict_set(&dict, "f", "f", 0);
91  av_dict_set(&dict, "f", NULL, 0);
92  av_dict_set(&dict, "ff", "f", 0);
93  av_dict_set(&dict, "ff", "f", AV_DICT_APPEND);
94  e = NULL;
95  while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
96  printf("%s %s\n", e->key, e->value);
97  av_dict_free(&dict);
98 
99  av_dict_set(&dict, NULL, "a", 0);
100  av_dict_set(&dict, NULL, "b", 0);
101  av_dict_get(dict, NULL, NULL, 0);
102  e = NULL;
103  while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
104  printf("'%s' '%s'\n", e->key, e->value);
105  av_dict_free(&dict);
106 
107 
108  //valgrind sensible test
109  printf("\nTesting av_dict_set_int()\n");
113  av_dict_set_int(&dict, "4", 4, 0);
114  av_dict_set_int(&dict, "5", 5, AV_DICT_DONT_OVERWRITE);
115  av_dict_set_int(&dict, "5", 6, AV_DICT_DONT_OVERWRITE);
116  av_dict_set_int(&dict, "12", 1, 0);
117  av_dict_set_int(&dict, "12", 2, AV_DICT_APPEND);
118  e = NULL;
119  while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
120  printf("%s %s\n", e->key, e->value);
121  av_dict_free(&dict);
122 
123  //valgrind sensible test
124  printf("\nTesting av_dict_set() with existing AVDictionaryEntry.key as key\n");
125  av_dict_set(&dict, "key", "old", 0);
126  e = av_dict_get(dict, "key", NULL, 0);
127  av_dict_set(&dict, e->key, "new val OK", 0);
128  e = av_dict_get(dict, "key", NULL, 0);
129  printf("%s\n", e->value);
130  av_dict_set(&dict, e->key, e->value, 0);
131  e = av_dict_get(dict, "key", NULL, 0);
132  printf("%s\n", e->value);
133  av_dict_free(&dict);
134 
135  return 0;
136 }
main
int main(void)
Definition: dict.c:52
AV_DICT_APPEND
#define AV_DICT_APPEND
If the entry already exists, append to it.
Definition: dict.h:77
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:70
AVDictionary
Definition: dict.c:30
print_dict
static void print_dict(const AVDictionary *m)
Definition: dict.c:23
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:74
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:40
AVDictionaryEntry::key
char * key
Definition: dict.h:82
NULL
#define NULL
Definition: coverity.c:32
AV_DICT_DONT_OVERWRITE
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
Definition: dict.h:76
printf
printf("static const uint8_t my_array[100] = {\n")
val
const char const char void * val
Definition: avisynth_c.h:863
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:203
test_separators
static void test_separators(const AVDictionary *m, const char pair, const char val)
Definition: dict.c:31
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:180
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:147
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:251
AVDictionaryEntry
Definition: dict.h:81
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:70
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:230
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:217
AVDictionaryEntry::value
char * value
Definition: dict.h:83
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:72