Skip to content

Commit

Permalink
when reading PAR file use support any line ending
Browse files Browse the repository at this point in the history
(for #18)
  • Loading branch information
wojdyr committed Sep 9, 2020
1 parent 60609da commit e5e1544
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion xylib/bruker_spc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void BrukerSpcDataSet::load_data(std::istream &f, const char* path)
#endif
if (par_file) {
std::string line;
while (std::getline(par_file, line, '\r')) {
while (getline_with_any_ending(par_file, line) && !par_file.eof()) {
std::string key, value;
str_split(line, ' ', key, value);
if (value.find('\n') == std::string::npos)
Expand Down
26 changes: 26 additions & 0 deletions xylib/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,32 @@ bool get_valid_line(std::istream &is, std::string &line, char comment_char)
return true;
}

// get line ending with \r, \n or \r\n
// based on https://stackoverflow.com/a/6089413/104453
std::istream& getline_with_any_ending(std::istream& is, std::string& t)
{
t.clear();
std::istream::sentry se(is, true);
std::streambuf* sb = is.rdbuf();
for (;;) {
int c = sb->sbumpc();
if (c == '\n')
return is;
if (c == '\r') {
if (sb->sgetc() == '\n')
sb->sbumpc();
return is;
}
if (c == std::streambuf::traits_type::eof()) {
// Also handle the case when the last line has no line ending
if (t.empty())
is.setstate(std::ios::eofbit);
return is;
}
t += (char)c;
}
}


void skip_whitespace(istream &f)
{
Expand Down
1 change: 1 addition & 0 deletions xylib/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ bool has_word(const std::string &sentence, const std::string &word);

std::string read_line(std::istream &is);
bool get_valid_line(std::istream &is, std::string &line, char comment_char);
std::istream& getline_with_any_ending(std::istream& is, std::string& t);

void skip_whitespace(std::istream &f);
Column* read_start_step_end_line(std::istream& f);
Expand Down

0 comments on commit e5e1544

Please sign in to comment.