dict.c 4.8 KB

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