1 |
#include <string> #include "inireader.h" #include "anystruct.h" |
2 |
inidata::inidata(){ } |
3 |
inidata::inidata(std::string const &szName){ (*this) = readINI(szName); } |
4 |
bool inidata::read(std::string const &szName){ (*this) = readINI(szName); return 1; } |
5 |
std::string &remcom(std::string &sz){ std::string niga0; gstring niga1; if(!sz.size()) return sz; std::size_t ic0 = sz.find('#'), ic1 = sz.find(';'), iq = sz.find('"'); if(ic0 == std::string::npos and ic1 == std::string::npos) return sz; uint32_t ic = ic0 > ic1 ? ic1 : ic0; if(ic < iq) sz.resize(ic); return sz; } |
6 |
std::string remcom(std::string &&sz){ if(!sz.size()) return sz; std::size_t ic0 = sz.find('#'), ic1 = sz.find(';'), iq = sz.find('"'); if(ic0 == std::string::npos and ic1 == std::string::npos) return sz; uint32_t ic = ic0 > ic1 ? ic1 : ic0; if(ic < iq) sz.resize(ic); return sz; } |
7 |
inline bool is_section(std::string const &str){ return str.front() == '[' and str.back() == ']'; } |
8 |
inline std::string &get_section(std::string &str){ return stringex::trimit(str.erase(0, 1).erase(str.size() - 1, 1)); } |
9 |
inline std::pair<std::size_t, std::size_t> get_mas(std::string const &str){ return {str.find('['), str.find(']')}; } |
10 |
inidata readINI(std::string const &szName){ using namespace stringex; std::ifstream file(szName); if(!file.is_open()){ file.open(szName + ".ini"); if(!file.is_open()) return {}; } inidata data; |
11 |
std::string szcursec = ""; data.data.insert({szcursec, {}}); while(!file.eof()){ std::string buf; std::getline(file, buf); remcom(buf); trim(buf); while(!file.eof() and buf.back() == '\\') buf += ' ' + trim(readline(file)); if(!buf.size()) continue; if(is_section(buf)){ szcursec = get_section(buf); data.data.insert({szcursec, {}}); continue; } auto prI = get_mas(buf); std::string_view strvBuf = buf; std::size_t ieq = strvBuf.find('='); std::string val = buf.substr(ieq + 1); if(prI.second != std::string::npos and prI.first < prI.second){ std::string name = buf.substr(0, prI.first); std::size_t i = atoi(strvBuf.substr(prI.first, prI.second).data()); auto &rmas = data.data[szcursec].md; if(!rmas.count(name)) rmas.insert({name, {}}); if(rmas.size() < i) rmas[name].resize(i); rmas[name][i] = val; continue; } std::string name = buf.substr(0, ieq); auto &rd = data.data[szcursec].d; if(rd.count(name)) rd[name] = val; else rd.insert({name, val}); } |
12 |
return data; } |
Комментарии