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

Added an swc_parse_error exception.

This exception is thrown when a parse error occurs, instead of
std::runtime_error and std::logical_error that used to be thrown before.
parent ac97ae0d
No related branches found
No related tags found
No related merge requests found
......@@ -166,6 +166,18 @@ private:
int parent_id_; // cell parent's id
};
class swc_parse_error : public std::runtime_error
{
public:
explicit swc_parse_error(const char *msg)
: std::runtime_error(msg)
{ }
explicit swc_parse_error(const std::string &msg)
: std::runtime_error(msg)
{ }
};
class swc_parser
{
public:
......@@ -199,7 +211,7 @@ public:
return is;
if (is.fail())
throw std::runtime_error("too long line detected");
throw swc_parse_error("too long line detected");
std::istringstream line(linebuff_);
cell = parse_record(line);
......@@ -211,8 +223,7 @@ private:
{
if (is.fail())
// If we try to read past the eof; fail bit will also be set
// FIXME: better throw a custom parse_error exception
throw std::logic_error("could not parse value");
throw swc_parse_error("could not parse value");
}
template<typename T>
......@@ -270,15 +281,16 @@ std::istream &operator>>(std::istream &is, cell_record &cell)
return is;
}
std::ostream &operator<<(std::ostream &os, const cell_record &cell)
{
// output in one-based indexing
os << cell.id_+1 << " "
<< cell.type_ << " "
<< std::setprecision(7) << cell.x_ << " "
<< std::setprecision(7) << cell.y_ << " "
<< std::setprecision(7) << cell.z_ << " "
<< std::setprecision(7) << cell.r_ << " "
<< std::setprecision(7) << cell.x_ << " "
<< std::setprecision(7) << cell.y_ << " "
<< std::setprecision(7) << cell.z_ << " "
<< std::setprecision(7) << cell.r_ << " "
<< ((cell.parent_id_ == -1) ? cell.parent_id_ : cell.parent_id_+1);
return os;
......
......@@ -111,14 +111,14 @@ TEST(swc_parser, invalid_input)
// check incomplete lines; missing parent
std::istringstream is("1 1 14.566132 34.873772 7.857000 0.717830\n");
cell_record cell;
EXPECT_THROW(is >> cell, std::logic_error);
EXPECT_THROW(is >> cell, swc_parse_error);
}
{
// Check non-parsable values
std::istringstream is("1a 1 14.566132 34.873772 7.857000 0.717830 -1\n");
cell_record cell;
EXPECT_THROW(is >> cell, std::logic_error);
EXPECT_THROW(is >> cell, swc_parse_error);
}
{
......
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