dict.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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(key);
  70. if (flags & AV_DICT_DONT_STRDUP_VAL) av_free(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. m->elems = tmp;
  84. else
  85. return AVERROR(ENOMEM);
  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. int len = strlen(oldval) + strlen(value) + 1;
  96. char *newval = av_mallocz(len);
  97. if (!newval)
  98. return AVERROR(ENOMEM);
  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. }
  113. static int parse_key_value_pair(AVDictionary **pm, const char **buf,
  114. const char *key_val_sep, const char *pairs_sep,
  115. int flags)
  116. {
  117. char *key = av_get_token(buf, key_val_sep);
  118. char *val = NULL;
  119. int ret;
  120. if (key && *key && strspn(*buf, key_val_sep)) {
  121. (*buf)++;
  122. val = av_get_token(buf, pairs_sep);
  123. }
  124. if (key && *key && val && *val)
  125. ret = av_dict_set(pm, key, val, flags);
  126. else
  127. ret = AVERROR(EINVAL);
  128. av_freep(&key);
  129. av_freep(&val);
  130. return ret;
  131. }
  132. int av_dict_parse_string(AVDictionary **pm, const char *str,
  133. const char *key_val_sep, const char *pairs_sep,
  134. int flags)
  135. {
  136. int ret;
  137. if (!str)
  138. return 0;
  139. /* ignore STRDUP flags */
  140. flags &= ~(AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
  141. while (*str) {
  142. if ((ret = parse_key_value_pair(pm, &str, key_val_sep, pairs_sep, flags)) < 0)
  143. return ret;
  144. if (*str)
  145. str++;
  146. }
  147. return 0;
  148. }
  149. void av_dict_free(AVDictionary **pm)
  150. {
  151. AVDictionary *m = *pm;
  152. if (m) {
  153. while (m->count--) {
  154. av_free(m->elems[m->count].key);
  155. av_free(m->elems[m->count].value);
  156. }
  157. av_free(m->elems);
  158. }
  159. av_freep(pm);
  160. }
  161. void av_dict_copy(AVDictionary **dst, FF_CONST_AVUTIL53 AVDictionary *src, int flags)
  162. {
  163. AVDictionaryEntry *t = NULL;
  164. while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX)))
  165. av_dict_set(dst, t->key, t->value, flags);
  166. }