Browse code

dict: add AV_DICT_APPEND flag.

Anton Khirnov authored on 2011/06/05 01:43:05
Showing 2 changed files
... ...
@@ -19,6 +19,7 @@
19 19
  */
20 20
 
21 21
 #include <strings.h>
22
+#include "avstring.h"
22 23
 #include "dict.h"
23 24
 #include "internal.h"
24 25
 #include "mem.h"
... ...
@@ -51,6 +52,7 @@ int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags
51 51
 {
52 52
     AVDictionary      *m = *pm;
53 53
     AVDictionaryEntry *tag = av_dict_get(m, key, NULL, flags);
54
+    char *oldval = NULL;
54 55
 
55 56
     if(!m)
56 57
         m = *pm = av_mallocz(sizeof(*m));
... ...
@@ -58,7 +60,10 @@ int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags
58 58
     if(tag) {
59 59
         if (flags & AV_DICT_DONT_OVERWRITE)
60 60
             return 0;
61
-        av_free(tag->value);
61
+        if (flags & AV_DICT_APPEND)
62
+            oldval = tag->value;
63
+        else
64
+            av_free(tag->value);
62 65
         av_free(tag->key);
63 66
         *tag = m->elems[--m->count];
64 67
     } else {
... ...
@@ -75,6 +80,12 @@ int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags
75 75
         m->elems[m->count].key  = av_strdup(key  );
76 76
         if (flags & AV_DICT_DONT_STRDUP_VAL) {
77 77
             m->elems[m->count].value = value;
78
+        } else if (oldval && flags & AV_DICT_APPEND) {
79
+            int len = strlen(oldval) + strlen(value) + 1;
80
+            if (!(oldval = av_realloc(oldval, len)))
81
+                return AVERROR(ENOMEM);
82
+            av_strlcat(oldval, value, len);
83
+            m->elems[m->count].value = oldval;
78 84
         } else
79 85
             m->elems[m->count].value = av_strdup(value);
80 86
         m->count++;
... ...
@@ -29,6 +29,8 @@
29 29
 #define AV_DICT_DONT_STRDUP_KEY 4
30 30
 #define AV_DICT_DONT_STRDUP_VAL 8
31 31
 #define AV_DICT_DONT_OVERWRITE 16   ///< Don't overwrite existing entries.
32
+#define AV_DICT_APPEND         32   /**< If the entry already exists, append to it.  Note that no
33
+                                      delimiter is added, the strings are simply concatenated. */
32 34
 
33 35
 typedef struct {
34 36
     char *key;