Commit 5a3fce5e authored by Joachim Pouderoux's avatar Joachim Pouderoux

Fix locale issue with Legacy VTK readers & writers

Previous implementation is sensible to the locale settings.
This can lead to the creation of VTK files with decimal separators
that, for instance, are not compatible with applications like ParaView
that sets the "C" locale to avoid such conflicts.
This fix set the "C" locale before reading/writing files and restore
the previous value when done.
parent 6aa7b028
......@@ -58,8 +58,8 @@
#include <vtksys/SystemTools.hxx>
#include <sstream>
#include <cctype>
#include <sstream>
// I need a safe way to read a line of arbitrary length. It exists on
// some platforms but not others so I'm afraid I have to write it
......@@ -447,6 +447,10 @@ size_t vtkDataReader::Peek(char *str, size_t n)
// Open a vtk data file. Returns zero if error.
int vtkDataReader::OpenVTKFile(const char* fname)
{
// Save current locale settings and set standard one to
// avoid locale issues - for instance with the decimal separator.
this->CurrentLocale = std::locale::global(std::locale::classic());
if(!fname && this->GetNumberOfFileNames() >0)
{
fname = this->GetFileName(0);
......@@ -3417,6 +3421,10 @@ char *vtkDataReader::LowerCase(char *str, const size_t len)
void vtkDataReader::CloseVTKFile()
{
vtkDebugMacro(<<"Closing vtk file");
// Restore the previous locale settings
std::locale::global(this->CurrentLocale);
delete this->IS;
this->IS = nullptr;
}
......
......@@ -33,6 +33,8 @@
#include "vtkSimpleReader.h"
#include "vtkStdString.h" // For API using strings
#include <locale> // For locale settings
#define VTK_ASCII 1
#define VTK_BINARY 2
......@@ -554,6 +556,8 @@ protected:
int FileMajorVersion;
int FileMinorVersion;
std::locale CurrentLocale;
void InitializeCharacteristics();
int CharacterizeFile(); //read entire file, storing important characteristics
void CheckFor(const char* name, char *line, int &num, char** &array,
......
......@@ -127,6 +127,10 @@ vtkDataWriter::~vtkDataWriter()
// Open a vtk data file. Returns nullptr if error.
ostream *vtkDataWriter::OpenVTKFile()
{
// Save current locale settings and set standard one to
// avoid locale issues - for instance with the decimal separator.
this->CurrentLocale = std::locale::global(std::locale::classic());
ostream *fptr;
if ((!this->WriteToOutputString) && ( !this->FileName ))
......@@ -2243,6 +2247,9 @@ void vtkDataWriter::CloseVTKFile(ostream *fp)
{
vtkDebugMacro(<<"Closing vtk file\n");
// Restore the previous locale settings
std::locale::global(this->CurrentLocale);
if ( fp != nullptr )
{
if (this->WriteToOutputString)
......
......@@ -32,6 +32,8 @@
#include "vtkIOLegacyModule.h" // For export macro
#include "vtkWriter.h"
#include <locale> // For locale settings
class vtkCellArray;
class vtkDataArray;
class vtkDataSet;
......@@ -315,6 +317,8 @@ protected:
char* PedigreeIdsName;
char* EdgeFlagsName;
std::locale CurrentLocale;
int WriteArray(ostream *fp, int dataType, vtkAbstractArray *data, const char *format,
vtkIdType num, vtkIdType numComp);
int WriteScalarData(ostream *fp, vtkDataArray *s, vtkIdType num);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment