X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/e7c6f85eecb6863620be677106173e1824c9a578..e337600f4c621ccd847d2e852ee5e2b6a0cd8dd6:/stglibs/conffiles.lib/conffiles.cpp diff --git a/stglibs/conffiles.lib/conffiles.cpp b/stglibs/conffiles.lib/conffiles.cpp index cf7dffee..263c9933 100644 --- a/stglibs/conffiles.lib/conffiles.cpp +++ b/stglibs/conffiles.lib/conffiles.cpp @@ -36,11 +36,11 @@ #include // E* #include #include +#include #include -#include "conffiles.h" -#include "common.h" +#include "stg/conffiles.h" using namespace std; @@ -53,7 +53,8 @@ return (strcasecmp(str1.c_str(), str2.c_str()) < 0); CONFIGFILE::CONFIGFILE(const string & fn, bool nook) : param_val(StringCaseCmp), fileName(fn), - error(0) + error(0), + changed(false) { ifstream f(fileName.c_str()); @@ -103,33 +104,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 { const map::const_iterator it(param_val.find(param)); -// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ if (it != param_val.end()) { - // þÔÏ-ÔÏ ÓÔÏÉÔ *val = it->second; return 0; } @@ -141,6 +122,7 @@ return -1; void CONFIGFILE::WriteString(const string & param, const string &val) { param_val[param] = val; +changed = true; } //--------------------------------------------------------------------------- int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const @@ -166,11 +148,9 @@ return -1; int CONFIGFILE::ReadInt(const string & param, int * val, int defaultVal) const { const 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) @@ -188,11 +168,9 @@ return -1; int CONFIGFILE::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const { const 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) @@ -210,11 +188,9 @@ return -1; int CONFIGFILE::ReadLongInt(const string & param, long int * val, long int defaultVal) const { const 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,11 +208,9 @@ return -1; int CONFIGFILE::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const { const 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,11 +228,9 @@ return -1; int CONFIGFILE::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const { const 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,11 +248,9 @@ return -1; int CONFIGFILE::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const { const 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,11 +268,9 @@ return -1; int CONFIGFILE::ReadShortInt(const string & param, short int * val, short int defaultVal) const { const 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,11 +288,9 @@ return -1; int CONFIGFILE::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const { const 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) @@ -341,19 +307,18 @@ return -1; //--------------------------------------------------------------------------- void CONFIGFILE::WriteInt(const 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 { const 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) @@ -373,6 +338,7 @@ void CONFIGFILE::WriteDouble(const string & param, double val) char s[30]; snprintf(s, 30, "%f", val); param_val[param] = s; +changed = true; } //--------------------------------------------------------------------------- int CONFIGFILE::Flush(const std::string & path) const @@ -397,8 +363,11 @@ return 0; //--------------------------------------------------------------------------- int CONFIGFILE::Flush() const { -std::string pid; -x2str(getpid(), pid); +if (!changed) + return 0; + +char pid[6]; +snprintf(pid, sizeof(pid), "%d", getpid()); if (Flush(fileName + "." + pid)) return -1; @@ -406,6 +375,8 @@ if (Flush(fileName + "." + pid)) if (rename((fileName + "." + pid).c_str(), fileName.c_str())) return -1; +changed = false; + return 0; } //---------------------------------------------------------------------------