WSL/SLF GitLab Repository

Commit 62882b37 authored by Mathias Bavay's avatar Mathias Bavay
Browse files

A bug was found in libsmet for Windows: if opening a file in ascii mode, end...

A bug was found in libsmet for Windows: if opening a file in ascii mode, end of lines are automatically translated, messing up our own effort to handle them, thus corrupting our readings. Now, all files are opened in binary mode.
parent 73acf900
......@@ -420,7 +420,7 @@ bool SMETWriter::valid_header()
void SMETWriter::write(const std::vector<std::string>& vec_timestamp, const std::vector<double>& data)
{
fout.open(filename.c_str());
fout.open(filename.c_str(), ios::binary);
if (fout.fail()) {
stringstream ss;
ss << "Error openning file \"" << filename << "\" for writing, possible reason: " << strerror(errno);
......@@ -466,7 +466,7 @@ void SMETWriter::write(const std::vector<std::string>& vec_timestamp, const std:
void SMETWriter::write(const std::vector<double>& data)
{
fout.open(filename.c_str());
fout.open(filename.c_str(), ios::binary);
if (fout.fail()) {
stringstream ss;
ss << "Error openning file \"" << filename << "\" for writing, possible reason: " << strerror(errno);
......@@ -663,7 +663,7 @@ SMETReader::SMETReader(const std::string& in_fname) : filename(in_fname), nr_of_
{
std::ifstream fin; //Input file streams
fin.clear();
fin.open (filename.c_str(), ios::in);
fin.open (filename.c_str(), ios::in|ios::binary); //ascii does end of line translation, which messes up the pointer code
if (fin.fail()) {
stringstream ss;
ss << "Error openning file \"" << filename << "\" for reading, possible reason: " << strerror(errno);
......@@ -934,7 +934,7 @@ void SMETReader::checkSignature(const std::vector<std::string>& vecSignature, bo
void SMETReader::read(const std::string& i_timestamp_start, const std::string& i_timestamp_end,
std::vector<std::string>& vec_timestamp, std::vector<double>& vec_data)
{
{
if (!timestamp_present){
read(vec_timestamp, vec_data);
} else {
......@@ -970,7 +970,7 @@ void SMETReader::read(std::vector<std::string>& vec_timestamp, std::vector<doubl
ifstream fin;
fin.clear();
fin.open (filename.c_str(), ios::in);
fin.open (filename.c_str(), ios::in|ios::binary); //ascii mode messes up pointer code on windows (automatic eol translation)
if (fin.fail()) {
stringstream ss;
ss << "Error openning file \"" << filename << "\" for reading, possible reason: " << strerror(errno);
......@@ -1011,9 +1011,9 @@ void SMETReader::read(std::vector<double>& vec_data)
vector<string> tmp_vec;
ios_base::openmode mode = ios::in;
if (!isAscii)
mode = ios::in | ios::binary;
ios_base::openmode mode = ios::in|ios::binary; //read as binary to avoid eol mess
/*if (!isAscii)
mode = ios::in | ios::binary;*/
ifstream fin;
fin.open (filename.c_str(), mode);
......@@ -1047,13 +1047,13 @@ void SMETReader::read(std::vector<double>& vec_data)
}
void SMETReader::read_data_ascii(std::ifstream& fin, std::vector<std::string>& vec_timestamp, std::vector<double>& vec_data)
{
{
size_t nr_of_data_fields = nr_of_fields;
if (timestamp_present) nr_of_data_fields++;
vector<string> tmp_vec;
string line;
streampos current_fpointer = -1;
streampos current_fpointer = -1;
while (!fin.eof()){
line = "";
......@@ -1061,13 +1061,13 @@ void SMETReader::read_data_ascii(std::ifstream& fin, std::vector<std::string>& v
getline(fin, line, eoln);
SMETCommon::stripComments(line);
SMETCommon::trim(line);
if (line == "") continue; //Pure comment lines and empty lines are ignored
if (line == "") continue; //Pure comment lines and empty lines are ignored
if (SMETCommon::readLineToVec(line, tmp_vec) == nr_of_data_fields){
try {
size_t shift = 0;
if (julian_interval && julian_present){
const double current_julian = SMETCommon::convert_to_double(tmp_vec[julian_field]);
const double current_julian = SMETCommon::convert_to_double(tmp_vec[julian_field]);
if (current_julian < julian_start)
continue; //skip lines that don't hold the dates we're interested in
else if (current_julian > julian_end)
......@@ -1075,7 +1075,7 @@ void SMETReader::read_data_ascii(std::ifstream& fin, std::vector<std::string>& v
}
if (timestamp_interval && timestamp_present){
const string& current_timestamp = tmp_vec[timestamp_field];
const string& current_timestamp = tmp_vec[timestamp_field];
if (current_timestamp < timestamp_start)
continue; //skip lines that don't hold the dates we're interested in
else if (current_timestamp > timestamp_end)
......@@ -1097,10 +1097,10 @@ void SMETReader::read_data_ascii(std::ifstream& fin, std::vector<std::string>& v
}
current_fpointer = tmp_fpointer;
} catch(SMETException&) {
std::cout << "Error reading file \"" << filename << "\" at line \"" << line << "\"\n";
std::cout << "Error reading file \"" << filename << "\" at line \"" << line << "\"" << std::endl;
throw;
}
} else {
} else {
std::stringstream ss;
ss << "File \'" << filename << "\' declares " << nr_of_data_fields << " columns ";
ss << "but this does not match the following line:\n" << line;
......
Supports Markdown
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