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