dict.c 8.4 KB

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