From dfd335f853b8711262647ecf67e16d8b790d9969 Mon Sep 17 00:00:00 2001 From: "Friedrich W. H. Kossebau" Date: Sun, 27 Jan 2019 00:24:01 +0100 Subject: [PATCH] Fix Milkdrop Preset parser to read floats always in C locale Also remove calls of setlocale(LC_NUMERIC, "C"); in library, which results in unexpected sideeffects in the rest of the process where the library is used. Same for the applications which also had this work-around added. Work-arounds using setlocale had been added in 90aa2c8f381c7bf35496536acd015b84533cd596 & 196c374a74a158eacd806469311f2b701cbc3849 --- .../MilkdropPresetFactory/Parser.cpp | 37 +++++-------------- src/libprojectM/projectM.cpp | 4 -- src/projectM-jack/qprojectM-jack.cpp | 1 - .../qprojectM-pulseaudio.cpp | 1 - 4 files changed, 10 insertions(+), 33 deletions(-) diff --git a/src/libprojectM/MilkdropPresetFactory/Parser.cpp b/src/libprojectM/MilkdropPresetFactory/Parser.cpp index 234c0b168..cf72b9996 100755 --- a/src/libprojectM/MilkdropPresetFactory/Parser.cpp +++ b/src/libprojectM/MilkdropPresetFactory/Parser.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include "Common.hpp" @@ -1287,26 +1288,17 @@ int Parser::parse_int(std::istream & fs, int * int_ptr) int Parser::string_to_float(char * string, float * float_ptr) { - char ** error_ptr; - if (*string == 0) return PROJECTM_PARSE_ERROR; - error_ptr = (char**)wipemalloc(sizeof(char**)); - - (*float_ptr) = strtod(string, error_ptr); - - /* These imply a succesful parse of the string */ - if ((**error_ptr == '\0') || (**error_ptr == '\r')) - { - free(error_ptr); - error_ptr = NULL; + std::istringstream iss(string); + iss.imbue(std::locale("C")); + iss >> (*float_ptr); + if (!iss.fail()) { return PROJECTM_SUCCESS; } (*float_ptr) = 0; - free(error_ptr); - error_ptr = NULL; return PROJECTM_PARSE_ERROR; } @@ -1315,12 +1307,9 @@ int Parser::parse_float(std::istream & fs, float * float_ptr) { char string[MAX_TOKEN_SIZE]; - char ** error_ptr; token_t token; int sign; - error_ptr =(char**) wipemalloc(sizeof(char**)); - token = parseToken(fs, string); switch (token) @@ -1339,26 +1328,20 @@ int Parser::parse_float(std::istream & fs, float * float_ptr) if (string[0] == 0) { - free(error_ptr); - error_ptr = NULL; return PROJECTM_PARSE_ERROR; } - (*float_ptr) = sign*strtod(string, error_ptr); - - /* No conversion was performed */ - if ((**error_ptr == '\0') || (**error_ptr == '\r')) - { - free(error_ptr); - error_ptr = NULL; + std::istringstream iss(string); + iss.imbue(std::locale("C")); + iss >> (*float_ptr); + if (!iss.fail()) { + (*float_ptr) *= sign; return PROJECTM_SUCCESS; } if (PARSE_DEBUG) printf("parse_float: float conversion failed for string \"%s\"\n", string); (*float_ptr) = 0; - free(error_ptr); - error_ptr = NULL; return PROJECTM_PARSE_ERROR; } diff --git a/src/libprojectM/projectM.cpp b/src/libprojectM/projectM.cpp index b9da965d6..b2ef6e86d 100755 --- a/src/libprojectM/projectM.cpp +++ b/src/libprojectM/projectM.cpp @@ -469,15 +469,11 @@ void projectM::projectM_reset() this->fpsstart = 0; - setlocale(LC_NUMERIC, "C"); - projectM_resetengine(); } void projectM::projectM_init ( int gx, int gy, int fps, int texsize, int width, int height ) { - setlocale(LC_NUMERIC, "C"); - /** Initialise start time */ timeKeeper = new TimeKeeper(_settings.presetDuration,_settings.smoothPresetDuration, _settings.easterEgg); diff --git a/src/projectM-jack/qprojectM-jack.cpp b/src/projectM-jack/qprojectM-jack.cpp index e65bea348..53bdac760 100644 --- a/src/projectM-jack/qprojectM-jack.cpp +++ b/src/projectM-jack/qprojectM-jack.cpp @@ -205,7 +205,6 @@ int main (int argc, char **argv) { // Start a new application ProjectMApplication app(argc, argv); - setlocale(LC_NUMERIC, "C"); // Fix std::string config_file; config_file = read_config(); diff --git a/src/projectM-pulseaudio/qprojectM-pulseaudio.cpp b/src/projectM-pulseaudio/qprojectM-pulseaudio.cpp index 0824b2889..0dc687215 100644 --- a/src/projectM-pulseaudio/qprojectM-pulseaudio.cpp +++ b/src/projectM-pulseaudio/qprojectM-pulseaudio.cpp @@ -116,7 +116,6 @@ int main ( int argc, char*argv[] ) ProjectMApplication app ( argc, argv ); - setlocale(LC_NUMERIC, "C"); // Fix std::string config_file; config_file = read_config();