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
1515
1616 API changes, most recent first:
1717
18+2014-11-21 - xxxxxxx - lavu 54.15.100 - dict.h
19+ Add av_dict_get_string().
20+
1821 2014-11-18 - xxxxxxx - lavu 54.14.100 - float_dsp.h
1922 Add avpriv_float_dsp_alloc().
2023
libavutil/Makefile+1−0
@@ -157,6 +157,7 @@ TESTPROGS = adler32 \ · 156 unmodified lines
157157 cpu \
158158 crc \
159159 des \
160+ dict \
160161 error \
161162 eval \
162163 file \
libavutil/dict.c+90−0
@@ -24,6 +24,7 @@ · 23 unmodified lines
2424 #include "dict.h"
2525 #include "internal.h"
2626 #include "mem.h"
27+#include "bprint.h"
2728
2829 struct AVDictionary {
2930 int count;
@@ -207,3 +208,92 @@ void av_dict_copy(AVDictionary **dst, FF_CONST_AVUTIL53 AVDictionary *src, int f · 177 unmodified lines
207208 while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX)))
208209 av_dict_set(dst, t->key, t->value, flags);
209210 }
211+
212+int 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
241+static 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+
249+static 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+
267+int 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
171171 */
172172 void av_dict_free(AVDictionary **m);
173173
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+ */
189+int av_dict_get_string(const AVDictionary *m, char **buffer,
190+ const char key_val_sep, const char pairs_sep);
191+
174192 /**
175193 * @}
176194 */
libavutil/version.h+1−1
@@ -56,7 +56,7 @@ · 55 unmodified lines
5656 */
5757
5858 #define LIBAVUTIL_VERSION_MAJOR 54
59−#define LIBAVUTIL_VERSION_MINOR 14
59+#define LIBAVUTIL_VERSION_MINOR 15
6060 #define LIBAVUTIL_VERSION_MICRO 100
6161
6262 #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