00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <strings.h>
00022 #include "avstring.h"
00023 #include "dict.h"
00024 #include "internal.h"
00025 #include "mem.h"
00026
00027 AVDictionaryEntry *
00028 av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
00029 {
00030 unsigned int i, j;
00031
00032 if(!m)
00033 return NULL;
00034
00035 if(prev) i= prev - m->elems + 1;
00036 else i= 0;
00037
00038 for(; i<m->count; i++){
00039 const char *s= m->elems[i].key;
00040 if(flags & AV_DICT_MATCH_CASE) for(j=0; s[j] == key[j] && key[j]; j++);
00041 else for(j=0; toupper(s[j]) == toupper(key[j]) && key[j]; j++);
00042 if(key[j])
00043 continue;
00044 if(s[j] && !(flags & AV_DICT_IGNORE_SUFFIX))
00045 continue;
00046 return &m->elems[i];
00047 }
00048 return NULL;
00049 }
00050
00051 int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
00052 {
00053 AVDictionary *m = *pm;
00054 AVDictionaryEntry *tag = av_dict_get(m, key, NULL, flags);
00055 char *oldval = NULL;
00056
00057 if(!m)
00058 m = *pm = av_mallocz(sizeof(*m));
00059
00060 if(tag) {
00061 if (flags & AV_DICT_DONT_OVERWRITE)
00062 return 0;
00063 if (flags & AV_DICT_APPEND)
00064 oldval = tag->value;
00065 else
00066 av_free(tag->value);
00067 av_free(tag->key);
00068 *tag = m->elems[--m->count];
00069 } else {
00070 AVDictionaryEntry *tmp = av_realloc(m->elems, (m->count+1) * sizeof(*m->elems));
00071 if(tmp) {
00072 m->elems = tmp;
00073 } else
00074 return AVERROR(ENOMEM);
00075 }
00076 if (value) {
00077 if (flags & AV_DICT_DONT_STRDUP_KEY) {
00078 m->elems[m->count].key = key;
00079 } else
00080 m->elems[m->count].key = av_strdup(key );
00081 if (flags & AV_DICT_DONT_STRDUP_VAL) {
00082 m->elems[m->count].value = value;
00083 } else if (oldval && flags & AV_DICT_APPEND) {
00084 int len = strlen(oldval) + strlen(value) + 1;
00085 if (!(oldval = av_realloc(oldval, len)))
00086 return AVERROR(ENOMEM);
00087 av_strlcat(oldval, value, len);
00088 m->elems[m->count].value = oldval;
00089 } else
00090 m->elems[m->count].value = av_strdup(value);
00091 m->count++;
00092 }
00093 if (!m->count) {
00094 av_free(m->elems);
00095 av_freep(pm);
00096 }
00097
00098 return 0;
00099 }
00100
00101 void av_dict_free(AVDictionary **pm)
00102 {
00103 AVDictionary *m = *pm;
00104
00105 if (m) {
00106 while(m->count--) {
00107 av_free(m->elems[m->count].key);
00108 av_free(m->elems[m->count].value);
00109 }
00110 av_free(m->elems);
00111 }
00112 av_freep(pm);
00113 }
00114
00115 void av_dict_copy(AVDictionary **dst, AVDictionary *src, int flags)
00116 {
00117 AVDictionaryEntry *t = NULL;
00118
00119 while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX)))
00120 av_dict_set(dst, t->key, t->value, flags);
00121 }