]> git.sesse.net Git - ffmpeg/blobdiff - libavutil/parseutils.c
txd: return meaningful error codes.
[ffmpeg] / libavutil / parseutils.c
index 0272c3ef677c3155d80dc97251bc699fda121bf5..917451eee7e13b90375c5144b209b0d2ea7f59a1 100644 (file)
  * misc parsing utilities
  */
 
-#include <strings.h>
-#include <sys/time.h>
 #include <time.h>
 
 #include "avstring.h"
 #include "avutil.h"
+#include "common.h"
 #include "eval.h"
+#include "log.h"
 #include "random_seed.h"
 #include "parseutils.h"
 
@@ -107,8 +107,7 @@ int av_parse_video_size(int *width_ptr, int *height_ptr, const char *str)
         }
     }
     if (i == n) {
-        p = str;
-        width = strtol(p, &p, 10);
+        width = strtol(str, &p, 10);
         if (*p)
             p++;
         height = strtol(p, &p, 10);
@@ -293,7 +292,7 @@ static ColorEntry color_table[] = {
 
 static int color_table_compare(const void *lhs, const void *rhs)
 {
-    return strcasecmp(lhs, ((const ColorEntry *)rhs)->name);
+    return av_strcasecmp(lhs, ((const ColorEntry *)rhs)->name);
 }
 
 #define ALPHA_SEP '@'
@@ -319,7 +318,7 @@ int av_parse_color(uint8_t *rgba_color, const char *color_string, int slen,
     len = strlen(color_string2);
     rgba_color[3] = 255;
 
-    if (!strcasecmp(color_string2, "random") || !strcasecmp(color_string2, "bikeshed")) {
+    if (!av_strcasecmp(color_string2, "random") || !av_strcasecmp(color_string2, "bikeshed")) {
         int rgba = av_get_random_seed();
         rgba_color[0] = rgba >> 24;
         rgba_color[1] = rgba >> 16;
@@ -355,7 +354,7 @@ int av_parse_color(uint8_t *rgba_color, const char *color_string, int slen,
     }
 
     if (tail) {
-        unsigned long int alpha;
+        double alpha;
         const char *alpha_string = tail;
         if (!strncmp(alpha_string, "0x", 2)) {
             alpha = strtoul(alpha_string, &tail, 16);
@@ -363,7 +362,7 @@ int av_parse_color(uint8_t *rgba_color, const char *color_string, int slen,
             alpha = 255 * strtod(alpha_string, &tail);
         }
 
-        if (tail == alpha_string || *tail || alpha > 255) {
+        if (tail == alpha_string || *tail || alpha > 255 || alpha < 0) {
             av_log(log_ctx, AV_LOG_ERROR, "Invalid alpha value specifier '%s' in '%s'\n",
                    alpha_string, color_string);
             return AVERROR(EINVAL);
@@ -400,10 +399,7 @@ static int date_get_num(const char **pp,
     return val;
 }
 
-/* small strptime for ffmpeg */
-static
-const char *small_strptime(const char *p, const char *fmt,
-                           struct tm *dt)
+static const char *small_strptime(const char *p, const char *fmt, struct tm *dt)
 {
     int c, val;
 
@@ -462,10 +458,9 @@ const char *small_strptime(const char *p, const char *fmt,
             p++;
         }
     }
-    return p;
 }
 
-static time_t mktimegm(struct tm *tm)
+time_t av_timegm(struct tm *tm)
 {
     time_t t;
 
@@ -484,11 +479,11 @@ static time_t mktimegm(struct tm *tm)
     return t;
 }
 
-int av_parse_time(int64_t *timeval, const char *datestr, int duration)
+int av_parse_time(int64_t *timeval, const char *timestr, int duration)
 {
     const char *p;
     int64_t t;
-    struct tm dt;
+    struct tm dt = { 0 };
     int i;
     static const char * const date_fmt[] = {
         "%Y-%m-%d",
@@ -503,22 +498,19 @@ int av_parse_time(int64_t *timeval, const char *datestr, int duration)
     char lastch;
     int negative = 0;
 
-#undef time
     time_t now = time(0);
 
-    len = strlen(datestr);
+    len = strlen(timestr);
     if (len > 0)
-        lastch = datestr[len - 1];
+        lastch = timestr[len - 1];
     else
         lastch = '\0';
     is_utc = (lastch == 'z' || lastch == 'Z');
 
-    memset(&dt, 0, sizeof(dt));
-
-    p = datestr;
+    p = timestr;
     q = NULL;
     if (!duration) {
-        if (!strncasecmp(datestr, "now", len)) {
+        if (!av_strncasecmp(timestr, "now", len)) {
             *timeval = (int64_t) now * 1000000;
             return 0;
         }
@@ -555,15 +547,15 @@ int av_parse_time(int64_t *timeval, const char *datestr, int duration)
             }
         }
     } else {
-        /* parse datestr as a duration */
+        /* parse timestr as a duration */
         if (p[0] == '-') {
             negative = 1;
             ++p;
         }
-        /* parse datestr as HH:MM:SS */
+        /* parse timestr as HH:MM:SS */
         q = small_strptime(p, time_fmt[0], &dt);
         if (!q) {
-            /* parse datestr as S+ */
+            /* parse timestr as S+ */
             dt.tm_sec = strtol(p, (char **)&q, 10);
             if (q == p) {
                 /* the parsing didn't succeed */
@@ -586,7 +578,7 @@ int av_parse_time(int64_t *timeval, const char *datestr, int duration)
     } else {
         dt.tm_isdst = -1;       /* unknown */
         if (is_utc) {
-            t = mktimegm(&dt);
+            t = av_timegm(&dt);
         } else {
             t = mktime(&dt);
         }
@@ -650,14 +642,12 @@ int av_find_info_tag(char *arg, int arg_size, const char *tag1, const char *info
 
 #ifdef TEST
 
-#undef printf
-
 int main(void)
 {
     printf("Testing av_parse_video_rate()\n");
     {
         int i;
-        const char *rates[] = {
+        static const char *const rates[] = {
             "-inf",
             "inf",
             "nan",
@@ -687,10 +677,10 @@ int main(void)
 
         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 ret:%d\n",
-                   rates[i], q.num, q.den, ret);
+            AVRational q = { 0, 0 };
+            ret = av_parse_video_rate(&q, rates[i]);
+            printf("'%s' -> %d/%d %s\n",
+                   rates[i], q.num, q.den, ret ? "ERROR" : "OK");
         }
     }
 
@@ -698,9 +688,7 @@ int main(void)
     {
         int i;
         uint8_t rgba[4];
-        const char *color_names[] = {
-            "bikeshed",
-            "RaNdOm",
+        static const char *const color_names[] = {
             "foo",
             "red",
             "Red ",
@@ -741,7 +729,8 @@ int main(void)
 
         for (i = 0;  i < FF_ARRAY_ELEMS(color_names); i++) {
             if (av_parse_color(rgba, color_names[i], -1, NULL) >= 0)
-                printf("%s -> R(%d) G(%d) B(%d) A(%d)\n", color_names[i], rgba[0], rgba[1], rgba[2], rgba[3]);
+                printf("%s -> R(%d) G(%d) B(%d) A(%d)\n",
+                       color_names[i], rgba[0], rgba[1], rgba[2], rgba[3]);
         }
     }