[MPlayer-cvslog] r32514 - in trunk: m_option.c m_option.h

cigaes subversion at mplayerhq.hu
Mon Oct 18 22:56:53 CEST 2010


Author: cigaes
Date: Mon Oct 18 22:56:52 2010
New Revision: 32514

Log:
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".

Modified:
   trunk/m_option.c
   trunk/m_option.h

Modified: trunk/m_option.c
==============================================================================
--- trunk/m_option.c	Mon Oct 18 22:44:04 2010	(r32513)
+++ trunk/m_option.c	Mon Oct 18 22:56:52 2010	(r32514)
@@ -1247,17 +1247,22 @@ const m_option_type_t m_option_type_afmt
 };
 
 
-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 @@ static int parse_time(const m_option_t* 
   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 @@ static int parse_time_size(const m_optio
 
   /* 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;

Modified: trunk/m_option.h
==============================================================================
--- trunk/m_option.h	Mon Oct 18 22:44:04 2010	(r32513)
+++ trunk/m_option.h	Mon Oct 18 22:56:52 2010	(r32514)
@@ -528,4 +528,15 @@ m_option_free(const m_option_t* opt,void
 
 /*@}*/
 
+/**
+ * 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 */


More information about the MPlayer-cvslog mailing list