Skip to content
Snippets Groups Projects
Commit b7e58f3d authored by Vasileios Karakasis's avatar Vasileios Karakasis
Browse files

Replaced line buffer by an std::string.

The line buffer is no more a pure character array but a standard string.
For reading lines from the input stream we use the higher level
std::getline() function which takes care of overflow issues.

Side improvements: String comment prefixes are now supported.

Other changes: added `-pedantic' to the compilation flags, to force
strict C++11 compliance.
parent 330f437f
No related branches found
No related tags found
No related merge requests found
...@@ -343,13 +343,6 @@ TEST(swc_parser, invalid_input) ...@@ -343,13 +343,6 @@ TEST(swc_parser, invalid_input)
EXPECT_THROW(is >> cell, std::logic_error); EXPECT_THROW(is >> cell, std::logic_error);
} }
{
// Check long lines
std::istringstream is(std::string(256, 'a') + "\n");
cell_record cell;
EXPECT_THROW(is >> cell, std::runtime_error);
}
{ {
// Check non-parsable values // Check non-parsable values
std::istringstream is("1a 1 14.566132 34.873772 7.857000 0.717830 -1\n"); std::istringstream is("1a 1 14.566132 34.873772 7.857000 0.717830 -1\n");
......
CC=clang++ CC=clang++
FLAGS=-std=c++11 -g FLAGS=-std=c++11 -g -pedantic
test.exe : main.cpp *.hpp makefile gtest.o test.exe : main.cpp *.hpp makefile gtest.o
$(CC) $(FLAGS) main.cpp -o test.exe gtest.o -pthread $(CC) $(FLAGS) main.cpp -o test.exe gtest.o -pthread
......
...@@ -12,6 +12,11 @@ namespace io ...@@ -12,6 +12,11 @@ namespace io
{ {
static bool starts_with(const std::string &str, const std::string &prefix)
{
return (str.find(prefix) == 0);
}
class cell_record class cell_record
{ {
public: public:
...@@ -140,37 +145,22 @@ class swc_parser ...@@ -140,37 +145,22 @@ class swc_parser
{ {
public: public:
swc_parser(const std::string &delim, swc_parser(const std::string &delim,
char comment_prefix, std::string comment_prefix)
std::size_t max_fields,
std::size_t max_line)
: delim_(delim) : delim_(delim)
, comment_prefix_(comment_prefix) , comment_prefix_(comment_prefix)
, max_fields_(max_fields) { }
, max_line_(max_line)
{
init_linebuff();
}
swc_parser() swc_parser()
: delim_(" ") : delim_(" ")
, comment_prefix_('#') , comment_prefix_("#")
, max_fields_(7) { }
, max_line_(256)
{
init_linebuff();
}
~swc_parser()
{
delete[] linebuff_;
}
std::istream &parse_record(std::istream &is, cell_record &cell) std::istream &parse_record(std::istream &is, cell_record &cell)
{ {
while (!is.eof() && !is.bad()) { while (!is.eof() && !is.bad()) {
// consume empty and comment lines first // consume empty and comment lines first
is.getline(linebuff_, max_line_); std::getline(is, linebuff_);
if (linebuff_[0] && linebuff_[0] != comment_prefix_) if (!linebuff_.empty() && !starts_with(linebuff_, comment_prefix_))
break; break;
} }
...@@ -179,11 +169,11 @@ public: ...@@ -179,11 +169,11 @@ public:
return is; return is;
if (is.eof() && if (is.eof() &&
(linebuff_[0] == 0 || linebuff_[0] == comment_prefix_)) (linebuff_.empty() || starts_with(linebuff_, comment_prefix_)))
// last line is either empty or a comment; don't parse anything // last line is either empty or a comment; don't parse anything
return is; return is;
if (is.fail() && is.gcount() == max_line_ - 1) if (is.fail())
throw std::runtime_error("too long line detected"); throw std::runtime_error("too long line detected");
std::istringstream line(linebuff_); std::istringstream line(linebuff_);
...@@ -192,11 +182,6 @@ public: ...@@ -192,11 +182,6 @@ public:
} }
private: private:
void init_linebuff()
{
linebuff_ = new char[max_line_];
}
void check_parse_status(const std::istream &is) void check_parse_status(const std::istream &is)
{ {
if (is.fail()) if (is.fail())
...@@ -205,13 +190,11 @@ private: ...@@ -205,13 +190,11 @@ private:
throw std::logic_error("could not parse value"); throw std::logic_error("could not parse value");
} }
// FIXME: need not to be a member function
template<typename T> template<typename T>
T parse_value_strict(std::istream &is) T parse_value_strict(std::istream &is)
{ {
T val; T val;
is >> val; is >> val;
// std::cout << val << "\n";
check_parse_status(is); check_parse_status(is);
// everything's fine // everything's fine
...@@ -222,10 +205,8 @@ private: ...@@ -222,10 +205,8 @@ private:
cell_record parse_record(std::istringstream &is); cell_record parse_record(std::istringstream &is);
std::string delim_; std::string delim_;
char comment_prefix_; std::string comment_prefix_;
std::size_t max_fields_; std::string linebuff_;
std::size_t max_line_;
char *linebuff_;
}; };
......
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