diff --git a/src/swcio.cpp b/src/swcio.cpp index 98178ea8cb0b282e58016b8ceb75a94b7ffec25e..41472494754fcd21b9f0df807ec86148e8058131 100644 --- a/src/swcio.cpp +++ b/src/swcio.cpp @@ -123,50 +123,16 @@ swc_record::kind parse_value_strict(std::istream &is, const swc_parser &parser) // swc_parser implementation // -std::istream& swc_parser::safe_getline(std::istream& is, std::string& t) -{ - // Based on http://stackoverflow.com/questions/6089231/getting-std-ifstream-to-handle-lf-cr-and-crlf - t.clear(); - - // The characters in the stream are read one-by-one using a std::streambuf. - // That is faster than reading them one-by-one using the std::istream. - // Code that uses streambuf this way must be guarded by a sentry object. - // The sentry object performs various tasks, - // such as thread synchronization and updating the stream state. - - std::istream::sentry se(is, true); - std::streambuf* sb = is.rdbuf(); - - for (;;) { - int c = sb->sbumpc(); - switch (c) { - case '\n': - return is; - case '\r': - if (sb->sgetc() == '\n') { - sb->sbumpc(); - } - return is; - case EOF: - // Also handle the case when the last line has no line ending - if (t.empty()) { - is.setstate(std::ios::eofbit); - } - return is; - default: - t += (char)c; - } - } -} - std::istream &swc_parser::parse_record(std::istream &is, swc_record &record) { while (!is.eof() && !is.bad()) { // consume empty and comment lines first - safe_getline(is, linebuff_); + std::getline(is, linebuff_); ++lineno_; - if (!linebuff_.empty() && !starts_with(linebuff_, comment_prefix_)) + if (!linebuff_.empty() && + !starts_with(linebuff_, comment_prefix_) && + !starts_with(linebuff_, "\r")) { break; } diff --git a/src/swcio.hpp b/src/swcio.hpp index 7d940290f30fb45c7d0c9378ff0cbdbf199105f2..4b6457123e062c65a2091853bc3e581e3c1eb8e7 100644 --- a/src/swcio.hpp +++ b/src/swcio.hpp @@ -212,8 +212,6 @@ public: std::istream &parse_record(std::istream &is, swc_record &record); private: - // Getline version supporting both windows and linux line endings - std::istream &safe_getline(std::istream& is, std::string& t); // Read the record from a string stream; will be treated like a single line swc_record parse_record(std::istringstream &is); diff --git a/tests/test_swcio.cpp b/tests/test_swcio.cpp index c07cb1bbc6a257323b20b5415df8c7b271a6fe26..0ebe9c84b06ff7c22db289ebcb79c21eb50239d4 100644 --- a/tests/test_swcio.cpp +++ b/tests/test_swcio.cpp @@ -525,7 +525,7 @@ TEST(swc_parser, from_file_ball_and_stick) // check that windows EOL are supported in linux. // This test is based on the ball_and_stick.swc with windows endings inserted // manually in a file stream, regression test for issue_34 -TEST(swc_parser, windows_mac_eol) +TEST(swc_parser, windows_eol) { // Check valid usage @@ -534,9 +534,9 @@ TEST(swc_parser, windows_mac_eol) is << "# - soma with radius 12.6157 2\r\n"; is << "# - dendrite with length 200 and radius 0.5\r\n"; is << "\r\n"; // parser stubles over empty line with \r\n - is << "1 1 0.0 0.0 0.0 6.30785 -1\r"; // Test old style max eol - is << "2 2 6.30785 0.0 0.0 0.5 1\r"; - is << "3 2 206.30785 0.0 0.0 0.5 2\r"; + is << "1 1 0.0 0.0 0.0 6.30785 -1\r\n"; + is << "2 2 6.30785 0.0 0.0 0.5 1\r\n"; + is << "3 2 206.30785 0.0 0.0 0.5 2\r\n"; is << "\n"; // read the file into a cell object