]> git.stg.codes - stg.git/blobdiff - stglibs/conffiles.lib/conffiles.cpp
Keep stglibs headers and libraries "at home"
[stg.git] / stglibs / conffiles.lib / conffiles.cpp
index 47a92db08a024cae1ebadc12a3c9afe943edf96e..263c9933daa156add001ef7b611927fe7a5b9938 100644 (file)
  */
 
 //---------------------------------------------------------------------------
-#include <cerrno>
+
+// getpid
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <cerrno> // E*
 #include <cstring>
 #include <cstdlib>
+#include <cstdio>
 
 #include <fstream>
 
-#include "conffiles.h"
-#include "common.h"
+#include "stg/conffiles.h"
 
 using namespace std;
 
@@ -48,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());
 
@@ -84,6 +90,7 @@ while (getline(f, line))
 //---------------------------------------------------------------------------
 CONFIGFILE::~CONFIGFILE()
 {
+Flush();
 }
 //---------------------------------------------------------------------------
 const string & CONFIGFILE::GetFileName() const
@@ -91,60 +98,19 @@ 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<string, string>::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
-{
-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<string, string>::const_iterator it(param_val.find(param));
-// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
 
 if (it != param_val.end())
     {
-    // þÔÏ-ÔÏ ÓÔÏÉÔ
     *val = it->second;
     return 0;
     }
@@ -152,18 +118,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
@@ -189,11 +148,9 @@ return -1;
 int CONFIGFILE::ReadInt(const string & param, int * val, int defaultVal) const
 {
 const map<string, string>::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)
@@ -211,11 +168,9 @@ return -1;
 int CONFIGFILE::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const
 {
 const map<string, string>::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)
@@ -233,11 +188,9 @@ return -1;
 int CONFIGFILE::ReadLongInt(const string & param, long int * val, long int defaultVal) const
 {
 const map<string, string>::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)
@@ -255,11 +208,9 @@ return -1;
 int CONFIGFILE::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const
 {
 const map<string, string>::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)
@@ -277,11 +228,9 @@ return -1;
 int CONFIGFILE::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const
 {
 const map<string, string>::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)
@@ -299,11 +248,9 @@ return -1;
 int CONFIGFILE::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const
 {
 const map<string, string>::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)
@@ -321,11 +268,9 @@ return -1;
 int CONFIGFILE::ReadShortInt(const string & param, short int * val, short int defaultVal) const
 {
 const map<string, string>::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)
@@ -343,11 +288,9 @@ return -1;
 int CONFIGFILE::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const
 {
 const map<string, string>::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)
@@ -362,24 +305,20 @@ 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;
+char buf[32];
+snprintf(buf, sizeof(buf), "%lld", static_cast<long long int>(val));
+param_val[param] = buf;
+changed = true;
 }
 //---------------------------------------------------------------------------
 int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const
 {
 const map<string, string>::const_iterator it(param_val.find(param));
-// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
 
 if (it != param_val.end())
     {
-    // þÔÏ-ÔÏ ÓÔÏÉÔ
     char *res;
     *val = strtod(it->second.c_str(), &res);
     if (*res != 0)
@@ -394,12 +333,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<string, string>::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;
+
+char pid[6];
+snprintf(pid, sizeof(pid), "%d", getpid());
+
+if (Flush(fileName + "." + pid))
+    return -1;
+
+if (rename((fileName + "." + pid).c_str(), fileName.c_str()))
+    return -1;
+
+changed = false;
+
 return 0;
 }
 //---------------------------------------------------------------------------