dict.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include "libavutil/dict.c"
  21. static void print_dict(const AVDictionary *m)
  22. {
  23. AVDictionaryEntry *t = NULL;
  24. while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
  25. printf("%s %s ", t->key, t->value);
  26. printf("\n");
  27. }
  28. static void test_separators(const AVDictionary *m, const char pair, const char val)
  29. {
  30. AVDictionary *dict = NULL;
  31. char pairs[] = {pair , '\0'};
  32. char vals[] = {val, '\0'};
  33. char *buffer = NULL;
  34. int ret;
  35. av_dict_copy(&dict, m, 0);
  36. print_dict(dict);
  37. av_dict_get_string(dict, &buffer, val, pair);
  38. printf("%s\n", buffer);
  39. av_dict_free(&dict);
  40. ret = av_dict_parse_string(&dict, buffer, vals, pairs, 0);
  41. printf("ret %d\n", ret);
  42. av_freep(&buffer);
  43. print_dict(dict);
  44. av_dict_free(&dict);
  45. }
  46. int main(void)
  47. {
  48. AVDictionary *dict = NULL;
  49. AVDictionaryEntry *e;
  50. char *buffer = NULL;
  51. printf("Testing av_dict_get_string() and av_dict_parse_string()\n");
  52. av_dict_get_string(dict, &buffer, '=', ',');
  53. printf("%s\n", buffer);
  54. av_freep(&buffer);
  55. av_dict_set(&dict, "aaa", "aaa", 0);
  56. av_dict_set(&dict, "b,b", "bbb", 0);
  57. av_dict_set(&dict, "c=c", "ccc", 0);
  58. av_dict_set(&dict, "ddd", "d,d", 0);
  59. av_dict_set(&dict, "eee", "e=e", 0);
  60. av_dict_set(&dict, "f,f", "f=f", 0);
  61. av_dict_set(&dict, "g=g", "g,g", 0);
  62. test_separators(dict, ',', '=');
  63. av_dict_free(&dict);
  64. av_dict_set(&dict, "aaa", "aaa", 0);
  65. av_dict_set(&dict, "bbb", "bbb", 0);
  66. av_dict_set(&dict, "ccc", "ccc", 0);
  67. av_dict_set(&dict, "\\,=\'\"", "\\,=\'\"", 0);
  68. test_separators(dict, '"', '=');
  69. test_separators(dict, '\'', '=');
  70. test_separators(dict, ',', '"');
  71. test_separators(dict, ',', '\'');
  72. test_separators(dict, '\'', '"');
  73. test_separators(dict, '"', '\'');
  74. av_dict_free(&dict);
  75. printf("\nTesting av_dict_set()\n");
  76. av_dict_set(&dict, "a", "a", 0);
  77. av_dict_set(&dict, "b", av_strdup("b"), AV_DICT_DONT_STRDUP_VAL);
  78. av_dict_set(&dict, av_strdup("c"), "c", AV_DICT_DONT_STRDUP_KEY);
  79. av_dict_set(&dict, av_strdup("d"), av_strdup("d"), AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
  80. av_dict_set(&dict, "e", "e", AV_DICT_DONT_OVERWRITE);
  81. av_dict_set(&dict, "e", "f", AV_DICT_DONT_OVERWRITE);
  82. av_dict_set(&dict, "f", "f", 0);
  83. av_dict_set(&dict, "f", NULL, 0);
  84. av_dict_set(&dict, "ff", "f", 0);
  85. av_dict_set(&dict, "ff", "f", AV_DICT_APPEND);
  86. e = NULL;
  87. while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
  88. printf("%s %s\n", e->key, e->value);
  89. av_dict_free(&dict);
  90. av_dict_set(&dict, NULL, "a", 0);
  91. av_dict_set(&dict, NULL, "b", 0);
  92. av_dict_get(dict, NULL, NULL, 0);
  93. e = NULL;
  94. while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
  95. printf("'%s' '%s'\n", e->key, e->value);
  96. av_dict_free(&dict);
  97. //valgrind sensible test
  98. printf("\nTesting av_dict_set_int()\n");
  99. av_dict_set_int(&dict, "1", 1, AV_DICT_DONT_STRDUP_VAL);
  100. av_dict_set_int(&dict, av_strdup("2"), 2, AV_DICT_DONT_STRDUP_KEY);
  101. av_dict_set_int(&dict, av_strdup("3"), 3, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
  102. av_dict_set_int(&dict, "4", 4, 0);
  103. av_dict_set_int(&dict, "5", 5, AV_DICT_DONT_OVERWRITE);
  104. av_dict_set_int(&dict, "5", 6, AV_DICT_DONT_OVERWRITE);
  105. av_dict_set_int(&dict, "12", 1, 0);
  106. av_dict_set_int(&dict, "12", 2, AV_DICT_APPEND);
  107. e = NULL;
  108. while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
  109. printf("%s %s\n", e->key, e->value);
  110. av_dict_free(&dict);
  111. //valgrind sensible test
  112. printf("\nTesting av_dict_set() with existing AVDictionaryEntry.key as key\n");
  113. av_dict_set(&dict, "key", "old", 0);
  114. e = av_dict_get(dict, "key", NULL, 0);
  115. av_dict_set(&dict, e->key, "new val OK", 0);
  116. e = av_dict_get(dict, "key", NULL, 0);
  117. printf("%s\n", e->value);
  118. av_dict_set(&dict, e->key, e->value, 0);
  119. e = av_dict_get(dict, "key", NULL, 0);
  120. printf("%s\n", e->value);
  121. av_dict_free(&dict);
  122. return 0;
  123. }