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

Andrey Utkin andrey.krieger.utkin at gmail.com
Mon Feb 20 11:05:25 CET 2012


---8<---

Sorry, previous version was not even tested.
This version has fixed the splitting itself, and arguments order is swapped
according to Luca comment.
No time to implement escaping yet. Sending just in hope somebody can use it
as is, or improve it.
BTW, Luca, it does work with pairs like a=b,b=c.

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

diff --git a/libavutil/dict.c b/libavutil/dict.c
index cb5f7b1..c8e721c 100644
--- a/libavutil/dict.c
+++ b/libavutil/dict.c
@@ -118,3 +118,40 @@ 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(AVDictionary **dict, const char *arg)
+{
+    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_begin) {
+        token_end = strchr(token_begin + 3, ',');
+        if (token_end)
+            *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;
+    }
+    av_free(opts);
+    return 0;
+}
diff --git a/libavutil/dict.h b/libavutil/dict.h
index 6e28b61..5788b89 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(AVDictionary **dict, const char *arg);
+
+/**
  * @}
  */
 
-- 
1.7.7



More information about the libav-devel mailing list