From 38a87779a5a307d884b413e6539d64f41353fba3 Mon Sep 17 00:00:00 2001 From: vsr Date: Wed, 12 Feb 2020 14:47:13 +0300 Subject: [PATCH] Fix problems with localization: prevent accidental breaking of application locale --- src/MEDLoader/SauvMedConvertor.cxx | 12 +----------- src/MEDLoader/SauvReader.cxx | 20 ++++++++++++++++++++ src/MEDLoader/SauvUtilities.hxx | 2 +- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/MEDLoader/SauvMedConvertor.cxx b/src/MEDLoader/SauvMedConvertor.cxx index ad9e35118..47be72956 100644 --- a/src/MEDLoader/SauvMedConvertor.cxx +++ b/src/MEDLoader/SauvMedConvertor.cxx @@ -1819,7 +1819,6 @@ void ASCIIReader::init( int nbToRead, int nbPosInLine, int width, int shift /*= { _curPos = 0; } - _curLocale.clear(); } //================================================================================ @@ -1852,11 +1851,7 @@ void ASCIIReader::initIntReading(int nbValues) void ASCIIReader::initDoubleReading(int nbValues) { - init( nbValues, 3, 22 ); - - // Correction 2 of getDouble(): set "C" numeric locale to read numbers - // with dot decimal point separator, as it is in SAUVE files - _curLocale = setlocale(LC_NUMERIC, "C"); + init( nbValues, 3, 22 ); } //================================================================================ @@ -1903,11 +1898,6 @@ void ASCIIReader::next() else { _curPos = 0; - if ( !_curLocale.empty() ) - { - setlocale(LC_NUMERIC, _curLocale.c_str()); - _curLocale.clear(); - } } } diff --git a/src/MEDLoader/SauvReader.cxx b/src/MEDLoader/SauvReader.cxx index 931d35255..3640d4a4d 100644 --- a/src/MEDLoader/SauvReader.cxx +++ b/src/MEDLoader/SauvReader.cxx @@ -38,6 +38,24 @@ using namespace std; #define GIBI_EQUAL(var_str, stat_str) (strncmp (var_str, stat_str, strlen(stat_str)) == 0) +namespace +{ + class Localizer + { + std::string _locale; + public: + Localizer() + { + _locale = setlocale(LC_NUMERIC, NULL); + setlocale(LC_NUMERIC, "C"); + } + ~Localizer() + { + setlocale(LC_NUMERIC, _locale.c_str()); + } + }; +} + //================================================================================ /*! * \brief Creates a reader of a given sauve file @@ -114,6 +132,8 @@ std::string SauvReader::lineNb() const MEDCoupling::MEDFileData * SauvReader::loadInMEDFileDS() { + Localizer loc; // localization, to read numbers in "C" locale + SauvUtilities::IntermediateMED iMed; // intermadiate DS _iMed = &iMed; diff --git a/src/MEDLoader/SauvUtilities.hxx b/src/MEDLoader/SauvUtilities.hxx index f10e2f8a1..2150df37b 100644 --- a/src/MEDLoader/SauvUtilities.hxx +++ b/src/MEDLoader/SauvUtilities.hxx @@ -113,7 +113,7 @@ namespace SauvUtilities std::size_t getHeapMemorySizeWithoutChildren() const { return 0; } std::vector getDirectChildrenWithNull() const { return std::vector(); } protected: - std::string _fileName, _curLocale; + std::string _fileName; int _iRead, _nbToRead; }; } -- 2.39.2