rtsprange: fix formatting and parsing of range floating-point values
authorTim-Philipp Müller <tim@centricular.net>
Thu, 20 Sep 2012 00:07:08 +0000 (01:07 +0100)
committerTim-Philipp Müller <tim@centricular.net>
Fri, 12 Oct 2012 23:19:54 +0000 (00:19 +0100)
Other locales might use a comma instead of a floating point
for floats, which might lead to parsing errors.

https://bugzilla.gnome.org/show_bug.cgi?id=684411

gst-libs/gst/rtsp/gstrtsprange.c

index bd8aae4..a5f4b84 100644 (file)
 
 #include "gstrtsprange.h"
 
+static gdouble
+gst_strtod (const gchar * dstr)
+{
+  gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
+
+  /* canonicalise floating point string so we can handle float strings
+   * in the form "24.930" or "24,930" irrespective of the current locale.
+   * We should always be getting floats in 24.930 format with a floating point,
+   * but let's accept malformed ones as well, easy mistake to make after all */
+  g_strlcpy (s, dstr, sizeof (s));
+  g_strdelimit (s, ",", '.');
+  return g_ascii_strtod (s, NULL);
+}
+
 /* npt-time     =   "now" | npt-sec | npt-hhmmss
  * npt-sec      =   1*DIGIT [ "." *DIGIT ]
  * npt-hhmmss   =   npt-hh ":" npt-mm ":" npt-ss [ "." *DIGIT ]
@@ -70,20 +84,16 @@ parse_npt_time (const gchar * str, GstRTSPTime * time)
   } else if (str[0] == '\0') {
     time->type = GST_RTSP_TIME_END;
   } else if (strstr (str, ":")) {
-    gfloat seconds;
     gint hours, mins;
 
-    sscanf (str, "%2d:%2d:%f", &hours, &mins, &seconds);
-
+    sscanf (str, "%2d:%2d:", &hours, &mins);
+    str = strchr (str, ':') + 1;
+    str = strchr (str, ':') + 1;
     time->type = GST_RTSP_TIME_SECONDS;
-    time->seconds = ((hours * 60) + mins) * 60 + seconds;
+    time->seconds = ((hours * 60) + mins) * 60 + gst_strtod (str);
   } else {
-    gfloat seconds;
-
-    sscanf (str, "%f", &seconds);
-
     time->type = GST_RTSP_TIME_SECONDS;
-    time->seconds = seconds;
+    time->seconds = gst_strtod (str);
   }
   return GST_RTSP_OK;
 }
@@ -180,11 +190,14 @@ invalid:
 static gboolean
 npt_time_string (const GstRTSPTime * time, GString * string)
 {
+  gchar dstrbuf[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
   gboolean res = TRUE;;
 
   switch (time->type) {
     case GST_RTSP_TIME_SECONDS:
-      g_string_append_printf (string, "%f", time->seconds);
+      /* need to format floating point value strings as in C locale */
+      g_ascii_dtostr (dstrbuf, G_ASCII_DTOSTR_BUF_SIZE, time->seconds);
+      g_string_append (string, dstrbuf);
       break;
     case GST_RTSP_TIME_NOW:
       g_string_append (string, "now");