dict.h (9535B)
1 /* 2 * This file is part of FFmpeg. 3 * 4 * FFmpeg is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2.1 of the License, or (at your option) any later version. 8 * 9 * FFmpeg is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with FFmpeg; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 17 */ 18 19 /** 20 * @file 21 * Public dictionary API. 22 * @deprecated 23 * AVDictionary is provided for compatibility with libav. It is both in 24 * implementation as well as API inefficient. It does not scale and is 25 * extremely slow with large dictionaries. 26 * It is recommended that new code uses our tree container from tree.c/h 27 * where applicable, which uses AVL trees to achieve O(log n) performance. 28 */ 29 30 #ifndef AVUTIL_DICT_H 31 #define AVUTIL_DICT_H 32 33 #include <stdint.h> 34 35 /** 36 * @addtogroup lavu_dict AVDictionary 37 * @ingroup lavu_data 38 * 39 * @brief Simple key:value store 40 * 41 * @{ 42 * Dictionaries are used for storing key-value pairs. 43 * 44 * - To **create an AVDictionary**, simply pass an address of a NULL 45 * pointer to av_dict_set(). NULL can be used as an empty dictionary 46 * wherever a pointer to an AVDictionary is required. 47 * - To **insert an entry**, use av_dict_set(). 48 * - Use av_dict_get() to **retrieve an entry**. 49 * - To **iterate over all entries**, use av_dict_iterate(). 50 * - In order to **free the dictionary and all its contents**, use 51 av_dict_free(). 52 * 53 @code 54 AVDictionary *d = NULL; // "create" an empty dictionary 55 AVDictionaryEntry *t = NULL; 56 57 av_dict_set(&d, "foo", "bar", 0); // add an entry 58 59 char *k = av_strdup("key"); // if your strings are already allocated, 60 char *v = av_strdup("value"); // you can avoid copying them like this 61 av_dict_set(&d, k, v, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL); 62 63 while ((t = av_dict_iterate(d, t))) { 64 <....> // iterate over all entries in d 65 } 66 av_dict_free(&d); 67 @endcode 68 */ 69 70 /** 71 * @name AVDictionary Flags 72 * Flags that influence behavior of the matching of keys or insertion to the 73 * dictionary. 74 * @{ 75 */ 76 #define AV_DICT_MATCH_CASE \ 77 1 /**< Only get an entry with exact-case key match. Only relevant in \ 78 av_dict_get(). */ 79 #define AV_DICT_IGNORE_SUFFIX \ 80 2 /**< Return first entry in a dictionary whose first part corresponds to \ 81 the search key, ignoring the suffix of the found key string. Only \ 82 relevant in av_dict_get(). */ 83 #define AV_DICT_DONT_STRDUP_KEY \ 84 4 /**< Take ownership of a key that's been \ 85 allocated with av_malloc() or another memory allocation function. */ 86 #define AV_DICT_DONT_STRDUP_VAL \ 87 8 /**< Take ownership of a value that's been \ 88 allocated with av_malloc() or another memory allocation function. */ 89 #define AV_DICT_DONT_OVERWRITE 16 /**< Don't overwrite existing entries. */ 90 #define AV_DICT_APPEND \ 91 32 /**< If the entry already exists, append to it. Note that no \ 92 delimiter is added, the strings are simply concatenated. */ 93 #define AV_DICT_MULTIKEY \ 94 64 /**< Allow to store several equal keys in the dictionary */ 95 /** 96 * @} 97 */ 98 99 typedef struct AVDictionaryEntry { 100 char* key; 101 char* value; 102 } AVDictionaryEntry; 103 104 typedef struct AVDictionary AVDictionary; 105 106 /** 107 * Get a dictionary entry with matching key. 108 * 109 * The returned entry key or value must not be changed, or it will 110 * cause undefined behavior. 111 * 112 * @param prev Set to the previous matching element to find the next. 113 * If set to NULL the first matching element is returned. 114 * @param key Matching key 115 * @param flags A collection of AV_DICT_* flags controlling how the 116 * entry is retrieved 117 * 118 * @return Found entry or NULL in case no matching entry was found in the 119 * dictionary 120 */ 121 AVDictionaryEntry* av_dict_get(const AVDictionary* m, const char* key, 122 const AVDictionaryEntry* prev, int flags); 123 124 /** 125 * Iterate over a dictionary 126 * 127 * Iterates through all entries in the dictionary. 128 * 129 * @warning The returned AVDictionaryEntry key/value must not be changed. 130 * 131 * @warning As av_dict_set() invalidates all previous entries returned 132 * by this function, it must not be called while iterating over the dict. 133 * 134 * Typical usage: 135 * @code 136 * const AVDictionaryEntry *e = NULL; 137 * while ((e = av_dict_iterate(m, e))) { 138 * // ... 139 * } 140 * @endcode 141 * 142 * @param m The dictionary to iterate over 143 * @param prev Pointer to the previous AVDictionaryEntry, NULL initially 144 * 145 * @retval AVDictionaryEntry* The next element in the dictionary 146 * @retval NULL No more elements in the dictionary 147 */ 148 const AVDictionaryEntry* av_dict_iterate(const AVDictionary* m, 149 const AVDictionaryEntry* prev); 150 151 /** 152 * Get number of entries in dictionary. 153 * 154 * @param m dictionary 155 * @return number of entries in dictionary 156 */ 157 int av_dict_count(const AVDictionary* m); 158 159 /** 160 * Set the given entry in *pm, overwriting an existing entry. 161 * 162 * Note: If AV_DICT_DONT_STRDUP_KEY or AV_DICT_DONT_STRDUP_VAL is set, 163 * these arguments will be freed on error. 164 * 165 * @warning Adding a new entry to a dictionary invalidates all existing entries 166 * previously returned with av_dict_get() or av_dict_iterate(). 167 * 168 * @param pm Pointer to a pointer to a dictionary struct. If *pm is NULL 169 * a dictionary struct is allocated and put in *pm. 170 * @param key Entry key to add to *pm (will either be av_strduped or added 171 * as a new key depending on flags) 172 * @param value Entry value to add to *pm (will be av_strduped or added as a 173 * new key depending on flags). Passing a NULL value will cause an existing 174 * entry to be deleted. 175 * 176 * @return >= 0 on success otherwise an error code <0 177 */ 178 int av_dict_set(AVDictionary** pm, const char* key, const char* value, 179 int flags); 180 181 /** 182 * Convenience wrapper for av_dict_set() that converts the value to a string 183 * and stores it. 184 * 185 * Note: If ::AV_DICT_DONT_STRDUP_KEY is set, key will be freed on error. 186 */ 187 int av_dict_set_int(AVDictionary** pm, const char* key, int64_t value, 188 int flags); 189 190 /** 191 * Parse the key/value pairs list and add the parsed entries to a dictionary. 192 * 193 * In case of failure, all the successfully set entries are stored in 194 * *pm. You may need to manually free the created dictionary. 195 * 196 * @param key_val_sep A 0-terminated list of characters used to separate 197 * key from value 198 * @param pairs_sep A 0-terminated list of characters used to separate 199 * two pairs from each other 200 * @param flags Flags to use when adding to the dictionary. 201 * ::AV_DICT_DONT_STRDUP_KEY and ::AV_DICT_DONT_STRDUP_VAL 202 * are ignored since the key/value tokens will always 203 * be duplicated. 204 * 205 * @return 0 on success, negative AVERROR code on failure 206 */ 207 int av_dict_parse_string(AVDictionary** pm, const char* str, 208 const char* key_val_sep, const char* pairs_sep, 209 int flags); 210 211 /** 212 * Copy entries from one AVDictionary struct into another. 213 * 214 * @note Metadata is read using the ::AV_DICT_IGNORE_SUFFIX flag 215 * 216 * @param dst Pointer to a pointer to a AVDictionary struct to copy into. If 217 * *dst is NULL, this function will allocate a struct for you and put it in *dst 218 * @param src Pointer to the source AVDictionary struct to copy items from. 219 * @param flags Flags to use when setting entries in *dst 220 * 221 * @return 0 on success, negative AVERROR code on failure. If dst was allocated 222 * by this function, callers should free the associated memory. 223 */ 224 int av_dict_copy(AVDictionary** dst, const AVDictionary* src, int flags); 225 226 /** 227 * Free all the memory allocated for an AVDictionary struct 228 * and all keys and values. 229 */ 230 void av_dict_free(AVDictionary** m); 231 232 /** 233 * Get dictionary entries as a string. 234 * 235 * Create a string containing dictionary's entries. 236 * Such string may be passed back to av_dict_parse_string(). 237 * @note String is escaped with backslashes ('\'). 238 * 239 * @warning Separators cannot be neither '\\' nor '\0'. They also cannot be the 240 * same. 241 * 242 * @param[in] m The dictionary 243 * @param[out] buffer Pointer to buffer that will be allocated with 244 * string containg entries. Buffer must be freed by the caller when is no longer 245 * needed. 246 * @param[in] key_val_sep Character used to separate key from value 247 * @param[in] pairs_sep Character used to separate two pairs from each 248 * other 249 * 250 * @return >= 0 on success, negative on error 251 */ 252 int av_dict_get_string(const AVDictionary* m, char** buffer, 253 const char key_val_sep, const char pairs_sep); 254 255 /** 256 * @} 257 */ 258 259 #endif /* AVUTIL_DICT_H */