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