Rename it to AVDictionary, since it will be used as such. Tags documentation and metadata conversion API is lavf-specific, so remains there.pull/2/head
parent
ac4a854811
commit
d9f80ea2a7
9 changed files with 252 additions and 112 deletions
@ -0,0 +1,110 @@ |
|||||||
|
/*
|
||||||
|
* copyright (c) 2009 Michael Niedermayer |
||||||
|
* |
||||||
|
* This file is part of Libav. |
||||||
|
* |
||||||
|
* Libav is free software; you can redistribute it and/or |
||||||
|
* modify it under the terms of the GNU Lesser General Public |
||||||
|
* License as published by the Free Software Foundation; either |
||||||
|
* version 2.1 of the License, or (at your option) any later version. |
||||||
|
* |
||||||
|
* Libav is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||||||
|
* Lesser General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU Lesser General Public |
||||||
|
* License along with Libav; if not, write to the Free Software |
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <strings.h> |
||||||
|
#include "dict.h" |
||||||
|
#include "internal.h" |
||||||
|
#include "mem.h" |
||||||
|
|
||||||
|
AVDictionaryEntry * |
||||||
|
av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags) |
||||||
|
{ |
||||||
|
unsigned int i, j; |
||||||
|
|
||||||
|
if(!m) |
||||||
|
return NULL; |
||||||
|
|
||||||
|
if(prev) i= prev - m->elems + 1; |
||||||
|
else i= 0; |
||||||
|
|
||||||
|
for(; i<m->count; i++){ |
||||||
|
const char *s= m->elems[i].key; |
||||||
|
if(flags & AV_DICT_MATCH_CASE) for(j=0; s[j] == key[j] && key[j]; j++); |
||||||
|
else for(j=0; toupper(s[j]) == toupper(key[j]) && key[j]; j++); |
||||||
|
if(key[j]) |
||||||
|
continue; |
||||||
|
if(s[j] && !(flags & AV_DICT_IGNORE_SUFFIX)) |
||||||
|
continue; |
||||||
|
return &m->elems[i]; |
||||||
|
} |
||||||
|
return NULL; |
||||||
|
} |
||||||
|
|
||||||
|
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags) |
||||||
|
{ |
||||||
|
AVDictionary *m = *pm; |
||||||
|
AVDictionaryEntry *tag = av_dict_get(m, key, NULL, flags); |
||||||
|
|
||||||
|
if(!m) |
||||||
|
m = *pm = av_mallocz(sizeof(*m)); |
||||||
|
|
||||||
|
if(tag) { |
||||||
|
if (flags & AV_DICT_DONT_OVERWRITE) |
||||||
|
return 0; |
||||||
|
av_free(tag->value); |
||||||
|
av_free(tag->key); |
||||||
|
*tag = m->elems[--m->count]; |
||||||
|
} else { |
||||||
|
AVDictionaryEntry *tmp = av_realloc(m->elems, (m->count+1) * sizeof(*m->elems)); |
||||||
|
if(tmp) { |
||||||
|
m->elems = tmp; |
||||||
|
} else |
||||||
|
return AVERROR(ENOMEM); |
||||||
|
} |
||||||
|
if (value) { |
||||||
|
if (flags & AV_DICT_DONT_STRDUP_KEY) { |
||||||
|
m->elems[m->count].key = key; |
||||||
|
} else |
||||||
|
m->elems[m->count].key = av_strdup(key ); |
||||||
|
if (flags & AV_DICT_DONT_STRDUP_VAL) { |
||||||
|
m->elems[m->count].value = value; |
||||||
|
} else |
||||||
|
m->elems[m->count].value = av_strdup(value); |
||||||
|
m->count++; |
||||||
|
} |
||||||
|
if (!m->count) { |
||||||
|
av_free(m->elems); |
||||||
|
av_freep(pm); |
||||||
|
} |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
void av_dict_free(AVDictionary **pm) |
||||||
|
{ |
||||||
|
AVDictionary *m = *pm; |
||||||
|
|
||||||
|
if (m) { |
||||||
|
while(m->count--) { |
||||||
|
av_free(m->elems[m->count].key); |
||||||
|
av_free(m->elems[m->count].value); |
||||||
|
} |
||||||
|
av_free(m->elems); |
||||||
|
} |
||||||
|
av_freep(pm); |
||||||
|
} |
||||||
|
|
||||||
|
void av_dict_copy(AVDictionary **dst, AVDictionary *src, int flags) |
||||||
|
{ |
||||||
|
AVDictionaryEntry *t = NULL; |
||||||
|
|
||||||
|
while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX))) |
||||||
|
av_dict_set(dst, t->key, t->value, flags); |
||||||
|
} |
@ -0,0 +1,78 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* This file is part of Libav. |
||||||
|
* |
||||||
|
* Libav is free software; you can redistribute it and/or |
||||||
|
* modify it under the terms of the GNU Lesser General Public |
||||||
|
* License as published by the Free Software Foundation; either |
||||||
|
* version 2.1 of the License, or (at your option) any later version. |
||||||
|
* |
||||||
|
* Libav is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||||||
|
* Lesser General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU Lesser General Public |
||||||
|
* License along with Libav; if not, write to the Free Software |
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
||||||
|
*/ |
||||||
|
|
||||||
|
/**
|
||||||
|
* @file Public dictionary API. |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef AVUTIL_DICT_H |
||||||
|
#define AVUTIL_DICT_H |
||||||
|
|
||||||
|
#define AV_DICT_MATCH_CASE 1 |
||||||
|
#define AV_DICT_IGNORE_SUFFIX 2 |
||||||
|
#define AV_DICT_DONT_STRDUP_KEY 4 |
||||||
|
#define AV_DICT_DONT_STRDUP_VAL 8 |
||||||
|
#define AV_DICT_DONT_OVERWRITE 16 ///< Don't overwrite existing entries.
|
||||||
|
|
||||||
|
typedef struct { |
||||||
|
char *key; |
||||||
|
char *value; |
||||||
|
} AVDictionaryEntry; |
||||||
|
|
||||||
|
typedef struct AVDictionary AVDictionary; |
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a dictionary entry with matching key. |
||||||
|
* |
||||||
|
* @param prev Set to the previous matching element to find the next. |
||||||
|
* If set to NULL the first matching element is returned. |
||||||
|
* @param flags Allows case as well as suffix-insensitive comparisons. |
||||||
|
* @return Found entry or NULL, changing key or value leads to undefined behavior. |
||||||
|
*/ |
||||||
|
AVDictionaryEntry * |
||||||
|
av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags); |
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the given entry in *pm, overwriting an existing entry. |
||||||
|
* |
||||||
|
* @param pm pointer to a pointer to a dictionary struct. If *pm is NULL |
||||||
|
* a dictionary struct is allocated and put in *pm. |
||||||
|
* @param key entry key to add to *pm (will be av_strduped depending on flags) |
||||||
|
* @param value entry value to add to *pm (will be av_strduped depending on flags). |
||||||
|
* Passing a NULL value will cause an existing tag to be deleted. |
||||||
|
* @return >= 0 on success otherwise an error code <0 |
||||||
|
*/ |
||||||
|
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags); |
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy entries from one AVDictionary struct into another. |
||||||
|
* @param dst pointer to a pointer to a AVDictionary struct. If *dst is NULL, |
||||||
|
* this function will allocate a struct for you and put it in *dst |
||||||
|
* @param src pointer to source AVDictionary struct |
||||||
|
* @param flags flags to use when setting entries in *dst |
||||||
|
* @note metadata is read using the AV_DICT_IGNORE_SUFFIX flag |
||||||
|
*/ |
||||||
|
void av_dict_copy(AVDictionary **dst, AVDictionary *src, int flags); |
||||||
|
|
||||||
|
/**
|
||||||
|
* Free all the memory allocated for an AVDictionary struct. |
||||||
|
*/ |
||||||
|
void av_dict_free(AVDictionary **m); |
||||||
|
|
||||||
|
#endif // AVUTIL_DICT_H
|
Loading…
Reference in new issue