dict.c 4.7 KB

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