From 234a384d88b2a00eca37bb5ac2d7ab7d85773567 Mon Sep 17 00:00:00 2001 From: Ricardas Jonaitis Date: Mon, 15 Jul 2024 13:14:26 +0300 Subject: [PATCH 1/2] Include due to std::cerr usage --- INI.h | 1 + 1 file changed, 1 insertion(+) diff --git a/INI.h b/INI.h index a8c99ce..b336906 100644 --- a/INI.h +++ b/INI.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include From 85a7d3a89dd0e179587a87b8d12ea58a896e7881 Mon Sep 17 00:00:00 2001 From: Ricardas Jonaitis Date: Mon, 15 Jul 2024 13:17:25 +0300 Subject: [PATCH 2/2] Fix parser usage in multiple compilation units causing multiple definition errors. --- INI.h | 56 ++++++++++++++++++++++----------------------- example/example.cpp | 4 ++-- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/INI.h b/INI.h index b336906..2c5d91b 100644 --- a/INI.h +++ b/INI.h @@ -43,20 +43,20 @@ #endif -fini_string_t& l_trim(fini_string_t& str, const fini_string_t trim_chars = "\t\v\f; ") { +static inline fini_string_t& l_trim(fini_string_t& str, const fini_string_t trim_chars = "\t\v\f; ") { str.erase(0, str.find_first_not_of(trim_chars)); return str; } -fini_string_t& r_trim(fini_string_t& str, const fini_string_t trim_chars = "\t\v\f; ") { +static inline fini_string_t& r_trim(fini_string_t& str, const fini_string_t trim_chars = "\t\v\f; ") { str.erase(str.find_last_not_of(trim_chars) + 1); return str; } -fini_string_t& trim(fini_string_t& str, const fini_string_t trim_chars = "\t\v\f; ") { +static inline fini_string_t& trim(fini_string_t& str, const fini_string_t trim_chars = "\t\v\f; ") { return l_trim(r_trim(str, trim_chars), trim_chars); } template -T convert_to (const fini_string_t &str) { +inline T convert_to (const fini_string_t &str) { fini_sstream_t ss(str); T num; ss >> num; @@ -64,22 +64,22 @@ T convert_to (const fini_string_t &str) { } template <> -fini_string_t convert_to(const fini_string_t &str) { +inline fini_string_t convert_to(const fini_string_t &str) { return str; } template <> -const char* convert_to(const fini_string_t &str) { +inline const char* convert_to(const fini_string_t &str) { return str.c_str(); } +static int INI_DEFAULT_PARSE_FLAGS = 0, INI_DEFAULT_SAVE_FLAGS = 0; + /// class INI { public: /// Define - static int PARSE_FLAGS, SAVE_FLAGS; - typedef fini_char_t data_t; typedef typename std::map keys_t; @@ -125,7 +125,7 @@ class INI }; /// Definition -INI::INI(const INI& from): source(from.source), filename(from.filename) { +inline INI::INI(const INI& from): source(from.source), filename(from.filename) { // Deep clone INI for(auto i: from.sections) { select(i.first); @@ -134,23 +134,23 @@ INI::INI(const INI& from): source(from.source), filename(from.filename) { } } -INI::INI(fini_string_t filename, bool doParse, int parseFlags): source(SOURCE_FILE), filename(filename) { +inline INI::INI(fini_string_t filename, bool doParse, int parseFlags): source(SOURCE_FILE), filename(filename) { this->create(""); if (doParse) parse(parseFlags); } -INI:: ~INI() { +inline INI:: ~INI() { clear(); } -void INI::clear() { +inline void INI::clear() { sections.clear(); } -bool INI::parse(int parseFlags) { - parseFlags = (parseFlags > 0)? parseFlags: PARSE_FLAGS; +inline bool INI::parse(int parseFlags) { + parseFlags = (parseFlags > 0)? parseFlags: INI_DEFAULT_PARSE_FLAGS; switch(source) { @@ -175,9 +175,7 @@ bool INI::parse(int parseFlags) { return true; } -int INI::PARSE_FLAGS = 0, INI::SAVE_FLAGS = 0; - -void INI::_parseFile(fini_ifstream_t& file, int parseFlags) { +inline void INI::_parseFile(fini_ifstream_t& file, int parseFlags) { fini_string_t line; fini_string_t section; // Set default section (support for sectionless files) size_t i = 0; @@ -225,8 +223,8 @@ void INI::_parseFile(fini_ifstream_t& file, int parseFlags) { } } -bool INI::save(fini_string_t filename, int saveFlags) { - saveFlags = (saveFlags > 0)? saveFlags: SAVE_FLAGS; +inline bool INI::save(fini_string_t filename, int saveFlags) { + saveFlags = (saveFlags > 0)? saveFlags: INI_DEFAULT_SAVE_FLAGS; fini_ofstream_t file((filename == "")? this->filename: filename, std::ios::trunc); if (!file.is_open()) @@ -278,13 +276,13 @@ bool INI::save(fini_string_t filename, int saveFlags) { } //Provide bracket access to section contents -INI::keys_t& INI::operator[](fini_string_t section) { +inline INI::keys_t& INI::operator[](fini_string_t section) { select(section); return *current; } //Create a new section and select it -void INI::create(fini_string_t section) { +inline void INI::create(fini_string_t section) { if (section != "" && sections.find(section) != sections.end()) { std::cerr << "Error: cpp-feather-ini-parser: Duplicate section '" << section << "'" << std::endl; throw -1; @@ -295,7 +293,7 @@ void INI::create(fini_string_t section) { } //Removes a section including all key/value pairs -void INI::remove(fini_string_t section) { +inline void INI::remove(fini_string_t section) { if (select(section, true)) sections.erase(section); @@ -303,7 +301,7 @@ void INI::remove(fini_string_t section) { } //Select a section for performing operations -bool INI::select(fini_string_t section, bool noCreate) { +inline bool INI::select(fini_string_t section, bool noCreate) { sections_t::iterator sectionsit = sections.find(section); if (sectionsit == sections.end()) { if (!noCreate) @@ -316,11 +314,11 @@ bool INI::select(fini_string_t section, bool noCreate) { return true; } -fini_string_t INI::get(fini_string_t section, fini_string_t key, fini_string_t def) { +inline fini_string_t INI::get(fini_string_t section, fini_string_t key, fini_string_t def) { return get(key, def); } -fini_string_t INI::get(fini_string_t key, fini_string_t def) { +inline fini_string_t INI::get(fini_string_t key, fini_string_t def) { auto it = current->find(key); if (it == current->end()) return def; @@ -329,12 +327,12 @@ fini_string_t INI::get(fini_string_t key, fini_string_t def) { } template -T INI::getAs(fini_string_t section, fini_string_t key, T def) { +inline T INI::getAs(fini_string_t section, fini_string_t key, T def) { return getAs(key, def); } template -T INI::getAs(fini_string_t key, T def) { +inline T INI::getAs(fini_string_t key, T def) { auto it = current->find(key); if (it == current->end()) return def; @@ -342,13 +340,13 @@ T INI::getAs(fini_string_t key, T def) { return convert_to(it->second); } -void INI::set(fini_string_t section, fini_string_t key, fini_string_t value) { +inline void INI::set(fini_string_t section, fini_string_t key, fini_string_t value) { if (!select(section)) create(section); set(key, value); } -void INI::set(fini_string_t key, fini_string_t value) { +inline void INI::set(fini_string_t key, fini_string_t value) { (*current)[key] = value; } diff --git a/example/example.cpp b/example/example.cpp index aa3b40f..a82263b 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -12,8 +12,8 @@ std::string getStringFromFile(const std::string& path); //Source for data loadin int main() { - INI::PARSE_FLAGS = INI::PARSE_COMMENTS_ALL | INI::PARSE_COMMENTS_SLASH | INI::PARSE_COMMENTS_HASH; - INI::SAVE_FLAGS = INI::SAVE_PRUNE | INI::SAVE_PADDING_SECTIONS | INI::SAVE_SPACE_SECTIONS | INI::SAVE_SPACE_KEYS | INI::SAVE_TAB_KEYS | INI::SAVE_SEMICOLON_KEYS; + INI_DEFAULT_PARSE_FLAGS = INI::PARSE_COMMENTS_ALL | INI::PARSE_COMMENTS_SLASH | INI::PARSE_COMMENTS_HASH; + INI_DEFAULT_SAVE_FLAGS = INI::SAVE_PRUNE | INI::SAVE_PADDING_SECTIONS | INI::SAVE_SPACE_SECTIONS | INI::SAVE_SPACE_KEYS | INI::SAVE_TAB_KEYS | INI::SAVE_SEMICOLON_KEYS; INI ini2("file.ini", true); // Assign ini file and parse INI ini(ini2); // Clone