[FFmpeg-devel] [PATCH] lavu: Add av_dict_from_string()

Andrey Utkin andrey.krieger.utkin at gmail.com
Thu Feb 16 16:45:00 CET 2012


---
 libavutil/dict.c |   35 +++++++++++++++++++++++++++++++++++
 libavutil/dict.h |    7 +++++++
 2 files changed, 42 insertions(+), 0 deletions(-)

diff --git a/libavutil/dict.c b/libavutil/dict.c
index cb5f7b1..35c3229 100644
--- a/libavutil/dict.c
+++ b/libavutil/dict.c
@@ -118,3 +118,38 @@ void av_dict_copy(AVDictionary **dst, AVDictionary *src, int flags)
     while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX)))
         av_dict_set(dst, t->key, t->value, flags);
 }
+
+int av_dict_from_string(const char *arg, AVDictionary **dict)
+{
+    int ret = 0;
+    char *opts;
+    char *token_begin;
+    char *token_end;
+    char *value_ptr;
+    if (!arg || !(*arg)) // empty string
+        return 0;
+    if (strlen(arg) < 3) // no room for at least 'a=b'
+        return AVERROR(EINVAL);
+    opts = av_strdup(arg);
+    if (!opts)
+        return AVERROR(ENOMEM);
+    token_begin = opts;
+    while ((token_end = strchr(token_begin + 3, ','))) {
+        *token_end = '\0';
+        value_ptr = strchr(token_begin + 1, '=');
+        if (!value_ptr) {
+            av_free(opts);
+            return AVERROR(EINVAL);
+        }
+        *value_ptr = '\0';
+        value_ptr++;
+        ret = av_dict_set(dict, token_begin, value_ptr, 0);
+        if (ret) {
+            av_free(opts);
+            return ret;
+        }
+        token_begin = token_end + 1;
+    }
+    av_free(opts);
+    return 0;
+}
diff --git a/libavutil/dict.h b/libavutil/dict.h
index 6e28b61..a563971 100644
--- a/libavutil/dict.h
+++ b/libavutil/dict.h
@@ -115,6 +115,13 @@ void av_dict_copy(AVDictionary **dst, AVDictionary *src, int flags);
 void av_dict_free(AVDictionary **m);
 
 /**
+ * Get AVDictionary from string in format "opt1=val1,opt_i=val_i,..."
+ *
+ * @return 0 on success, otherwise an error code <0
+ */
+int av_dict_from_string(const char *arg, AVDictionary **dict);
+
+/**
  * @}
  */
 
-- 
1.7.7



More information about the ffmpeg-devel mailing list