Merge branch 'textinarchive_windows_unix_compatibility' into 'master'

Fix TextOutArchive (Win) -> TextInArchive (Unix) incompatibility

See merge request ngsolve/netgen!671
This commit is contained in:
Lackner, Christopher 2024-09-18 17:48:37 +02:00
commit e94096e008

View File

@ -1,6 +1,7 @@
#ifndef NETGEN_CORE_ARCHIVE_HPP #ifndef NETGEN_CORE_ARCHIVE_HPP
#define NETGEN_CORE_ARCHIVE_HPP #define NETGEN_CORE_ARCHIVE_HPP
#include <algorithm>
#include <any> #include <any>
#include <array> // for array #include <array> // for array
#include <complex> // for complex #include <complex> // for complex
@ -1136,15 +1137,32 @@ namespace ngcore
{ char c; *stream >> c; b = (c=='t'); return *this; } { char c; *stream >> c; b = (c=='t'); return *this; }
Archive & operator & (std::string & str) override Archive & operator & (std::string & str) override
{ {
// Ignore \r (carriage return) characters when reading strings
// this is necessary for instance when a file was written on Windows and is read on Unix
int len; int len;
*stream >> len; *stream >> len;
char ch; char ch;
stream->get(ch); // '\n' stream->get(ch); // read newline character
if(ch == '\r') // windows line endings -> read \n as well if(ch == '\r') // windows line endings -> read \n as well
stream->get(ch); stream->get(ch);
str.resize(len); str.resize(len);
if(len) if(len)
stream->get(&str[0], len+1, '\0'); stream->get(&str[0], len+1, '\0');
// remove all \r characters from the string, check if size changed
// if so, read the remaining characters
str.erase(std::remove(str.begin(), str.end(), '\r'), str.cend());
size_t chars_to_read = len-str.size();
while (chars_to_read>0)
{
auto old_size = str.size();
str.resize(len);
stream->get(&str[old_size], chars_to_read+1, '\0');
str.erase(std::remove(str.begin()+old_size, str.end(), '\r'), str.cend());
chars_to_read = len - str.size();
}
return *this; return *this;
} }
Archive & operator & (char *& str) override Archive & operator & (char *& str) override