dict.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. struct AVDictionary {
  26. int count;
  27. AVDictionaryEntry *elems;
  28. };
  29. int av_dict_count(const AVDictionary *m)
  30. {
  31. return m ? m->count : 0;
  32. }
  33. AVDictionaryEntry *av_dict_get(FF_CONST_AVUTIL53 AVDictionary *m, const char *key,
  34. const AVDictionaryEntry *prev, int flags)
  35. {
  36. unsigned int i, j;
  37. if (!m)
  38. return NULL;
  39. if (prev)
  40. i = prev - m->elems + 1;
  41. else
  42. i = 0;
  43. for (; i < m->count; i++) {
  44. const char *s = m->elems[i].key;
  45. if (flags & AV_DICT_MATCH_CASE)
  46. for (j = 0; s[j] == key[j] && key[j]; j++)
  47. ;
  48. else
  49. for (j = 0; av_toupper(s[j]) == av_toupper(key[j]) && key[j]; j++)
  50. ;
  51. if (key[j])
  52. continue;
  53. if (s[j] && !(flags & AV_DICT_IGNORE_SUFFIX))
  54. continue;
  55. return &m->elems[i];
  56. }
  57. return NULL;
  58. }
  59. int av_dict_set(AVDictionary **pm, const char *key, const char *value,
  60. int flags)
  61. {
  62. AVDictionary *m = *pm;
  63. AVDictionaryEntry *tag = av_dict_get(m, key, NULL, flags);
  64. char *oldval = NULL;
  65. if (!m)
  66. m = *pm = av_mallocz(sizeof(*m));
  67. if (tag) {
  68. if (flags & AV_DICT_DONT_OVERWRITE) {
  69. if (flags & AV_DICT_DONT_STRDUP_KEY) av_free((void*)key);
  70. if (flags & AV_DICT_DONT_STRDUP_VAL) av_free((void*)value);
  71. return 0;
  72. }
  73. if (flags & AV_DICT_APPEND)
  74. oldval = tag->value;
  75. else
  76. av_free(tag->value);
  77. av_free(tag->key);
  78. *tag = m->elems[--m->count];
  79. } else {
  80. AVDictionaryEntry *tmp = av_realloc(m->elems,
  81. (m->count + 1) * sizeof(*m->elems));
  82. if (!tmp)
  83. goto err_out;
  84. m->elems = tmp;
  85. }
  86. if (value) {
  87. if (flags & AV_DICT_DONT_STRDUP_KEY)
  88. m->elems[m->count].key = (char*)(intptr_t)key;
  89. else
  90. m->elems[m->count].key = av_strdup(key);
  91. if (flags & AV_DICT_DONT_STRDUP_VAL) {
  92. m->elems[m->count].value = (char*)(intptr_t)value;
  93. } else if (oldval && flags & AV_DICT_APPEND) {
  94. size_t len = strlen(oldval) + strlen(value) + 1;
  95. char *newval = av_mallocz(len);
  96. if (!newval)
  97. goto err_out;
  98. av_strlcat(newval, oldval, len);
  99. av_freep(&oldval);
  100. av_strlcat(newval, value, len);
  101. m->elems[m->count].value = newval;
  102. } else
  103. m->elems[m->count].value = av_strdup(value);
  104. m->count++;
  105. }
  106. if (!m->count) {
  107. av_free(m->elems);
  108. av_freep(pm);
  109. }
  110. return 0;
  111. err_out:
  112. if (!m->count) {
  113. av_free(m->elems);
  114. av_freep(pm);
  115. }
  116. if (flags & AV_DICT_DONT_STRDUP_KEY) av_free((void*)key);
  117. if (flags & AV_DICT_DONT_STRDUP_VAL) av_free((void*)value);
  118. return AVERROR(ENOMEM);
  119. }
  120. int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value,
  121. int flags)
  122. {
  123. char valuestr[22];
  124. snprintf(valuestr, sizeof(valuestr), "%"PRId64, value);
  125. return av_dict_set(pm, key, valuestr, flags);
  126. }
  127. static int parse_key_value_pair(AVDictionary **pm, const char **buf,
  128. const char *key_val_sep, const char *pairs_sep,
  129. int flags)
  130. {
  131. char *key = av_get_token(buf, key_val_sep);
  132. char *val = NULL;
  133. int ret;
  134. if (key && *key && strspn(*buf, key_val_sep)) {
  135. (*buf)++;
  136. val = av_get_token(buf, pairs_sep);
  137. }
  138. if (key && *key && val && *val)
  139. ret = av_dict_set(pm, key, val, flags);
  140. else
  141. ret = AVERROR(EINVAL);
  142. av_freep(&key);
  143. av_freep(&val);
  144. return ret;
  145. }
  146. int av_dict_parse_string(AVDictionary **pm, const char *str,
  147. const char *key_val_sep, const char *pairs_sep,
  148. int flags)
  149. {
  150. int ret;
  151. if (!str)
  152. return 0;
  153. /* ignore STRDUP flags */
  154. flags &= ~(AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
  155. while (*str) {
  156. if ((ret = parse_key_value_pair(pm, &str, key_val_sep, pairs_sep, flags)) < 0)
  157. return ret;
  158. if (*str)
  159. str++;
  160. }
  161. return 0;
  162. }
  163. void av_dict_free(AVDictionary **pm)
  164. {
  165. AVDictionary *m = *pm;
  166. if (m) {
  167. while (m->count--) {
  168. av_free(m->elems[m->count].key);
  169. av_free(m->elems[m->count].value);
  170. }
  171. av_free(m->elems);
  172. }
  173. av_freep(pm);
  174. }
  175. void av_dict_copy(AVDictionary **dst, FF_CONST_AVUTIL53 AVDictionary *src, int flags)
  176. {
  177. AVDictionaryEntry *t = NULL;
  178. while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX)))
  179. av_dict_set(dst, t->key, t->value, flags);
  180. }