X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/ae8686e733897fb5ead33d7a7c3fa8569a9c23c5..63ec10fdcc402bf07f7a8c0dfb326feb60249038:/stglibs/conffiles.lib/conffiles.cpp?ds=sidebyside diff --git a/stglibs/conffiles.lib/conffiles.cpp b/stglibs/conffiles.lib/conffiles.cpp index b247d041..f839f7c5 100644 --- a/stglibs/conffiles.lib/conffiles.cpp +++ b/stglibs/conffiles.lib/conffiles.cpp @@ -28,9 +28,15 @@ */ //--------------------------------------------------------------------------- -#include + +// getpid +#include +#include + +#include // E* #include #include +#include #include @@ -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()); @@ -98,27 +105,6 @@ int e = error; error = 0; return e; } -//--------------------------------------------------------------------------- -int CONFIGFILE::Flush() const -{ -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 { @@ -157,6 +143,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 @@ -360,6 +347,7 @@ void CONFIGFILE::WriteInt(const string & param, int64_t val) string s; x2str(val, s); param_val[param] = s; +changed = true; } //--------------------------------------------------------------------------- int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const @@ -389,5 +377,45 @@ 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 +{ +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; } //---------------------------------------------------------------------------