annotate dep/mini/ini.h @ 227:c4f03f83b252

library: do explicit conversion from fs::path to std::string this fixes compiler errors on Windows.
author Paper <mrpapersonic@gmail.com>
date Mon, 08 Jan 2024 21:30:18 -0500
parents c537996cf67b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
101
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
1 /*
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
2 * The MIT License (MIT)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3 * Copyright (c) 2018 Danijel Durakovic
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4 *
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6 * this software and associated documentation files (the "Software"), to deal in
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 * the Software without restriction, including without limitation the rights to
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9 * of the Software, and to permit persons to whom the Software is furnished to do
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 * so, subject to the following conditions:
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11 *
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12 * The above copyright notice and this permission notice shall be included in all
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13 * copies or substantial portions of the Software.
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14 *
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21 *
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22 */
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24 ///////////////////////////////////////////////////////////////////////////////
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25 //
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
26 // /mINI/ v0.9.14
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27 // An INI file reader and writer for the modern age.
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
28 //
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29 ///////////////////////////////////////////////////////////////////////////////
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30 //
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31 // A tiny utility library for manipulating INI files with a straightforward
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32 // API and a minimal footprint. It conforms to the (somewhat) standard INI
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
33 // format - sections and keys are case insensitive and all leading and
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
34 // trailing whitespace is ignored. Comments are lines that begin with a
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35 // semicolon. Trailing comments are allowed on section lines.
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 //
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37 // Files are read on demand, upon which data is kept in memory and the file
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38 // is closed. This utility supports lazy writing, which only writes changes
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 // and updates to a file and preserves custom formatting and comments. A lazy
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40 // write invoked by a write() call will read the output file, find what
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41 // changes have been made and update the file accordingly. If you only need to
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 // generate files, use generate() instead. Section and key order is preserved
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 // on read, write and insert.
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44 //
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45 ///////////////////////////////////////////////////////////////////////////////
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46 //
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 // /* BASIC USAGE EXAMPLE: */
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48 //
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49 // /* read from file */
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50 // mINI::INIFile file("myfile.ini");
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51 // mINI::INIStructure ini;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 // file.read(ini);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53 //
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54 // /* read value; gets a reference to actual value in the structure.
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 // if key or section don't exist, a new empty value will be created */
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56 // std::string& value = ini["section"]["key"];
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57 //
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58 // /* read value safely; gets a copy of value in the structure.
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
59 // does not alter the structure */
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 // std::string value = ini.get("section").get("key");
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61 //
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62 // /* set or update values */
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
63 // ini["section"]["key"] = "value";
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
64 //
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
65 // /* set multiple values */
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
66 // ini["section2"].set({
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
67 // {"key1", "value1"},
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
68 // {"key2", "value2"}
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
69 // });
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
70 //
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
71 // /* write updates back to file, preserving comments and formatting */
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
72 // file.write(ini);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
73 //
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
74 // /* or generate a file (overwrites the original) */
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
75 // file.generate(ini);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
76 //
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
77 ///////////////////////////////////////////////////////////////////////////////
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
78 //
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
79 // Long live the INI file!!!
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
80 //
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
81 ///////////////////////////////////////////////////////////////////////////////
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
82
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
83 #ifndef MINI_INI_H_
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
84 #define MINI_INI_H_
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
85
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
86 #include <string>
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
87 #include <sstream>
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
88 #include <algorithm>
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
89 #include <utility>
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
90 #include <unordered_map>
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
91 #include <vector>
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
92 #include <memory>
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
93 #include <fstream>
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
94 #include <sys/stat.h>
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
95 #include <cctype>
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
96
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
97 namespace mINI
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
98 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
99 namespace INIStringUtil
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
100 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
101 const char* const whitespaceDelimiters = " \t\n\r\f\v";
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
102 inline void trim(std::string& str)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
103 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
104 str.erase(str.find_last_not_of(whitespaceDelimiters) + 1);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
105 str.erase(0, str.find_first_not_of(whitespaceDelimiters));
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
106 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
107 #ifndef MINI_CASE_SENSITIVE
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
108 inline void toLower(std::string& str)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
109 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
110 std::transform(str.begin(), str.end(), str.begin(), [](const char c) {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
111 return static_cast<char>(std::tolower(c));
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
112 });
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
113 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
114 #endif
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
115 inline void replace(std::string& str, std::string const& a, std::string const& b)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
116 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
117 if (!a.empty())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
118 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
119 std::size_t pos = 0;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
120 while ((pos = str.find(a, pos)) != std::string::npos)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
121 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
122 str.replace(pos, a.size(), b);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
123 pos += b.size();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
124 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
125 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
126 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
127 #ifdef _WIN32
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
128 const char* const endl = "\r\n";
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
129 #else
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
130 const char* const endl = "\n";
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
131 #endif
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
132 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
133
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
134 template<typename T>
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
135 class INIMap
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
136 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
137 private:
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
138 using T_DataIndexMap = std::unordered_map<std::string, std::size_t>;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
139 using T_DataItem = std::pair<std::string, T>;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
140 using T_DataContainer = std::vector<T_DataItem>;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
141 using T_MultiArgs = typename std::vector<std::pair<std::string, T>>;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
142
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
143 T_DataIndexMap dataIndexMap;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
144 T_DataContainer data;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
145
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
146 inline std::size_t setEmpty(std::string& key)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
147 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
148 std::size_t index = data.size();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
149 dataIndexMap[key] = index;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
150 data.emplace_back(key, T());
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
151 return index;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
152 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
153
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
154 public:
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
155 using const_iterator = typename T_DataContainer::const_iterator;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
156
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
157 INIMap() { }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
158
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
159 INIMap(INIMap const& other)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
160 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
161 std::size_t data_size = other.data.size();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
162 for (std::size_t i = 0; i < data_size; ++i)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
163 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
164 auto const& key = other.data[i].first;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
165 auto const& obj = other.data[i].second;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
166 data.emplace_back(key, obj);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
167 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
168 dataIndexMap = T_DataIndexMap(other.dataIndexMap);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
169 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
170
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
171 T& operator[](std::string key)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
172 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
173 INIStringUtil::trim(key);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
174 #ifndef MINI_CASE_SENSITIVE
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
175 INIStringUtil::toLower(key);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
176 #endif
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
177 auto it = dataIndexMap.find(key);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
178 bool hasIt = (it != dataIndexMap.end());
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
179 std::size_t index = (hasIt) ? it->second : setEmpty(key);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
180 return data[index].second;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
181 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
182 T get(std::string key) const
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
183 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
184 INIStringUtil::trim(key);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
185 #ifndef MINI_CASE_SENSITIVE
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
186 INIStringUtil::toLower(key);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
187 #endif
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
188 auto it = dataIndexMap.find(key);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
189 if (it == dataIndexMap.end())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
190 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
191 return T();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
192 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
193 return T(data[it->second].second);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
194 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
195 bool has(std::string key) const
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
196 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
197 INIStringUtil::trim(key);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
198 #ifndef MINI_CASE_SENSITIVE
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
199 INIStringUtil::toLower(key);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
200 #endif
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
201 return (dataIndexMap.count(key) == 1);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
202 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
203 void set(std::string key, T obj)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
204 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
205 INIStringUtil::trim(key);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
206 #ifndef MINI_CASE_SENSITIVE
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
207 INIStringUtil::toLower(key);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
208 #endif
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
209 auto it = dataIndexMap.find(key);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
210 if (it != dataIndexMap.end())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
211 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
212 data[it->second].second = obj;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
213 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
214 else
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
215 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
216 dataIndexMap[key] = data.size();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
217 data.emplace_back(key, obj);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
218 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
219 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
220 void set(T_MultiArgs const& multiArgs)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
221 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
222 for (auto const& it : multiArgs)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
223 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
224 auto const& key = it.first;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
225 auto const& obj = it.second;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
226 set(key, obj);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
227 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
228 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
229 bool remove(std::string key)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
230 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
231 INIStringUtil::trim(key);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
232 #ifndef MINI_CASE_SENSITIVE
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
233 INIStringUtil::toLower(key);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
234 #endif
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
235 auto it = dataIndexMap.find(key);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
236 if (it != dataIndexMap.end())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
237 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
238 std::size_t index = it->second;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
239 data.erase(data.begin() + index);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
240 dataIndexMap.erase(it);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
241 for (auto& it2 : dataIndexMap)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
242 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
243 auto& vi = it2.second;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
244 if (vi > index)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
245 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
246 vi--;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
247 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
248 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
249 return true;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
250 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
251 return false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
252 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
253 void clear()
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
254 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
255 data.clear();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
256 dataIndexMap.clear();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
257 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
258 std::size_t size() const
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
259 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
260 return data.size();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
261 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
262 const_iterator begin() const { return data.begin(); }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
263 const_iterator end() const { return data.end(); }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
264 };
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
265
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
266 using INIStructure = INIMap<INIMap<std::string>>;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
267
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
268 namespace INIParser
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
269 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
270 using T_ParseValues = std::pair<std::string, std::string>;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
271
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
272 enum class PDataType : char
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
273 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
274 PDATA_NONE,
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
275 PDATA_COMMENT,
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
276 PDATA_SECTION,
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
277 PDATA_KEYVALUE,
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
278 PDATA_UNKNOWN
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
279 };
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
280
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
281 inline PDataType parseLine(std::string line, T_ParseValues& parseData)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
282 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
283 parseData.first.clear();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
284 parseData.second.clear();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
285 INIStringUtil::trim(line);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
286 if (line.empty())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
287 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
288 return PDataType::PDATA_NONE;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
289 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
290 char firstCharacter = line[0];
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
291 if (firstCharacter == ';')
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
292 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
293 return PDataType::PDATA_COMMENT;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
294 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
295 if (firstCharacter == '[')
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
296 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
297 auto commentAt = line.find_first_of(';');
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
298 if (commentAt != std::string::npos)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
299 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
300 line = line.substr(0, commentAt);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
301 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
302 auto closingBracketAt = line.find_last_of(']');
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
303 if (closingBracketAt != std::string::npos)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
304 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
305 auto section = line.substr(1, closingBracketAt - 1);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
306 INIStringUtil::trim(section);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
307 parseData.first = section;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
308 return PDataType::PDATA_SECTION;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
309 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
310 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
311 auto lineNorm = line;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
312 INIStringUtil::replace(lineNorm, "\\=", " ");
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
313 auto equalsAt = lineNorm.find_first_of('=');
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
314 if (equalsAt != std::string::npos)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
315 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
316 auto key = line.substr(0, equalsAt);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
317 INIStringUtil::trim(key);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
318 INIStringUtil::replace(key, "\\=", "=");
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
319 auto value = line.substr(equalsAt + 1);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
320 INIStringUtil::trim(value);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
321 parseData.first = key;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
322 parseData.second = value;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
323 return PDataType::PDATA_KEYVALUE;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
324 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
325 return PDataType::PDATA_UNKNOWN;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
326 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
327 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
328
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
329 class INIReader
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
330 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
331 public:
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
332 using T_LineData = std::vector<std::string>;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
333 using T_LineDataPtr = std::shared_ptr<T_LineData>;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
334
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
335 bool isBOM = false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
336
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
337 private:
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
338 std::ifstream fileReadStream;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
339 T_LineDataPtr lineData;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
340
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
341 T_LineData readFile()
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
342 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
343 fileReadStream.seekg(0, std::ios::end);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
344 const std::size_t fileSize = static_cast<std::size_t>(fileReadStream.tellg());
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
345 fileReadStream.seekg(0, std::ios::beg);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
346 if (fileSize >= 3) {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
347 const char header[3] = {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
348 static_cast<char>(fileReadStream.get()),
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
349 static_cast<char>(fileReadStream.get()),
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
350 static_cast<char>(fileReadStream.get())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
351 };
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
352 isBOM = (
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
353 header[0] == static_cast<char>(0xEF) &&
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
354 header[1] == static_cast<char>(0xBB) &&
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
355 header[2] == static_cast<char>(0xBF)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
356 );
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
357 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
358 else {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
359 isBOM = false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
360 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
361 std::string fileContents;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
362 fileContents.resize(fileSize);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
363 fileReadStream.seekg(isBOM ? 3 : 0, std::ios::beg);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
364 fileReadStream.read(&fileContents[0], fileSize);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
365 fileReadStream.close();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
366 T_LineData output;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
367 if (fileSize == 0)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
368 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
369 return output;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
370 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
371 std::string buffer;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
372 buffer.reserve(50);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
373 for (std::size_t i = 0; i < fileSize; ++i)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
374 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
375 char& c = fileContents[i];
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
376 if (c == '\n')
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
377 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
378 output.emplace_back(buffer);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
379 buffer.clear();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
380 continue;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
381 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
382 if (c != '\0' && c != '\r')
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
383 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
384 buffer += c;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
385 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
386 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
387 output.emplace_back(buffer);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
388 return output;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
389 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
390
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
391 public:
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
392 INIReader(std::string const& filename, bool keepLineData = false)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
393 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
394 fileReadStream.open(filename, std::ios::in | std::ios::binary);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
395 if (keepLineData)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
396 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
397 lineData = std::make_shared<T_LineData>();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
398 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
399 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
400 ~INIReader() { }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
401
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
402 bool operator>>(INIStructure& data)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
403 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
404 if (!fileReadStream.is_open())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
405 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
406 return false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
407 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
408 T_LineData fileLines = readFile();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
409 std::string section;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
410 bool inSection = false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
411 INIParser::T_ParseValues parseData;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
412 for (auto const& line : fileLines)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
413 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
414 auto parseResult = INIParser::parseLine(line, parseData);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
415 if (parseResult == INIParser::PDataType::PDATA_SECTION)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
416 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
417 inSection = true;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
418 data[section = parseData.first];
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
419 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
420 else if (inSection && parseResult == INIParser::PDataType::PDATA_KEYVALUE)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
421 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
422 auto const& key = parseData.first;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
423 auto const& value = parseData.second;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
424 data[section][key] = value;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
425 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
426 if (lineData && parseResult != INIParser::PDataType::PDATA_UNKNOWN)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
427 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
428 if (parseResult == INIParser::PDataType::PDATA_KEYVALUE && !inSection)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
429 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
430 continue;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
431 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
432 lineData->emplace_back(line);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
433 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
434 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
435 return true;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
436 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
437 T_LineDataPtr getLines()
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
438 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
439 return lineData;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
440 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
441 };
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
442
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
443 class INIGenerator
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
444 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
445 private:
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
446 std::ofstream fileWriteStream;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
447
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
448 public:
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
449 bool prettyPrint = false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
450
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
451 INIGenerator(std::string const& filename)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
452 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
453 fileWriteStream.open(filename, std::ios::out | std::ios::binary);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
454 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
455 ~INIGenerator() { }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
456
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
457 bool operator<<(INIStructure const& data)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
458 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
459 if (!fileWriteStream.is_open())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
460 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
461 return false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
462 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
463 if (!data.size())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
464 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
465 return true;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
466 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
467 auto it = data.begin();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
468 for (;;)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
469 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
470 auto const& section = it->first;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
471 auto const& collection = it->second;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
472 fileWriteStream
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
473 << "["
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
474 << section
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
475 << "]";
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
476 if (collection.size())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
477 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
478 fileWriteStream << INIStringUtil::endl;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
479 auto it2 = collection.begin();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
480 for (;;)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
481 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
482 auto key = it2->first;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
483 INIStringUtil::replace(key, "=", "\\=");
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
484 auto value = it2->second;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
485 INIStringUtil::trim(value);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
486 fileWriteStream
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
487 << key
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
488 << ((prettyPrint) ? " = " : "=")
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
489 << value;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
490 if (++it2 == collection.end())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
491 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
492 break;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
493 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
494 fileWriteStream << INIStringUtil::endl;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
495 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
496 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
497 if (++it == data.end())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
498 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
499 break;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
500 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
501 fileWriteStream << INIStringUtil::endl;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
502 if (prettyPrint)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
503 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
504 fileWriteStream << INIStringUtil::endl;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
505 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
506 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
507 return true;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
508 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
509 };
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
510
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
511 class INIWriter
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
512 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
513 private:
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
514 using T_LineData = std::vector<std::string>;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
515 using T_LineDataPtr = std::shared_ptr<T_LineData>;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
516
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
517 std::string filename;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
518
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
519 T_LineData getLazyOutput(T_LineDataPtr const& lineData, INIStructure& data, INIStructure& original)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
520 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
521 T_LineData output;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
522 INIParser::T_ParseValues parseData;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
523 std::string sectionCurrent;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
524 bool parsingSection = false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
525 bool continueToNextSection = false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
526 bool discardNextEmpty = false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
527 bool writeNewKeys = false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
528 std::size_t lastKeyLine = 0;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
529 for (auto line = lineData->begin(); line != lineData->end(); ++line)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
530 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
531 if (!writeNewKeys)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
532 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
533 auto parseResult = INIParser::parseLine(*line, parseData);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
534 if (parseResult == INIParser::PDataType::PDATA_SECTION)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
535 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
536 if (parsingSection)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
537 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
538 writeNewKeys = true;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
539 parsingSection = false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
540 --line;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
541 continue;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
542 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
543 sectionCurrent = parseData.first;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
544 if (data.has(sectionCurrent))
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
545 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
546 parsingSection = true;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
547 continueToNextSection = false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
548 discardNextEmpty = false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
549 output.emplace_back(*line);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
550 lastKeyLine = output.size();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
551 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
552 else
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
553 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
554 continueToNextSection = true;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
555 discardNextEmpty = true;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
556 continue;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
557 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
558 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
559 else if (parseResult == INIParser::PDataType::PDATA_KEYVALUE)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
560 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
561 if (continueToNextSection)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
562 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
563 continue;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
564 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
565 if (data.has(sectionCurrent))
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
566 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
567 auto& collection = data[sectionCurrent];
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
568 auto const& key = parseData.first;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
569 auto const& value = parseData.second;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
570 if (collection.has(key))
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
571 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
572 auto outputValue = collection[key];
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
573 if (value == outputValue)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
574 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
575 output.emplace_back(*line);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
576 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
577 else
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
578 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
579 INIStringUtil::trim(outputValue);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
580 auto lineNorm = *line;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
581 INIStringUtil::replace(lineNorm, "\\=", " ");
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
582 auto equalsAt = lineNorm.find_first_of('=');
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
583 auto valueAt = lineNorm.find_first_not_of(
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
584 INIStringUtil::whitespaceDelimiters,
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
585 equalsAt + 1
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
586 );
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
587 std::string outputLine = line->substr(0, valueAt);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
588 if (prettyPrint && equalsAt + 1 == valueAt)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
589 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
590 outputLine += " ";
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
591 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
592 outputLine += outputValue;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
593 output.emplace_back(outputLine);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
594 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
595 lastKeyLine = output.size();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
596 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
597 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
598 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
599 else
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
600 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
601 if (discardNextEmpty && line->empty())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
602 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
603 discardNextEmpty = false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
604 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
605 else if (parseResult != INIParser::PDataType::PDATA_UNKNOWN)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
606 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
607 output.emplace_back(*line);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
608 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
609 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
610 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
611 if (writeNewKeys || std::next(line) == lineData->end())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
612 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
613 T_LineData linesToAdd;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
614 if (data.has(sectionCurrent) && original.has(sectionCurrent))
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
615 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
616 auto const& collection = data[sectionCurrent];
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
617 auto const& collectionOriginal = original[sectionCurrent];
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
618 for (auto const& it : collection)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
619 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
620 auto key = it.first;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
621 if (collectionOriginal.has(key))
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
622 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
623 continue;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
624 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
625 auto value = it.second;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
626 INIStringUtil::replace(key, "=", "\\=");
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
627 INIStringUtil::trim(value);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
628 linesToAdd.emplace_back(
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
629 key + ((prettyPrint) ? " = " : "=") + value
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
630 );
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
631 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
632 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
633 if (!linesToAdd.empty())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
634 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
635 output.insert(
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
636 output.begin() + lastKeyLine,
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
637 linesToAdd.begin(),
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
638 linesToAdd.end()
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
639 );
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
640 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
641 if (writeNewKeys)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
642 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
643 writeNewKeys = false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
644 --line;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
645 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
646 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
647 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
648 for (auto const& it : data)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
649 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
650 auto const& section = it.first;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
651 if (original.has(section))
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
652 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
653 continue;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
654 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
655 if (prettyPrint && output.size() > 0 && !output.back().empty())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
656 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
657 output.emplace_back();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
658 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
659 output.emplace_back("[" + section + "]");
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
660 auto const& collection = it.second;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
661 for (auto const& it2 : collection)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
662 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
663 auto key = it2.first;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
664 auto value = it2.second;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
665 INIStringUtil::replace(key, "=", "\\=");
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
666 INIStringUtil::trim(value);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
667 output.emplace_back(
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
668 key + ((prettyPrint) ? " = " : "=") + value
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
669 );
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
670 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
671 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
672 return output;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
673 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
674
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
675 public:
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
676 bool prettyPrint = false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
677
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
678 INIWriter(std::string const& filename)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
679 : filename(filename)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
680 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
681 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
682 ~INIWriter() { }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
683
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
684 bool operator<<(INIStructure& data)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
685 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
686 struct stat buf;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
687 bool fileExists = (stat(filename.c_str(), &buf) == 0);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
688 if (!fileExists)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
689 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
690 INIGenerator generator(filename);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
691 generator.prettyPrint = prettyPrint;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
692 return generator << data;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
693 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
694 INIStructure originalData;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
695 T_LineDataPtr lineData;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
696 bool readSuccess = false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
697 bool fileIsBOM = false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
698 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
699 INIReader reader(filename, true);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
700 if ((readSuccess = reader >> originalData))
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
701 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
702 lineData = reader.getLines();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
703 fileIsBOM = reader.isBOM;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
704 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
705 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
706 if (!readSuccess)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
707 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
708 return false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
709 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
710 T_LineData output = getLazyOutput(lineData, data, originalData);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
711 std::ofstream fileWriteStream(filename, std::ios::out | std::ios::binary);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
712 if (fileWriteStream.is_open())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
713 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
714 if (fileIsBOM) {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
715 const char utf8_BOM[3] = {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
716 static_cast<char>(0xEF),
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
717 static_cast<char>(0xBB),
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
718 static_cast<char>(0xBF)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
719 };
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
720 fileWriteStream.write(utf8_BOM, 3);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
721 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
722 if (output.size())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
723 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
724 auto line = output.begin();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
725 for (;;)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
726 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
727 fileWriteStream << *line;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
728 if (++line == output.end())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
729 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
730 break;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
731 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
732 fileWriteStream << INIStringUtil::endl;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
733 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
734 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
735 return true;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
736 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
737 return false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
738 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
739 };
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
740
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
741 class INIFile
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
742 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
743 private:
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
744 std::string filename;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
745
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
746 public:
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
747 INIFile(std::string const& filename)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
748 : filename(filename)
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
749 { }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
750
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
751 ~INIFile() { }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
752
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
753 bool read(INIStructure& data) const
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
754 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
755 if (data.size())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
756 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
757 data.clear();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
758 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
759 if (filename.empty())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
760 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
761 return false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
762 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
763 INIReader reader(filename);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
764 return reader >> data;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
765 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
766 bool generate(INIStructure const& data, bool pretty = false) const
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
767 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
768 if (filename.empty())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
769 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
770 return false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
771 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
772 INIGenerator generator(filename);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
773 generator.prettyPrint = pretty;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
774 return generator << data;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
775 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
776 bool write(INIStructure& data, bool pretty = false) const
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
777 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
778 if (filename.empty())
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
779 {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
780 return false;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
781 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
782 INIWriter writer(filename);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
783 writer.prettyPrint = pretty;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
784 return writer << data;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
785 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
786 };
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
787 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
788
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
789 #endif // MINI_INI_H_