6 #ifndef UTILS_INI_PARSER_H 7 #define UTILS_INI_PARSER_H 9 #include <Utils/ExtSTD.h> 10 #include <Utils/ExtGLM.h> 11 #include <Utils/Dictionary.h> 12 #include <Utils/Ulog.h> 21 IniParser() =
default;
23 IniParser(
const std::string &filename)
29 std::string PopErrorLog()
31 auto log = error_log_;
37 const std::unordered_set<std::string> &GetUnreadableRecords()
const 39 return unreadable_records_;
43 bool LoadFile(
const std::string &file_name)
45 std::ifstream ifs(file_name);
49 ulog::Fail(
"Config file \"" + file_name +
"\" was not found.");
55 while (std::getline(ifs, line))
58 if (line.empty() || line[0] ==
'#')
62 if (line.front() ==
'[' && line.back() ==
']')
64 section = line.substr(1, line.length() - 2);
67 Record record = GetRecord(line);
68 if (!record.first.empty())
72 unreadable_records_.emplace(record.first +
" : " + record.second);
78 record.first = section +
"/" + record.first;
80 dictionary_.Set(record.first.c_str(), record.second.c_str());
95 bool Exists(
const std::string &key)
const 97 return dictionary_.IsAvailable(key.c_str());
101 std::string GetStr(
const std::string &key,
const std::string &default_val =
"")
const 105 LogStandardError(key,
"'" + default_val +
"'");
108 return dictionary_.Get(key.c_str());
112 bool GetBool(
const std::string &key,
const bool default_val =
false)
const 119 const std::string val = dictionary_.Get(key.c_str());
124 size_t GetSizeT(
const std::string &key,
const size_t default_val = 0)
const 126 return static_cast<size_t>(GetInt(key, static_cast<int>(default_val)));
130 template<
typename Type>
131 Type GetUint(
const std::string &key,
const Type default_val = 0)
const 133 return static_cast<Type>(GetInt(key, static_cast<int>(default_val)));
137 int GetInt(
const std::string &key,
const int default_val = 0)
const 141 LogStandardError(key, std::to_string(default_val));
144 return std::stoi(dictionary_.Get(key.c_str()));
148 float GetFloat(
const std::string &key,
const float default_val = 0.0f)
const 152 LogStandardError(key, std::to_string(default_val));
155 return std::stof(dictionary_.Get(key.c_str()));
159 double GetDouble(
const std::string &key,
const double default_val = 0.0)
const 163 LogStandardError(key, std::to_string(default_val));
166 return std::stod(dictionary_.Get(key.c_str()));
170 template <glm::length_t N,
typename Type>
171 glm::vec<N, Type, glm::highp> GetVec(
const std::string &key,
const glm::vec<N, Type, glm::highp> default_val = glm::vec<N, Type, glm::highp>(0))
178 auto elements =
std_ext::Parse(dictionary_.Get(key.c_str()),
" ");
179 if (elements.size() != N)
181 auto el0 = std::to_string(N);
182 auto el1 = std::to_string(elements.size());
183 error_log_ +=
"[" + key +
"] key contains invalid value.";
184 error_log_ +=
"Expected vector of " + el0 +
" elements, not " + el1 +
".";
188 glm::vec<N, Type, glm::highp> result;
189 for (glm::length_t i = 0; i < N; ++i)
191 result[i] =
Type(std::stod(elements[i]));
197 std::vector<float> GetFloatArray(
const std::string &key,
const std::vector<float> &default_val = {})
const 199 const auto result =
std_ext::Transformed(GetArray(key), [
this, key](std::string number_as_string)
201 return std::stof(number_as_string);
204 return result.empty() ? default_val : result;
208 std::vector<int> GetIntArray(
const std::string &key,
const std::vector<int> &default_val = {})
const 210 const auto result =
std_ext::Transformed(GetArray(key), [
this, key](std::string number_as_string)
212 return std::stoi(number_as_string);
215 return result.empty() ? default_val : result;
219 std::vector<bool> GetBoolArray(
const std::string &key,
const std::vector<bool> &default_val = {})
const 221 const auto result =
std_ext::Transformed(GetArray(key), [
this, key](std::string number_as_string)
226 return result.empty() ? default_val : result;
230 std::vector<std::string> GetArray(
const std::string &key,
const std::vector<std::string> &default_val = {})
const 232 std::string next_arr_key = key +
"[0]";
233 std::vector<std::string> result;
234 if (!Exists(next_arr_key))
236 LogStandardError(next_arr_key,
"empty array");
240 while (Exists(next_arr_key))
242 result.emplace_back(dictionary_.Get(next_arr_key.c_str()));
243 next_arr_key = key +
"[" + std::to_string(next_i) +
"]";
250 std::map<std::string, std::string> replacement_map_;
251 const bool is_replacement_map_enabled_ =
false;
253 mutable std::string error_log_;
254 mutable std::unordered_set<std::string> unreadable_records_;
256 void LogStandardError(
const std::string &key,
const std::string &val)
const 258 error_log_ +=
"[" + key +
"] key not found, using " + val +
".\n\r";
261 using Record = std::pair<std::string, std::string>;
263 virtual Record GetRecord(
const std::string &line)
266 size_t delim_pos = line.find_first_of(
"=");
267 if (delim_pos == std::string::npos)
269 record = Record(line,
"0");
273 record.first = line.substr(0, delim_pos);
274 record.second = line.substr(delim_pos + 1, line.size());
279 if (record.second.size() >= 2 && record.second.front() ==
'"' && record.second.back() ==
'"')
281 record.second = record.second.substr(1, record.second.length() - 2);
284 if (is_replacement_map_enabled_)
std::string Lowercase(const std::string &text)
Converts text into all lowercase.
Definition: ExtSTD.h:311
std::string to_string(const glm::vec< N, genType, glm::highp > &vec, const std::string &delim=", ")
Converts the input vector into a string with predefined delimiter.
Definition: ExtGLM.h:87
std::string & Trim(std::string &s, const std::string &t=WHITESPACES)
Trims from both ends of string (left & right).
Definition: ExtSTD.h:719
bool IsASCII(const std::string &s)
Check std::string for non-ASCII characters.
Definition: ExtSTD.h:1011
Type
Enum class that specifies (geometry) file types that we recognize.
Definition: FileTypes.h:33
std::vector< std::string > Parse(const std::string &text, const std::string &separators=WHITESPACES)
Parses the input text to sub strings by a given list of separators.
Definition: ExtSTD.h:735
auto Transformed(const C &container, F Function)
Simplified wrapper for std::transform.
Definition: ExtSTD.h:831
Enables mapping string keys to string words efficiently.
Definition: Dictionary.h:19
General namespace for utilities.
Definition: BulletConversions.h:9
std::string BoolToString(const bool value)
Converts bool to string ("true" or "false").
Definition: ExtSTD.h:160
std::string Replace(const std::string &text, const std::string &key_to_search, const std::string &replace_text)
Replaces all occurrences of key_to_search with replace_text in specified text.
Definition: ExtSTD.h:117