Skip to content
Snippets Groups Projects
Commit 34cd1031 authored by Bienchen's avatar Bienchen
Browse files

Made StarParser sensible to problems on file opening.

parent bb2b9454
No related branches found
No related tags found
No related merge requests found
......@@ -33,18 +33,23 @@ namespace ost { namespace io {
StarParser::StarParser(std::istream& stream, bool items_as_row):
filename_("<stream>"), line_num_(0),
has_current_line_(false), current_line_(),
items_row_header_(), items_row_columns_(),
items_row_header_(), file_open_(true), items_row_columns_(),
items_row_values_()
{
items_as_row_ = items_as_row;
if (!stream) {
file_open_ = false;
}
stream_.push(stream);
}
StarParser::StarParser(const String& filename, bool items_as_row):
fstream_(filename.c_str()), filename_(filename),
line_num_(0), has_current_line_(false), current_line_(),
items_row_header_(), items_row_columns_(), items_row_values_()
items_row_header_(), file_open_(true), items_row_columns_(),
items_row_values_()
{
items_as_row_ = items_as_row;
......@@ -54,6 +59,10 @@ StarParser::StarParser(const String& filename, bool items_as_row):
}
stream_.push(fstream_);
if (!fstream_) {
file_open_ = false;
}
}
String StarParser::FormatDiagnostic(StarDiagType type, const String& message,
......@@ -536,6 +545,11 @@ void StarParser::ParseGlobal()
void StarParser::Parse()
{
if (!file_open_) {
throw IOException(this->FormatDiagnostic(STAR_DIAG_ERROR,
"Failed to open file '" +
filename_ + "'!"));
}
StringRef line;
std::stringstream ss;
while (this->GetLine(line)) {
......
......@@ -277,6 +277,7 @@ private:
String current_line_;
bool items_as_row_;
StarLoopDesc items_row_header_;
bool file_open_;
std::vector<StringRef> items_row_columns_;
std::vector<String> items_row_values_;
};
......
......@@ -238,11 +238,12 @@ BOOST_AUTO_TEST_CASE(star_data_item)
BOOST_MESSAGE(" Running star_data_item tests...");
std::ifstream s("testfiles/data-item.cif");
DataItemTestParser star_p(s);
star_p.Parse();
BOOST_CHECK_NO_THROW(star_p.Parse());
BOOST_CHECK_EQUAL(star_p.s1, "a");
BOOST_CHECK_EQUAL(star_p.s2, "a b c");
BOOST_CHECK_EQUAL(star_p.s3, "a\nb\nc");
BOOST_CHECK_EQUAL(star_p.s4, "a'b");
BOOST_MESSAGE(" done.");
}
BOOST_AUTO_TEST_CASE(format_diag_stream)
......@@ -450,5 +451,14 @@ BOOST_AUTO_TEST_CASE(star_try_bool_conversions)
IOException);
BOOST_MESSAGE(" done.");
}
BOOST_AUTO_TEST_CASE(star_wrong_filename)
{
BOOST_MESSAGE(" Running star_wrong_filename tests...");
DataItemTestParser star_p("testfiles/doesnotexist.foo");
BOOST_CHECK_THROW(star_p.Parse(), IOException);
BOOST_MESSAGE(" done.");
}
BOOST_AUTO_TEST_SUITE_END();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment