Mercurial > code
changeset 432:88df9c580c36
Ini: throw error if file could not be opened
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 16 Oct 2015 08:50:20 +0200 |
parents | 625f5d64b093 |
children | 70bcc6962c88 |
files | C++/modules/Ini/Ini.cpp C++/modules/Ini/Ini.h C++/tests/Ini/main.cpp |
diffstat | 3 files changed, 20 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- 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 <cassert> #include <cctype> +#include <cstring> #include <iostream> #include <iterator> #include <fstream> @@ -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<char> it{stream}; std::istreambuf_iterator<char> end{};
--- 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); }; /**
--- 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);