History

lavu/dict: add av_dict_get_string

Lukasz Marek committed 12 years ago

Commit ab922f91 parent 7d75a395 files · +113 −1Browse files

Unified · Split

5 files
  1. doc/APIchanges +3 −0
  2. libavutil/Makefile +1 −0
  3. libavutil/dict.c +90 −0
  4. libavutil/dict.h +18 −0
  5. libavutil/version.h +1 −1
doc/APIchanges+3−0
@@ -15,6 +15,9 @@ libavutil: 2014-08-09 · 14 unmodified lines
15 15
16API changes, most recent first:16API changes, most recent first:
17 17
182014-11-21 - xxxxxxx - lavu 54.15.100 - dict.h
19 Add av_dict_get_string().
20
182014-11-18 - xxxxxxx - lavu 54.14.100 - float_dsp.h212014-11-18 - xxxxxxx - lavu 54.14.100 - float_dsp.h
19 Add avpriv_float_dsp_alloc().22 Add avpriv_float_dsp_alloc().
20 23
libavutil/Makefile+1−0
@@ -157,6 +157,7 @@ TESTPROGS = adler32 \ · 156 unmodified lines
157 cpu \157 cpu \
158 crc \158 crc \
159 des \159 des \
160 dict \
160 error \161 error \
161 eval \162 eval \
162 file \163 file \
libavutil/dict.c+90−0
@@ -24,6 +24,7 @@ · 23 unmodified lines
24#include "dict.h"24#include "dict.h"
25#include "internal.h"25#include "internal.h"
26#include "mem.h"26#include "mem.h"
27#include "bprint.h"
27 28
28struct AVDictionary {29struct AVDictionary {
29 int count;30 int count;
@@ -207,3 +208,92 @@ void av_dict_copy(AVDictionary **dst, FF_CONST_AVUTIL53 AVDictionary *src, int f · 177 unmodified lines
207 while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX)))208 while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX)))
208 av_dict_set(dst, t->key, t->value, flags);209 av_dict_set(dst, t->key, t->value, flags);
209}210}
211
212int av_dict_get_string(const AVDictionary *m, char **buffer,
213 const char key_val_sep, const char pairs_sep)
214{
215 AVDictionaryEntry *t = NULL;
216 AVBPrint bprint;
217 int cnt = 0;
218 char special_chars[] = {pairs_sep, key_val_sep, '\0'};
219
220 if (!buffer || pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
221 pairs_sep == '\\' || key_val_sep == '\\')
222 return AVERROR(EINVAL);
223
224 if (!av_dict_count(m)) {
225 *buffer = av_strdup("");
226 return *buffer ? 0 : AVERROR(ENOMEM);
227 }
228
229 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
230 while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) {
231 if (cnt++)
232 av_bprint_append_data(&bprint, &pairs_sep, 1);
233 av_bprint_escape(&bprint, t->key, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
234 av_bprint_append_data(&bprint, &key_val_sep, 1);
235 av_bprint_escape(&bprint, t->value, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
236 }
237 return av_bprint_finalize(&bprint, buffer);
238}
239
240#ifdef TEST
241static void print_dict(const AVDictionary *m)
242{
243 AVDictionaryEntry *t = NULL;
244 while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
245 printf("%s %s ", t->key, t->value);
246 printf("\n");
247}
248
249static void test_separators(const AVDictionary *m, const char pair, const char val)
250{
251 AVDictionary *dict = NULL;
252 char pairs[] = {pair , '\0'};
253 char vals[] = {val, '\0'};
254
255 char *buffer = NULL;
256 av_dict_copy(&dict, m, 0);
257 print_dict(dict);
258 av_dict_get_string(dict, &buffer, val, pair);
259 printf("%s\n", buffer);
260 av_dict_free(&dict);
261 av_dict_parse_string(&dict, buffer, vals, pairs, 0);
262 av_freep(&buffer);
263 print_dict(dict);
264 av_dict_free(&dict);
265}
266
267int main(void)
268{
269 AVDictionary *dict = NULL;
270 char *buffer = NULL;
271
272 printf("Testing av_dict_get_string() and av_dict_parse_string()\n");
273 av_dict_get_string(dict, &buffer, '=', ',');
274 printf("%s\n", buffer);
275 av_freep(&buffer);
276 av_dict_set(&dict, "aaa", "aaa", 0);
277 av_dict_set(&dict, "b,b", "bbb", 0);
278 av_dict_set(&dict, "c=c", "ccc", 0);
279 av_dict_set(&dict, "ddd", "d,d", 0);
280 av_dict_set(&dict, "eee", "e=e", 0);
281 av_dict_set(&dict, "f,f", "f=f", 0);
282 av_dict_set(&dict, "g=g", "g,g", 0);
283 test_separators(dict, ',', '=');
284 av_dict_free(&dict);
285 av_dict_set(&dict, "aaa", "aaa", 0);
286 av_dict_set(&dict, "bbb", "bbb", 0);
287 av_dict_set(&dict, "ccc", "ccc", 0);
288 av_dict_set(&dict, "\\,=\'\"", "\\,=\'\"", 0);
289 test_separators(dict, '"', '=');
290 test_separators(dict, '\'', '=');
291 test_separators(dict, ',', '"');
292 test_separators(dict, ',', '\'');
293 test_separators(dict, '\'', '"');
294 test_separators(dict, '"', '\'');
295 av_dict_free(&dict);
296
297 return 0;
298}
299#endif
libavutil/dict.h+18−0
@@ -171,6 +171,24 @@ void av_dict_copy(AVDictionary **dst, FF_CONST_AVUTIL53 AVDictionary *src, int f · 170 unmodified lines
171 */171 */
172void av_dict_free(AVDictionary **m);172void av_dict_free(AVDictionary **m);
173 173
174/**
175 * Get dictionary entries as a string.
176 *
177 * Create a string containing dictionary's entries.
178 * Such string may be passed back to av_dict_parse_string().
179 * @note String is escaped with backslashes ('\').
180 *
181 * @param[in] m dictionary
182 * @param[out] buffer Pointer to buffer that will be allocated with string containg entries.
183 * Buffer must be freed by the caller when is no longer needed.
184 * @param[in] key_val_sep character used to separate key from value
185 * @param[in] pairs_sep character used to separate two pairs from each other
186 * @return >= 0 on success, negative on error
187 * @warning Separators cannot be neither '\\' nor '\0'. They also cannot be the same.
188 */
189int av_dict_get_string(const AVDictionary *m, char **buffer,
190 const char key_val_sep, const char pairs_sep);
191
174/**192/**
175 * @}193 * @}
176 */194 */
libavutil/version.h+1−1
@@ -56,7 +56,7 @@ · 55 unmodified lines
56 */56 */
57 57
58#define LIBAVUTIL_VERSION_MAJOR 5458#define LIBAVUTIL_VERSION_MAJOR 54
59#define LIBAVUTIL_VERSION_MINOR 1459#define LIBAVUTIL_VERSION_MINOR 15
60#define LIBAVUTIL_VERSION_MICRO 10060#define LIBAVUTIL_VERSION_MICRO 100
61 61
62#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \62#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t