X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/704483df2dab0f376b996e04d5c971e99151f439..458b15bd1de32319a96a909e2797ed2ca3a4aed1:/stglibs/conffiles.lib/conffiles.cpp?ds=sidebyside diff --git a/stglibs/conffiles.lib/conffiles.cpp b/stglibs/conffiles.lib/conffiles.cpp index ea51939a..302c3651 100644 --- a/stglibs/conffiles.lib/conffiles.cpp +++ b/stglibs/conffiles.lib/conffiles.cpp @@ -40,24 +40,55 @@ #include -#include "conffiles.h" -#include "stg/common.h" +#include "stg/conffiles.h" -using namespace std; +namespace +{ +//--------------------------------------------------------------------------- +std::string TrimL(std::string val) +{ +size_t pos = val.find_first_not_of(" \t"); +if (pos == std::string::npos) + { + val.erase(val.begin(), val.end()); + } +else + { + val.erase(0, pos); + } +return val; +} +//--------------------------------------------------------------------------- +std::string TrimR(std::string val) +{ +size_t pos = val.find_last_not_of(" \t"); +if (pos != std::string::npos) + { + val.erase(pos + 1); + } +return val; +} +//--------------------------------------------------------------------------- +std::string Trim(std::string val) +{ +return TrimR(TrimL(val)); +} +//--------------------------------------------------------------------------- +} // namespace anonymous //--------------------------------------------------------------------------- -bool StringCaseCmp(const string & str1, const string & str2) +bool StringCaseCmp(const std::string & str1, const std::string & str2) { return (strcasecmp(str1.c_str(), str2.c_str()) < 0); } //--------------------------------------------------------------------------- -CONFIGFILE::CONFIGFILE(const string & fn, bool nook) +CONFIGFILE::CONFIGFILE(const std::string & fn, bool nook) : param_val(StringCaseCmp), fileName(fn), error(0), changed(false) { -ifstream f(fileName.c_str()); +std::ifstream f(fileName.c_str()); if (!f) { @@ -66,25 +97,25 @@ if (!f) return; } -string line; +std::string line; while (getline(f, line)) { size_t pos = line.find('#'); - if (pos != string::npos) + if (pos != std::string::npos) line.resize(pos); - if (line.find_first_not_of(" \t\r") == string::npos) + if (line.find_first_not_of(" \t\r") == std::string::npos) continue; pos = line.find_first_of('='); - if (pos == string::npos) + if (pos == std::string::npos) { error = -1; return; } - string parameter = line.substr(0, pos); - string value = line.substr(pos + 1); + std::string parameter = Trim(line.substr(0, pos)); + std::string value = Trim(line.substr(pos + 1)); param_val[parameter] = value; } } @@ -94,7 +125,7 @@ CONFIGFILE::~CONFIGFILE() Flush(); } //--------------------------------------------------------------------------- -const string & CONFIGFILE::GetFileName() const +const std::string & CONFIGFILE::GetFileName() const { return fileName; } @@ -105,33 +136,13 @@ int e = error; error = 0; return e; } -/*//--------------------------------------------------------------------------- -int CONFIGFILE::ReadString(const string & param, char * str, int * maxLen, const char * defaultVal) const -{ -it = param_val.find(param); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ - -if (it != param_val.end()) - { - // þÔÏ-ÔÏ ÓÔÏÉÔ - strncpy(str, param_val[param].c_str(), *maxLen); - *maxLen = param_val[param].size(); - return 0; - } - -strncpy(str, defaultVal, *maxLen); -*maxLen = strlen(defaultVal); -return -1; -}*/ //--------------------------------------------------------------------------- -int CONFIGFILE::ReadString(const string & param, string * val, const string & defaultVal) const +int CONFIGFILE::ReadString(const std::string & param, std::string * val, const std::string & defaultVal) const { -const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ *val = it->second; return 0; } @@ -140,15 +151,15 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -void CONFIGFILE::WriteString(const string & param, const string &val) +void CONFIGFILE::WriteString(const std::string & param, const std::string &val) { param_val[param] = val; changed = true; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const +int CONFIGFILE::ReadTime(const std::string & param, time_t * val, time_t defaultVal) const { -const map::const_iterator it(param_val.find(param)); +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { @@ -166,16 +177,14 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadInt(const string & param, int * val, int defaultVal) const +int CONFIGFILE::ReadInt(const std::string & param, int * val, int defaultVal) const { -const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ char *res; - *val = strtol(it->second.c_str(), &res, 10); + *val = static_cast(strtol(it->second.c_str(), &res, 10)); if (*res != 0) { *val = defaultVal; //Error! @@ -188,16 +197,14 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const +int CONFIGFILE::ReadUInt(const std::string & param, unsigned int * val, unsigned int defaultVal) const { -const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ char *res; - *val = strtoul(it->second.c_str(), &res, 10); + *val = static_cast(strtoul(it->second.c_str(), &res, 10)); if (*res != 0) { *val = defaultVal; //Error! @@ -210,14 +217,12 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadLongInt(const string & param, long int * val, long int defaultVal) const +int CONFIGFILE::ReadLongInt(const std::string & param, long int * val, long int defaultVal) const { -const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ char *res; *val = strtol(it->second.c_str(), &res, 10); if (*res != 0) @@ -232,14 +237,12 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const +int CONFIGFILE::ReadULongInt(const std::string & param, unsigned long int * val, unsigned long int defaultVal) const { -const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ char *res; *val = strtoul(it->second.c_str(), &res, 10); if (*res != 0) @@ -254,14 +257,12 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const +int CONFIGFILE::ReadLongLongInt(const std::string & param, int64_t * val, int64_t defaultVal) const { -const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ char *res; *val = strtoll(it->second.c_str(), &res, 10); if (*res != 0) @@ -276,14 +277,12 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const +int CONFIGFILE::ReadULongLongInt(const std::string & param, uint64_t * val, uint64_t defaultVal) const { -const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ char *res; *val = strtoull(it->second.c_str(), &res, 10); if (*res != 0) @@ -298,14 +297,12 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadShortInt(const string & param, short int * val, short int defaultVal) const +int CONFIGFILE::ReadShortInt(const std::string & param, short int * val, short int defaultVal) const { -const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ char *res; *val = (short)strtol(it->second.c_str(), &res, 10); if (*res != 0) @@ -320,14 +317,12 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const +int CONFIGFILE::ReadUShortInt(const std::string & param, unsigned short int * val, unsigned short int defaultVal) const { -const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ char *res; *val = (short)strtoul(it->second.c_str(), &res, 10); if (*res != 0) @@ -342,22 +337,20 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -void CONFIGFILE::WriteInt(const string & param, int64_t val) +void CONFIGFILE::WriteInt(const std::string & param, int64_t val) { -string s; -x2str(val, s); -param_val[param] = s; +char buf[32]; +snprintf(buf, sizeof(buf), "%lld", static_cast(val)); +param_val[param] = buf; changed = true; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const +int CONFIGFILE::ReadDouble(const std::string & param, double * val, double defaultVal) const { -const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ char *res; *val = strtod(it->second.c_str(), &res); if (*res != 0) @@ -372,7 +365,7 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -void CONFIGFILE::WriteDouble(const string & param, double val) +void CONFIGFILE::WriteDouble(const std::string & param, double val) { char s[30]; snprintf(s, 30, "%f", val); @@ -382,14 +375,14 @@ changed = true; //--------------------------------------------------------------------------- int CONFIGFILE::Flush(const std::string & path) const { -ofstream f(path.c_str()); +std::ofstream f(path.c_str()); if (!f.is_open()) { error = EIO; return EIO; } -map::const_iterator it = param_val.begin(); +std::map::const_iterator it = param_val.begin(); while (it != param_val.end()) { f << it->first << "=" << it->second << "\n"; @@ -405,8 +398,8 @@ int CONFIGFILE::Flush() const if (!changed) return 0; -std::string pid; -x2str(getpid(), pid); +char pid[6]; +snprintf(pid, sizeof(pid), "%d", getpid()); if (Flush(fileName + "." + pid)) return -1;