/* MIT License Copyright (c) 2017-2020 Matthias C. M. Troffaes Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #pragma once #include #include #include #include #include #include #include #include #include #include #include namespace inipp { namespace detail { // trim functions based on http://stackoverflow.com/a/217605 template inline void ltrim(std::basic_string & s, const std::locale & loc) { s.erase(s.begin(), std::find_if(s.begin(), s.end(), [&loc](CharT ch) { return !std::isspace(ch, loc); })); } template inline void rtrim(std::basic_string & s, const std::locale & loc) { s.erase(std::find_if(s.rbegin(), s.rend(), [&loc](CharT ch) { return !std::isspace(ch, loc); }).base(), s.end()); } // string replacement function based on http://stackoverflow.com/a/3418285 template inline bool replace(std::basic_string & str, const std::basic_string & from, const std::basic_string & to) { auto changed = false; size_t start_pos = 0; while ((start_pos = str.find(from, start_pos)) != std::basic_string::npos) { str.replace(start_pos, from.length(), to); start_pos += to.length(); changed = true; } return changed; } } // namespace detail template inline bool extract(const std::basic_string & value, T & dst) { CharT c; std::basic_istringstream is{ value }; T result; if ((is >> std::boolalpha >> result) && !(is >> c)) { dst = result; return true; } else { return false; } } template inline bool extract(const std::basic_string & value, std::basic_string & dst) { dst = value; return true; } template class Format { public: // used for generating const CharT char_section_start; const CharT char_section_end; const CharT char_assign; const CharT char_comment; // used for parsing virtual bool is_section_start(CharT ch) const { return ch == char_section_start; } virtual bool is_section_end(CharT ch) const { return ch == char_section_end; } virtual bool is_assign(CharT ch) const { return ch == char_assign; } virtual bool is_comment(CharT ch) const { return ch == char_comment; } // used for interpolation const CharT char_interpol; const CharT char_interpol_start; const CharT char_interpol_sep; const CharT char_interpol_end; Format(CharT section_start, CharT section_end, CharT assign, CharT comment, CharT interpol, CharT interpol_start, CharT interpol_sep, CharT interpol_end) : char_section_start(section_start) , char_section_end(section_end) , char_assign(assign) , char_comment(comment) , char_interpol(interpol) , char_interpol_start(interpol_start) , char_interpol_sep(interpol_sep) , char_interpol_end(interpol_end) {} Format() : Format('[', ']', '=', ';', '$', '{', ':', '}') {} const std::basic_string local_symbol(const std::basic_string& name) const { return char_interpol + (char_interpol_start + name + char_interpol_end); } const std::basic_string global_symbol(const std::basic_string& sec_name, const std::basic_string& name) const { return local_symbol(sec_name + char_interpol_sep + name); } }; template class Ini { public: using String = std::basic_string; using Section = std::map; using Sections = std::map; Sections sections; std::list errors; std::shared_ptr> format; static const int max_interpolation_depth = 10; Ini() : format(std::make_shared>()) {}; Ini(std::shared_ptr> fmt) : format(fmt) {}; void generate(std::basic_ostream& os) const { for (auto const & sec : sections) { os << format->char_section_start << sec.first << format->char_section_end << std::endl; for (auto const & val : sec.second) { os << val.first << format->char_assign << val.second << std::endl; } os << std::endl; } } void parse(std::basic_istream & is) { String line; String section; const std::locale loc{"C"}; while (std::getline(is, line)) { detail::ltrim(line, loc); detail::rtrim(line, loc); const auto length = line.length(); if (length > 0) { const auto pos = std::find_if(line.begin(), line.end(), [this](CharT ch) { return format->is_assign(ch); }); const auto & front = line.front(); if (format->is_comment(front)) { } else if (format->is_section_start(front)) { if (format->is_section_end(line.back())) section = line.substr(1, length - 2); else errors.push_back(line); } else if (pos != line.begin() && pos != line.end()) { String variable(line.begin(), pos); String value(pos + 1, line.end()); detail::rtrim(variable, loc); detail::ltrim(value, loc); auto & sec = sections[section]; if (sec.find(variable) == sec.end()) sec.insert(std::make_pair(variable, value)); else errors.push_back(line); } else { errors.push_back(line); } } } } void interpolate() { int global_iteration = 0; auto changed = false; // replace each "${variable}" by "${section:variable}" for (auto & sec : sections) replace_symbols(local_symbols(sec.first, sec.second), sec.second); // replace each "${section:variable}" by its value do { changed = false; const auto syms = global_symbols(); for (auto & sec : sections) changed |= replace_symbols(syms, sec.second); } while (changed && (max_interpolation_depth > global_iteration++)); } void default_section(const Section & sec) { for (auto & sec2 : sections) for (const auto & val : sec) sec2.second.insert(val); } void clear() { sections.clear(); errors.clear(); } private: using Symbols = std::list>; const Symbols local_symbols(const String & sec_name, const Section & sec) const { Symbols result; for (const auto & val : sec) result.push_back(std::make_pair(format->local_symbol(val.first), format->global_symbol(sec_name, val.first))); return result; } const Symbols global_symbols() const { Symbols result; for (const auto & sec : sections) for (const auto & val : sec.second) result.push_back( std::make_pair(format->global_symbol(sec.first, val.first), val.second)); return result; } bool replace_symbols(const Symbols & syms, Section & sec) const { auto changed = false; for (auto & sym : syms) for (auto & val : sec) changed |= detail::replace(val.second, sym.first, sym.second); return changed; } }; } // namespace inipp