| @@ -24,6 +24,7 @@ · 23 unmodified lines |
| 24 | 24 | #include "dict.h" |
| 25 | 25 | #include "internal.h" |
| 26 | 26 | #include "mem.h" |
| 27 | +#include "bprint.h" |
| 27 | 28 | |
| 28 | 29 | struct AVDictionary { |
| 29 | 30 | int count; |
| @@ -207,3 +208,92 @@ void av_dict_copy(AVDictionary **dst, FF_CONST_AVUTIL53 AVDictionary *src, int f · 177 unmodified lines |
| 207 | 208 | while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX))) |
| 208 | 209 | av_dict_set(dst, t->key, t->value, flags); |
| 209 | 210 | } |
| 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 |