# HG changeset patch # User David Demelier # Date 1444978220 -7200 # Node ID 88df9c580c36d839996a090603d8677e53d2a744 # Parent 625f5d64b09362e654cf176830fbb09ed5ce77b1 Ini: throw error if file could not be opened diff -r 625f5d64b093 -r 88df9c580c36 C++/modules/Ini/Ini.cpp --- a/C++/modules/Ini/Ini.cpp Wed Oct 14 20:19:28 2015 +0200 +++ b/C++/modules/Ini/Ini.cpp Fri Oct 16 08:50:20 2015 +0200 @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -391,6 +392,11 @@ Tokens Document::analyze(const File &file) { std::fstream stream{file.path}; + + if (!stream) { + throw std::runtime_error{std::strerror(errno)}; + } + std::istreambuf_iterator it{stream}; std::istreambuf_iterator end{}; diff -r 625f5d64b093 -r 88df9c580c36 C++/modules/Ini/Ini.h --- a/C++/modules/Ini/Ini.h Wed Oct 14 20:19:28 2015 +0200 +++ b/C++/modules/Ini/Ini.h Fri Oct 16 08:50:20 2015 +0200 @@ -55,10 +55,10 @@ * @param c the column * @param m the message */ - inline Error(int l, int c, std::string m) noexcept - : m_line{l} - , m_column{c} - , m_message{std::move(m)} + inline Error(int line, int column, std::string message) noexcept + : m_line{line} + , m_column{column} + , m_message{std::move(message)} { } @@ -372,14 +372,6 @@ * Path to the file. */ std::string path; - - /** - * Load the file into the document. - * - * @param doc the document - * @throw Error on errors - */ - void load(Document &doc); }; /** @@ -393,14 +385,6 @@ * The ini content. */ std::string text; - - /** - * Load the file into the document. - * - * @param doc the document - * @throw Error on errors - */ - void load(Document &doc); }; /** diff -r 625f5d64b093 -r 88df9c580c36 C++/tests/Ini/main.cpp --- a/C++/tests/Ini/main.cpp Wed Oct 14 20:19:28 2015 +0200 +++ b/C++/tests/Ini/main.cpp Fri Oct 16 08:50:20 2015 +0200 @@ -281,6 +281,16 @@ } } +TEST(Errors, notFound) +{ + try { + ini::Document doc{ini::File{"does not exists"}}; + + FAIL() << "Failure expected, got success"; + } catch (const std::exception &) { + } +} + int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv);