dict.c 7.3 KB

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