changeset 32428:92b92a78ca2a

Make the parse_timestring public, with a slightly extended API. As a consequence, "2 hours" is no longer recognized as a valid timestamp meaning "2 seconds".
author cigaes
date Mon, 18 Oct 2010 20:56:52 +0000
parents 58232aeb3fdd
children ac882481e5bb
files m_option.c m_option.h
diffstat 2 files changed, 27 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/m_option.c	Mon Oct 18 20:44:04 2010 +0000
+++ b/m_option.c	Mon Oct 18 20:56:52 2010 +0000
@@ -1247,17 +1247,22 @@
 };
 
 
-static double parse_timestring(const char *str)
+int parse_timestring(const char *str, double *time, char endchar)
 {
-  int a, b;
+  int a, b, len;
   double d;
-  if (sscanf(str, "%d:%d:%lf", &a, &b, &d) == 3)
-    return 3600*a + 60*b + d;
-  else if (sscanf(str, "%d:%lf", &a, &d) == 2)
-    return 60*a + d;
-  else if (sscanf(str, "%lf", &d) == 1)
-    return d;
-  return -1e100;
+  *time = 0; /* ensure initialization for error cases */
+  if (sscanf(str, "%d:%d:%lf%n", &a, &b, &d, &len) >= 3)
+    *time = 3600*a + 60*b + d;
+  else if (sscanf(str, "%d:%lf%n", &a, &d, &len) >= 2)
+    *time = 60*a + d;
+  else if (sscanf(str, "%lf%n", &d, &len) >= 1)
+    *time = d;
+  else
+    return 0; /* unsupported time format */
+  if (str[len] && str[len] != endchar)
+    return 0; /* invalid extra characters at the end */
+  return len;
 }
 
 
@@ -1268,8 +1273,7 @@
   if (param == NULL || strlen(param) == 0)
     return M_OPT_MISSING_PARAM;
 
-  time = parse_timestring(param);
-  if (time == -1e100) {
+  if (!parse_timestring(param, &time, 0)) {
     mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: invalid time: '%s'\n",
            name,param);
     return M_OPT_INVALID;
@@ -1327,7 +1331,7 @@
 
   /* End at time parsing. This has to be last because the parsing accepts
    * even a number followed by garbage */
-  if ((end_at = parse_timestring(param)) == -1e100) {
+  if (!parse_timestring(param, &end_at, 0)) {
     mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: invalid time or size: '%s'\n",
            name,param);
     return M_OPT_INVALID;
--- a/m_option.h	Mon Oct 18 20:44:04 2010 +0000
+++ b/m_option.h	Mon Oct 18 20:56:52 2010 +0000
@@ -528,4 +528,15 @@
 
 /*@}*/
 
+/**
+ * Parse a string as a timestamp.
+ *
+ * @param[in]  str      the string to parse.
+ * @param[out] time     parsed time.
+ * @param[in]  endchar  return an error of the next character after the
+ *                      timestamp is neither nul nor endchar.
+ * @return              Number of chars in the timestamp.
+ */
+int parse_timestring(const char *str, double *time, char endchar);
+
 #endif /* MPLAYER_M_OPTION_H */