4 Copyright (c) 2017-2020 Matthias C. M. Troffaes
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
41 // trim functions based on http://stackoverflow.com/a/217605
43 template <class CharT>
44 inline void ltrim(std::basic_string<CharT> & s) {
46 std::find_if(s.begin(), s.end(),
47 [](int ch) { return !std::isspace(ch); }));
50 template <class CharT>
51 inline void rtrim(std::basic_string<CharT> & s) {
52 s.erase(std::find_if(s.rbegin(), s.rend(),
53 [](int ch) { return !std::isspace(ch); }).base(),
57 // string replacement function based on http://stackoverflow.com/a/3418285
59 template <class CharT>
60 inline bool replace(std::basic_string<CharT> & str, const std::basic_string<CharT> & from, const std::basic_string<CharT> & to) {
63 while ((start_pos = str.find(from, start_pos)) != std::basic_string<CharT>::npos) {
64 str.replace(start_pos, from.length(), to);
65 start_pos += to.length();
73 template <typename CharT, typename T>
74 inline bool extract(const std::basic_string<CharT> & value, T & dst) {
76 std::basic_istringstream<CharT> is{ value };
78 if ((is >> std::boolalpha >> result) && !(is >> c)) {
87 template <typename CharT>
88 inline bool extract(const std::basic_string<CharT> & value, std::basic_string<CharT> & dst) {
97 typedef std::basic_string<CharT> String;
98 typedef std::map<String, String> Section;
99 typedef std::map<String, Section> Sections;
102 std::list<String> errors;
104 static const CharT char_section_start = (CharT)'[';
105 static const CharT char_section_end = (CharT)']';
106 static const CharT char_assign = (CharT)'=';
107 static const CharT char_comment = (CharT)';';
108 static const CharT char_interpol = (CharT)'$';
109 static const CharT char_interpol_start = (CharT)'{';
110 static const CharT char_interpol_sep = (CharT)':';
111 static const CharT char_interpol_end = (CharT)'}';
113 static const int max_interpolation_depth = 10;
115 void generate(std::basic_ostream<CharT> & os) const {
116 for (auto const & sec : sections) {
117 os << char_section_start << sec.first << char_section_end << std::endl;
118 for (auto const & val : sec.second) {
119 os << val.first << char_assign << val.second << std::endl;
125 void parse(std::basic_istream<CharT> & is) {
128 while (std::getline(is, line)) {
131 const auto length = line.length();
133 const auto pos = line.find_first_of(char_assign);
134 const auto & front = line.front();
135 if (front == char_comment) {
138 else if (front == char_section_start) {
139 if (line.back() == char_section_end)
140 section = line.substr(1, length - 2);
142 errors.push_back(line);
144 else if (pos != 0 && pos != String::npos) {
145 String variable(line.substr(0, pos));
146 String value(line.substr(pos + 1, length));
147 detail::rtrim(variable);
148 detail::ltrim(value);
149 auto & sec = sections[section];
150 if (sec.find(variable) == sec.end())
151 sec.insert(std::make_pair(variable, value));
153 errors.push_back(line);
156 errors.push_back(line);
163 int global_iteration = 0;
164 auto changed = false;
165 // replace each "${variable}" by "${section:variable}"
166 for (auto & sec : sections)
167 replace_symbols(local_symbols(sec.first, sec.second), sec.second);
168 // replace each "${section:variable}" by its value
171 const auto syms = global_symbols();
172 for (auto & sec : sections)
173 changed |= replace_symbols(syms, sec.second);
174 } while (changed && (max_interpolation_depth > global_iteration++));
177 void default_section(const Section & sec) {
178 for (auto & sec2 : sections)
179 for (const auto & val : sec)
180 sec2.second.insert(val);
189 typedef std::list<std::pair<String, String> > Symbols;
191 String local_symbol(const String & name) const {
192 return char_interpol + (char_interpol_start + name + char_interpol_end);
195 String global_symbol(const String & sec_name, const String & name) const {
196 return local_symbol(sec_name + char_interpol_sep + name);
199 String local_symbols(const String & sec_name, const Section & sec) const {
201 for (const auto & val : sec)
202 result.push_back(std::make_pair(local_symbol(val.first), global_symbol(sec_name, val.first)));
206 String global_symbols() const {
208 for (const auto & sec : sections)
209 for (const auto & val : sec.second)
211 std::make_pair(global_symbol(sec.first, val.first), val.second));
215 bool replace_symbols(const Symbols & syms, Section & sec) const {
216 auto changed = false;
217 for (auto & sym : syms)
218 for (auto & val : sec)
219 changed |= detail::replace(val.second, sym.first, sym.second);