]> git.sesse.net Git - vlc/blobdiff - src/misc/charset.c
* i18n_strtod: locale-agnostic strtod() (accepts both comma and dot as decimal separ...
[vlc] / src / misc / charset.c
index a575f72136e69e70ca1c56cf2c105e1ae727f47a..2a66d6732670a3845831aaf7a4500053cb6c03e3 100644 (file)
@@ -380,25 +380,72 @@ char *__vlc_fix_readdir_charset( vlc_object_t *p_this, const char *psz_string )
  * dot (which is the american default), and comma (which is used in France,
  * the country with the most VLC developers, among others).
  *
- * i18n_atof() has the same prototype as ANSI C atof() but it accepts
+ * i18n_strtod() has the same prototype as ANSI C strtod() but it accepts
  * either decimal separator when deserializing the string to a float number,
  * independant of the local computer setting.
  */
-double i18n_atof( const char *str )
+double i18n_strtod( const char *str, char **end )
 {
-    char *end;
-    double d = strtod( str, &end );
+    char *end_buf, e;
+    double d;
 
-    if(( *end == ',' ) || ( *end == '.' ))
+    if( end == NULL )
+        end = &end_buf;
+    d = strtod( str, end );
+
+    e = **end;
+    if(( e == ',' ) || ( e == '.' ))
     {
-        char *dup = strdup( str );
+        char dup[strlen( str ) + 1];
+        strcpy( dup, str );
 
         if( dup == NULL )
             return d;
 
-        dup[end - str] = ( *end == ',' ) ? '.' : ',';
-        d = strtod( dup, &end );
-        free( dup );
+        dup[*end - str] = ( e == ',' ) ? '.' : ',';
+        d = strtod( dup, end );
     }
     return d;
 }
+
+/**
+ * i18n_atof() has the same prototype as ANSI C atof() but it accepts
+ * either decimal separator when deserializing the string to a float number,
+ * independant of the local computer setting.
+ */
+double i18n_atof( const char *str )
+{
+    return i18n_strtod( str, NULL );
+}
+
+
+/**
+ * us_strtod() has the same prototype as ANSI C strtod() but it expects
+ * a dot as decimal separator regardless of the system locale.
+ */
+double us_strtod( const char *str, char **end )
+{
+    char dup[strlen( str ) + 1], *ptr;
+    double d;
+    strcpy( dup, str );
+
+    ptr = strchr( dup, ',' );
+    if( ptr != NULL )
+        *ptr = '\0';
+
+    d = strtod( dup, &ptr );
+    if( end != NULL )
+        *end = (char *)&str[ptr - dup];
+
+    return d;
+}
+
+/**
+ * us_atof() has the same prototype as ANSI C atof() but it expects a dot
+ * as decimal separator, regardless of the system locale.
+ */
+double us_atof( const char *str )
+{
+    return us_strtod( str, NULL );
+}
+