[PATCH 02/12] Use av_parse_and_eval_expr() in av_parse_video_rate(), simplify.

Stefano Sabatini stefano.sabatini-lala
Thu Sep 30 23:49:16 CEST 2010


Also add a test for it.
---
 libavcore/parseutils.c |   67 ++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 51 insertions(+), 16 deletions(-)

diff --git a/libavcore/parseutils.c b/libavcore/parseutils.c
index b59b819..cf2c132 100644
--- a/libavcore/parseutils.c
+++ b/libavcore/parseutils.c
@@ -23,6 +23,7 @@
 
 #include "parseutils.h"
 #include "libavutil/avutil.h"
+#include "libavutil/eval.h"
 
 typedef struct {
     const char *abbr;
@@ -115,9 +116,9 @@ int av_parse_video_size(int *width_ptr, int *height_ptr, const char *str)
 
 int av_parse_video_rate(AVRational *rate, const char *arg)
 {
-    int i;
+    int i, ret;
     int n = FF_ARRAY_ELEMS(video_rate_abbrs);
-    char *cp;
+    double res;
 
     /* First, we check our abbreviation table */
     for (i = 0; i < n; ++i)
@@ -127,21 +128,55 @@ int av_parse_video_rate(AVRational *rate, const char *arg)
         }
 
     /* Then, we try to parse it as fraction */
-    cp = strchr(arg, '/');
-    if (!cp)
-        cp = strchr(arg, ':');
-    if (cp) {
-        char *cpp;
-        rate->num = strtol(arg, &cpp, 10);
-        if (cpp != arg || cpp == cp)
-            rate->den = strtol(cp+1, &cpp, 10);
-        else
-            rate->num = 0;
-    } else {
-        /* Finally we give up and parse it as double */
-        *rate = av_d2q(strtod(arg, 0), 1001000);
-    }
+    if ((ret = av_parse_and_eval_expr(&res, arg, NULL, NULL, NULL, NULL, NULL, NULL,
+                                      NULL, 0, NULL)) < 0)
+        return ret;
+    *rate = av_d2q(res, INT_MAX);
     if (rate->num <= 0 || rate->den <= 0)
         return AVERROR(EINVAL);
     return 0;
 }
+
+#ifdef TEST
+
+#undef printf
+
+int main(void)
+{
+    printf("Testing av_parse_video_rate()\n");
+    {
+        int i;
+        const char *rates[] = {
+            "123/0",
+            "",
+            "/",
+            " 123  /  321",
+            "foo/foo",
+            "foo/1",
+            "1/foo",
+            "0/0",
+            "/0",
+            "1/",
+            "-123/123",
+            "-foo",
+            "123.23",
+            ".23",
+            "-.23",
+            "-0.234",
+            "-0.0000001",
+            " -21332.2324   ",
+        };
+
+        for (i = 0; i < FF_ARRAY_ELEMS(rates); i++) {
+            int ret;
+            AVRational q = (AVRational){0, 0};
+            ret = av_parse_video_rate(&q, rates[i]),
+            printf("'%s' -> %d/%d %s\n",
+                   rates[i], q.num, q.den, ret ? "***error!" : "");
+        }
+    }
+
+    return 0;
+}
+
+#endif /* TEST */
-- 
1.7.1


--KsGdsel6WgEHnImy--



More information about the ffmpeg-devel mailing list