X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/b12381fd076b83ab289dc38a14b701c4fbb76715..1325cf9788d4089f9d20bf2121b0872c6369c817:/stglibs/conffiles.lib/conffiles.cpp?ds=sidebyside diff --git a/stglibs/conffiles.lib/conffiles.cpp b/stglibs/conffiles.lib/conffiles.cpp index 47a92db0..ea51939a 100644 --- a/stglibs/conffiles.lib/conffiles.cpp +++ b/stglibs/conffiles.lib/conffiles.cpp @@ -28,14 +28,20 @@ */ //--------------------------------------------------------------------------- -#include + +// getpid +#include +#include + +#include // E* #include #include +#include #include #include "conffiles.h" -#include "common.h" +#include "stg/common.h" using namespace std; @@ -48,7 +54,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()); @@ -84,6 +91,7 @@ while (getline(f, line)) //--------------------------------------------------------------------------- CONFIGFILE::~CONFIGFILE() { +Flush(); } //--------------------------------------------------------------------------- const string & CONFIGFILE::GetFileName() const @@ -91,33 +99,12 @@ const string & CONFIGFILE::GetFileName() const return fileName; } //--------------------------------------------------------------------------- -int CONFIGFILE::Error() +int CONFIGFILE::Error() const { int e = error; error = 0; return e; } -//--------------------------------------------------------------------------- -int CONFIGFILE::Flush() -{ -ofstream f(fileName.c_str()); -if (!f.is_open()) - { - error = EIO; - return EIO; - } - -map::const_iterator it = param_val.begin(); -while (it != param_val.end()) - { - f << it->first << "=" << it->second << "\n"; - it++; - } - -f.close(); - -return 0; -} /*//--------------------------------------------------------------------------- int CONFIGFILE::ReadString(const string & param, char * str, int * maxLen, const char * defaultVal) const { @@ -152,18 +139,11 @@ if (it != param_val.end()) *val = defaultVal; return -1; } -/*//--------------------------------------------------------------------------- -int CONFIGFILE::WriteString(const string & param, const char * val) -{ -WriteString(param, string(val)); -return 0; -}*/ //--------------------------------------------------------------------------- -int CONFIGFILE::WriteString(const string & param, const string &val) +void CONFIGFILE::WriteString(const string & param, const string &val) { param_val[param] = val; -Flush(); -return 0; +changed = true; } //--------------------------------------------------------------------------- int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const @@ -362,14 +342,12 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -int CONFIGFILE::WriteInt(const string & param, int64_t val) +void CONFIGFILE::WriteInt(const string & param, int64_t val) { string s; -//sprintf(s, "%lld", val); x2str(val, s); param_val[param] = s; -Flush(); -return 0; +changed = true; } //--------------------------------------------------------------------------- int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const @@ -394,12 +372,50 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -int CONFIGFILE::WriteDouble(const string & param, double val) +void CONFIGFILE::WriteDouble(const string & param, double val) { char s[30]; -sprintf(s, "%f", val); +snprintf(s, 30, "%f", val); param_val[param] = s; -Flush(); +changed = true; +} +//--------------------------------------------------------------------------- +int CONFIGFILE::Flush(const std::string & path) const +{ +ofstream f(path.c_str()); +if (!f.is_open()) + { + error = EIO; + return EIO; + } + +map::const_iterator it = param_val.begin(); +while (it != param_val.end()) + { + f << it->first << "=" << it->second << "\n"; + ++it; + } + +f.close(); +return 0; +} +//--------------------------------------------------------------------------- +int CONFIGFILE::Flush() const +{ +if (!changed) + return 0; + +std::string pid; +x2str(getpid(), pid); + +if (Flush(fileName + "." + pid)) + return -1; + +if (rename((fileName + "." + pid).c_str(), fileName.c_str())) + return -1; + +changed = false; + return 0; } //---------------------------------------------------------------------------