Mercurial > code
changeset 409:0d004aba3ff6
Ini: put in ini namespace and remove class prefixes
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 06 Oct 2015 14:58:02 +0200 |
parents | f083259de5e6 |
children | a786ad1bdf75 |
files | C++/modules/Ini/Ini.cpp C++/modules/Ini/Ini.h C++/tests/Ini/main.cpp |
diffstat | 3 files changed, 86 insertions(+), 78 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/modules/Ini/Ini.cpp Tue Oct 06 14:48:36 2015 +0200 +++ b/C++/modules/Ini/Ini.cpp Tue Oct 06 14:58:02 2015 +0200 @@ -33,6 +33,8 @@ #include "Ini.h" +namespace ini { + namespace { /* -------------------------------------------------------- @@ -148,14 +150,14 @@ using TokenStack = std::vector<Token>; /* -------------------------------------------------------- - * IniBuilder + * Builder * -------------------------------------------------------- */ -class IniBuilder { +class Builder { private: std::string m_path; std::string m_base; - Ini &m_ini; + Document &m_ini; private: inline bool isReserved(char c) const noexcept @@ -263,16 +265,16 @@ } } - IniSection readSection(TokenStack::iterator &it, TokenStack::iterator end) + Section readSection(TokenStack::iterator &it, TokenStack::iterator end) { if (++it == end || it->type() != TokenType::Word) { - throw IniError(it->line(), it->position(), "word expected after [, got " + it->toString()); + throw Error(it->line(), it->position(), "word expected after [, got " + it->toString()); } - IniSection section(it->value()); + Section section(it->value()); if (++it == end || it->type() != TokenType::SectionEnd) { - throw IniError(it->line(), it->position(), "] expected, got " + it->toString()); + throw Error(it->line(), it->position(), "] expected, got " + it->toString()); } // Remove ] @@ -292,25 +294,25 @@ } else if (it->type() == TokenType::Word) { section.push_back(readOption(it, end)); } else { - throw IniError(it->line(), it->position(), "unexpected token " + it->toString()); + throw Error(it->line(), it->position(), "unexpected token " + it->toString()); } } return section; } - IniOption readOption(TokenStack::iterator &it, TokenStack::iterator end) + Option readOption(TokenStack::iterator &it, TokenStack::iterator end) { std::string key = it->value(); if (++it == end) { - throw IniError(it->line(), it->position(), "expected '=' after option declaration, got <EOF>"); + throw Error(it->line(), it->position(), "expected '=' after option declaration, got <EOF>"); } readSpace(it, end); if (it == end || it->type() != TokenType::Assign) { - throw IniError(it->line(), it->position(), "expected '=' after option declaration, got " + it++->toString()); + throw Error(it->line(), it->position(), "expected '=' after option declaration, got " + it++->toString()); } readSpace(++it, end); @@ -325,24 +327,24 @@ } if (it == end) - throw IniError(save->line(), save->position(), "undisclosed quote: " + save->toString() + " expected"); + throw Error(save->line(), save->position(), "undisclosed quote: " + save->toString() + " expected"); ++ it; } else if (it->type() == TokenType::Word) { oss << it++->value(); } else if (it->type() != TokenType::NewLine && it->type() != TokenType::Comment) { // No value requested, must be NewLine or comment - throw IniError(it->line(), it->position(), "expected option value after '=', got " + it->toString()); + throw Error(it->line(), it->position(), "expected option value after '=', got " + it->toString()); } - return IniOption(std::move(key), oss.str()); + return Option(std::move(key), oss.str()); } void readInclude(TokenStack::iterator &it, TokenStack::iterator end) { if (++it == end || (it->type() != TokenType::Word || it->value() != "include")) { - throw IniError(it->line(), it->position(), "expected `include' after '@' token, got " + it->toString()); + throw Error(it->line(), it->position(), "expected `include' after '@' token, got " + it->toString()); } readSpace(++it, end); @@ -351,12 +353,12 @@ TokenStack::iterator save = it; if (it == end || (it->type() != TokenType::QuoteSimple && it->type() != TokenType::QuoteDouble)) { - throw IniError(it->line(), it->position(), "expected filename after @include statement"); + throw Error(it->line(), it->position(), "expected filename after @include statement"); } // Filename if (++it == end || it->type() != TokenType::Word) { - throw IniError(it->line(), it->position(), "expected filename after @include statement"); + throw Error(it->line(), it->position(), "expected filename after @include statement"); } std::string value = it->value(); @@ -370,17 +372,17 @@ // Must be closed with the same quote if (++it == end || it->type() != save->type()) { - throw IniError(save->line(), save->position(), "undiclosed quote: " + save->toString() + " expected"); + throw Error(save->line(), save->position(), "undiclosed quote: " + save->toString() + " expected"); } // Remove quote ++ it; - IniBuilder(m_ini, fullpath); + Builder(m_ini, fullpath); } public: - IniBuilder(Ini &ini, std::string path) + Builder(Document &ini, std::string path) : m_path(path) , m_base(base(std::move(path))) , m_ini(ini) @@ -407,7 +409,7 @@ } else if (it->type() == TokenType::SectionBegin) { m_ini.push_back(readSection(it, end)); } else { - throw IniError(it->line(), it->position(), "unexpected " + it->toString() + " on root document"); + throw Error(it->line(), it->position(), "unexpected " + it->toString() + " on root document"); } } } @@ -416,10 +418,12 @@ } // !namespace /* -------------------------------------------------------- - * Ini + * Document * -------------------------------------------------------- */ -Ini::Ini(const std::string &path) +Document::Document(const std::string &path) { - IniBuilder(*this, path); + Builder(*this, path); } + +} // !ini \ No newline at end of file
--- a/C++/modules/Ini/Ini.h Tue Oct 06 14:48:36 2015 +0200 +++ b/C++/modules/Ini/Ini.h Tue Oct 06 14:58:02 2015 +0200 @@ -29,11 +29,13 @@ #include <stdexcept> #include <string> +namespace ini { + /** - * @class IniError + * @class Error * @brief Error in a file */ -class IniError : public std::exception { +class Error : public std::exception { private: int m_line; int m_position; @@ -47,7 +49,7 @@ * @param position the position * @param error the error */ - inline IniError(int line, int position, std::string error) + inline Error(int line, int position, std::string error) : m_line(line) , m_position(position) , m_error(std::move(error)) @@ -86,10 +88,10 @@ }; /** - * @class IniOption + * @class Option * @brief Option definition */ -class IniOption { +class Option { private: std::string m_key; std::string m_value; @@ -101,7 +103,7 @@ * @param key the key * @param value the value */ - inline IniOption(std::string key, std::string value) + inline Option(std::string key, std::string value) : m_key(std::move(key)) , m_value(std::move(value)) { @@ -129,18 +131,18 @@ }; /** - * @class IniSection + * @class Section * @brief Section that contains one or more options */ -class IniSection { +class Section { private: std::string m_key; - std::deque<IniOption> m_options; + std::deque<Option> m_options; template <typename T> T find(const std::string &key) const { - auto it = std::find_if(m_options.begin(), m_options.end(), [&] (const IniOption &o) { + auto it = std::find_if(m_options.begin(), m_options.end(), [&] (const Option &o) { return o.key() == key; }); @@ -154,7 +156,7 @@ /** * Default constructor has no sections and no values. */ - IniSection() = default; + Section() = default; /** * Construct a section with a set of options. @@ -162,7 +164,7 @@ * @param key the section name * @param options the list of options */ - inline IniSection(std::string key, std::deque<IniOption> options = {}) noexcept + inline Section(std::string key, std::deque<Option> options = {}) noexcept : m_key(std::move(key)) , m_options(std::move(options)) { @@ -243,7 +245,7 @@ * * @param option the option to add */ - inline void push_back(IniOption option) + inline void push_back(Option option) { m_options.push_back(std::move(option)); } @@ -253,7 +255,7 @@ * * @param option the option to add */ - inline void push_front(IniOption option) + inline void push_front(Option option) { m_options.push_front(std::move(option)); } @@ -275,7 +277,7 @@ * @return the option * @warning No bounds checking is performed */ - inline IniOption &operator[](int index) noexcept + inline Option &operator[](int index) noexcept { return m_options[index]; } @@ -287,7 +289,7 @@ * @return the option * @warning No bounds checking is performed */ - inline const IniOption &operator[](int index) const noexcept + inline const Option &operator[](int index) const noexcept { return m_options[index]; } @@ -299,9 +301,9 @@ * @return the option * @warning No bounds checking is performed */ - inline IniOption &operator[](const std::string &key) + inline Option &operator[](const std::string &key) { - return find<IniOption &>(key); + return find<Option &>(key); } /** @@ -311,9 +313,9 @@ * @return the option * @warning No bounds checking is performed */ - inline const IniOption &operator[](const std::string &key) const + inline const Option &operator[](const std::string &key) const { - return find<const IniOption &>(key); + return find<const Option &>(key); } }; @@ -321,14 +323,14 @@ * @class Ini * @brief Ini config file loader */ -class Ini { +class Document { private: - std::deque<IniSection> m_sections; + std::deque<Section> m_sections; template <typename T> T find(const std::string &key) const { - auto it = std::find_if(m_sections.begin(), m_sections.end(), [&] (const IniSection &s) { + auto it = std::find_if(m_sections.begin(), m_sections.end(), [&] (const Section &s) { return s.key() == key; }); @@ -342,15 +344,15 @@ /** * Default constructor with an empty configuration. */ - Ini() = default; + Document() = default; /** * Open the path as the configuration file. * * @param path the path - * @throw IniError on any error + * @throw Error on any error */ - Ini(const std::string &path); + Document(const std::string &path); /** * Get an iterator to the beginning. @@ -427,7 +429,7 @@ * * @param section the section to add */ - inline void push_back(IniSection section) + inline void push_back(Section section) { m_sections.push_back(std::move(section)); } @@ -437,7 +439,7 @@ * * @param section the section to add */ - inline void push_front(IniSection section) + inline void push_front(Section section) { m_sections.push_front(std::move(section)); } @@ -449,7 +451,7 @@ * @return the section * @warning No bounds checking is performed */ - inline IniSection &operator[](int index) noexcept + inline Section &operator[](int index) noexcept { return m_sections[index]; } @@ -461,7 +463,7 @@ * @return the section * @warning No bounds checking is performed */ - inline const IniSection &operator[](int index) const noexcept + inline const Section &operator[](int index) const noexcept { return m_sections[index]; } @@ -473,9 +475,9 @@ * @return the section * @warning No bounds checking is performed */ - inline IniSection &operator[](const std::string &key) + inline Section &operator[](const std::string &key) { - return find<IniSection &>(key); + return find<Section &>(key); } /** @@ -485,10 +487,12 @@ * @return the section * @warning No bounds checking is performed */ - inline const IniSection &operator[](const std::string &key) const + inline const Section &operator[](const std::string &key) const { - return find<IniSection &>(key); + return find<Section &>(key); } }; +} // !ini + #endif // !_INI_H_
--- a/C++/tests/Ini/main.cpp Tue Oct 06 14:48:36 2015 +0200 +++ b/C++/tests/Ini/main.cpp Tue Oct 06 14:58:02 2015 +0200 @@ -24,7 +24,7 @@ class BasicTest : public testing::Test { protected: - Ini m_ini; + ini::Document m_ini; public: BasicTest() @@ -39,7 +39,7 @@ ASSERT_EQ(1, static_cast<int>(m_ini.size())); } -TEST_F(BasicTest, iniOperators) +TEST_F(BasicTest, operators) { try { ASSERT_EQ(3, static_cast<int>(m_ini[0].size())); @@ -50,7 +50,7 @@ } } -TEST_F(BasicTest, iniSectionOperators) +TEST_F(BasicTest, sectionOperators) { try { // option1=1 (indexes) @@ -85,25 +85,25 @@ * Reserved tokens in words * -------------------------------------------------------- */ -TEST(Tokens, iniReserved) +TEST(Tokens, reserved) { try { - Ini ini("Ini/tokens.conf"); + ini::Document doc("Ini/tokens.conf"); - ASSERT_EQ("I have [brackets]", ini["tokens"]["bracket"].value()); - ASSERT_EQ("I have foo@at", ini["tokens"]["at"].value()); + ASSERT_EQ("I have [brackets]", doc["tokens"]["bracket"].value()); + ASSERT_EQ("I have foo@at", doc["tokens"]["at"].value()); } catch (const std::exception &ex) { FAIL() << ex.what(); } } /* -------------------------------------------------------- - * Multiple definition + * Multiple defini::Documenttion * -------------------------------------------------------- */ class MultiTest : public testing::Test { protected: - Ini m_ini; + ini::Document m_ini; public: MultiTest() @@ -131,7 +131,7 @@ class NoValueTest : public testing::Test { protected: - Ini m_ini; + ini::Document m_ini; public: NoValueTest() @@ -154,7 +154,7 @@ class IncludeTest : public testing::Test { protected: - Ini m_ini; + ini::Document m_ini; public: IncludeTest() @@ -183,12 +183,12 @@ TEST(Compact, test) { try { - Ini ini("Ini/compact.conf"); + ini::Document doc("Ini/compact.conf"); - ASSERT_EQ(2, static_cast<int>(ini.size())); - ASSERT_EQ("true", ini["general"]["verbose"].value()); - ASSERT_EQ("false", ini["general"]["foreground"].value()); - ASSERT_EQ("google.fr", ini["server"]["host"].value()); + ASSERT_EQ(2, static_cast<int>(doc.size())); + ASSERT_EQ("true", doc["general"]["verbose"].value()); + ASSERT_EQ("false", doc["general"]["foreground"].value()); + ASSERT_EQ("google.fr", doc["server"]["host"].value()); } catch (const std::exception &ex) { FAIL() << ex.what(); } @@ -202,7 +202,7 @@ { // An option outside a section is not allowed try { - Ini ini("Ini/error-nosection.conf"); + ini::Document doc("Ini/error-nosection.conf"); FAIL() << "Failure expected, got success"; } catch (const std::exception &ex) { @@ -213,7 +213,7 @@ { // The = assignment must be on the same line as the option key try { - Ini ini("Ini/error-lineassigment.conf"); + ini::Document doc("Ini/error-lineassigment.conf"); FAIL() << "Failure expected, got success"; } catch (const std::exception &ex) { @@ -224,7 +224,7 @@ { // Comment can't between option-key and = assigment try { - Ini ini("Ini/error-badcomment.conf"); + ini::Document doc("Ini/error-badcomment.conf"); FAIL() << "Failure expected, got success"; } catch (const std::exception &ex) { @@ -235,7 +235,7 @@ { // Bad section naming try { - Ini ini("Ini/error-badsection.conf"); + ini::Document doc("Ini/error-badsection.conf"); FAIL() << "Failure expected, got success"; } catch (const std::exception &ex) {