dict.h 9.2 KB

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