dict.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 <string.h>
  21. #include "avstring.h"
  22. #include "dict.h"
  23. #include "internal.h"
  24. #include "mem.h"
  25. #include "bprint.h"
  26. struct AVDictionary {
  27. int count;
  28. AVDictionaryEntry *elems;
  29. };
  30. int av_dict_count(const AVDictionary *m)
  31. {
  32. return m ? m->count : 0;
  33. }
  34. AVDictionaryEntry *av_dict_get(const AVDictionary *m, const char *key,
  35. const AVDictionaryEntry *prev, int flags)
  36. {
  37. unsigned int i, j;
  38. if (!m)
  39. return NULL;
  40. if (prev)
  41. i = prev - m->elems + 1;
  42. else
  43. i = 0;
  44. for (; i < m->count; i++) {
  45. const char *s = m->elems[i].key;
  46. if (flags & AV_DICT_MATCH_CASE)
  47. for (j = 0; s[j] == key[j] && key[j]; j++)
  48. ;
  49. else
  50. for (j = 0; av_toupper(s[j]) == av_toupper(key[j]) && key[j]; j++)
  51. ;
  52. if (key[j])
  53. continue;
  54. if (s[j] && !(flags & AV_DICT_IGNORE_SUFFIX))
  55. continue;
  56. return &m->elems[i];
  57. }
  58. return NULL;
  59. }
  60. int av_dict_set(AVDictionary **pm, const char *key, const char *value,
  61. int flags)
  62. {
  63. AVDictionary *m = *pm;
  64. AVDictionaryEntry *tag = av_dict_get(m, key, NULL, flags);
  65. char *oldval = NULL;
  66. if (!m)
  67. m = *pm = av_mallocz(sizeof(*m));
  68. if (tag) {
  69. if (flags & AV_DICT_DONT_OVERWRITE) {
  70. if (flags & AV_DICT_DONT_STRDUP_KEY) av_free((void*)key);
  71. if (flags & AV_DICT_DONT_STRDUP_VAL) av_free((void*)value);
  72. return 0;
  73. }
  74. if (flags & AV_DICT_APPEND)
  75. oldval = tag->value;
  76. else
  77. av_free(tag->value);
  78. av_free(tag->key);
  79. *tag = m->elems[--m->count];
  80. } else {
  81. AVDictionaryEntry *tmp = av_realloc(m->elems,
  82. (m->count + 1) * sizeof(*m->elems));
  83. if (!tmp)
  84. goto err_out;
  85. m->elems = tmp;
  86. }
  87. if (value) {
  88. if (flags & AV_DICT_DONT_STRDUP_KEY)
  89. m->elems[m->count].key = (char*)(intptr_t)key;
  90. else
  91. m->elems[m->count].key = av_strdup(key);
  92. if (flags & AV_DICT_DONT_STRDUP_VAL) {
  93. m->elems[m->count].value = (char*)(intptr_t)value;
  94. } else if (oldval && flags & AV_DICT_APPEND) {
  95. size_t len = strlen(oldval) + strlen(value) + 1;
  96. char *newval = av_mallocz(len);
  97. if (!newval)
  98. goto err_out;
  99. av_strlcat(newval, oldval, len);
  100. av_freep(&oldval);
  101. av_strlcat(newval, value, len);
  102. m->elems[m->count].value = newval;
  103. } else
  104. m->elems[m->count].value = av_strdup(value);
  105. m->count++;
  106. }
  107. if (!m->count) {
  108. av_free(m->elems);
  109. av_freep(pm);
  110. }
  111. return 0;
  112. err_out:
  113. if (!m->count) {
  114. av_free(m->elems);
  115. av_freep(pm);
  116. }
  117. if (flags & AV_DICT_DONT_STRDUP_KEY) av_free((void*)key);
  118. if (flags & AV_DICT_DONT_STRDUP_VAL) av_free((void*)value);
  119. return AVERROR(ENOMEM);
  120. }
  121. int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value,
  122. int flags)
  123. {
  124. char valuestr[22];
  125. snprintf(valuestr, sizeof(valuestr), "%"PRId64, value);
  126. return av_dict_set(pm, key, valuestr, flags);
  127. }
  128. static int parse_key_value_pair(AVDictionary **pm, const char **buf,
  129. const char *key_val_sep, const char *pairs_sep,
  130. int flags)
  131. {
  132. char *key = av_get_token(buf, key_val_sep);
  133. char *val = NULL;
  134. int ret;
  135. if (key && *key && strspn(*buf, key_val_sep)) {
  136. (*buf)++;
  137. val = av_get_token(buf, pairs_sep);
  138. }
  139. if (key && *key && val && *val)
  140. ret = av_dict_set(pm, key, val, flags);
  141. else
  142. ret = AVERROR(EINVAL);
  143. av_freep(&key);
  144. av_freep(&val);
  145. return ret;
  146. }
  147. int av_dict_parse_string(AVDictionary **pm, const char *str,
  148. const char *key_val_sep, const char *pairs_sep,
  149. int flags)
  150. {
  151. int ret;
  152. if (!str)
  153. return 0;
  154. /* ignore STRDUP flags */
  155. flags &= ~(AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
  156. while (*str) {
  157. if ((ret = parse_key_value_pair(pm, &str, key_val_sep, pairs_sep, flags)) < 0)
  158. return ret;
  159. if (*str)
  160. str++;
  161. }
  162. return 0;
  163. }
  164. void av_dict_free(AVDictionary **pm)
  165. {
  166. AVDictionary *m = *pm;
  167. if (m) {
  168. while (m->count--) {
  169. av_free(m->elems[m->count].key);
  170. av_free(m->elems[m->count].value);
  171. }
  172. av_free(m->elems);
  173. }
  174. av_freep(pm);
  175. }
  176. void av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
  177. {
  178. AVDictionaryEntry *t = NULL;
  179. while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX)))
  180. av_dict_set(dst, t->key, t->value, flags);
  181. }
  182. int av_dict_get_string(const AVDictionary *m, char **buffer,
  183. const char key_val_sep, const char pairs_sep)
  184. {
  185. AVDictionaryEntry *t = NULL;
  186. AVBPrint bprint;
  187. int cnt = 0;
  188. char special_chars[] = {pairs_sep, key_val_sep, '\0'};
  189. if (!buffer || pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
  190. pairs_sep == '\\' || key_val_sep == '\\')
  191. return AVERROR(EINVAL);
  192. if (!av_dict_count(m)) {
  193. *buffer = av_strdup("");
  194. return *buffer ? 0 : AVERROR(ENOMEM);
  195. }
  196. av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
  197. while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) {
  198. if (cnt++)
  199. av_bprint_append_data(&bprint, &pairs_sep, 1);
  200. av_bprint_escape(&bprint, t->key, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
  201. av_bprint_append_data(&bprint, &key_val_sep, 1);
  202. av_bprint_escape(&bprint, t->value, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
  203. }
  204. return av_bprint_finalize(&bprint, buffer);
  205. }
  206. #ifdef TEST
  207. static void print_dict(const AVDictionary *m)
  208. {
  209. AVDictionaryEntry *t = NULL;
  210. while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
  211. printf("%s %s ", t->key, t->value);
  212. printf("\n");
  213. }
  214. static void test_separators(const AVDictionary *m, const char pair, const char val)
  215. {
  216. AVDictionary *dict = NULL;
  217. char pairs[] = {pair , '\0'};
  218. char vals[] = {val, '\0'};
  219. char *buffer = NULL;
  220. av_dict_copy(&dict, m, 0);
  221. print_dict(dict);
  222. av_dict_get_string(dict, &buffer, val, pair);
  223. printf("%s\n", buffer);
  224. av_dict_free(&dict);
  225. av_dict_parse_string(&dict, buffer, vals, pairs, 0);
  226. av_freep(&buffer);
  227. print_dict(dict);
  228. av_dict_free(&dict);
  229. }
  230. int main(void)
  231. {
  232. AVDictionary *dict = NULL;
  233. char *buffer = NULL;
  234. printf("Testing av_dict_get_string() and av_dict_parse_string()\n");
  235. av_dict_get_string(dict, &buffer, '=', ',');
  236. printf("%s\n", buffer);
  237. av_freep(&buffer);
  238. av_dict_set(&dict, "aaa", "aaa", 0);
  239. av_dict_set(&dict, "b,b", "bbb", 0);
  240. av_dict_set(&dict, "c=c", "ccc", 0);
  241. av_dict_set(&dict, "ddd", "d,d", 0);
  242. av_dict_set(&dict, "eee", "e=e", 0);
  243. av_dict_set(&dict, "f,f", "f=f", 0);
  244. av_dict_set(&dict, "g=g", "g,g", 0);
  245. test_separators(dict, ',', '=');
  246. av_dict_free(&dict);
  247. av_dict_set(&dict, "aaa", "aaa", 0);
  248. av_dict_set(&dict, "bbb", "bbb", 0);
  249. av_dict_set(&dict, "ccc", "ccc", 0);
  250. av_dict_set(&dict, "\\,=\'\"", "\\,=\'\"", 0);
  251. test_separators(dict, '"', '=');
  252. test_separators(dict, '\'', '=');
  253. test_separators(dict, ',', '"');
  254. test_separators(dict, ',', '\'');
  255. test_separators(dict, '\'', '"');
  256. test_separators(dict, '"', '\'');
  257. av_dict_free(&dict);
  258. return 0;
  259. }
  260. #endif