• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavutil/dict.c

Go to the documentation of this file.
00001 /*
00002  * copyright (c) 2009 Michael Niedermayer
00003  *
00004  * This file is part of Libav.
00005  *
00006  * Libav is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * Libav is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with Libav; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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  = 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 = 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 }
Generated on Thu Jan 24 2013 17:08:56 for Libav by doxygen 1.7.1