rtsprange: fix formatting and parsing of range floating-point values

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
This commit is contained in:
Tim-Philipp Müller 2012-09-20 01:07:08 +01:00
parent a1d2f75058
commit 336842d35c

View file

@ -55,6 +55,20 @@
#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");