*/
//---------------------------------------------------------------------------
-#include <errno.h>
-#include <string.h>
-#include <stdlib.h>
+
+// getpid
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <cerrno> // E*
+#include <cstring>
+#include <sstream>
+#include <cstdlib>
+#include <cstdio>
+
#include <fstream>
-#include <algorithm>
-#include "conffiles.h"
-#include "common.h"
-using namespace std;
+#include "stg/conffiles.h"
+namespace
+{
//---------------------------------------------------------------------------
-bool StringCaseCmp(const string & str1, const string & str2)
+std::string TrimL(std::string val)
{
-return (strcasecmp(str1.c_str(), str2.c_str()) < 0);
+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;
}
//---------------------------------------------------------------------------
-CONFIGFILE::CONFIGFILE(const string &fn):
-param_val(StringCaseCmp)
+std::string Trim(std::string val)
{
-fileName = fn;
-f = fopen(fn.c_str(), "rt");
+return TrimR(TrimL(val));
+}
+//---------------------------------------------------------------------------
+} // namespace anonymous
-error = 0;
-param_val.clear();
+//---------------------------------------------------------------------------
+bool StringCaseCmp(const std::string & str1, const std::string & str2)
+{
+return (strcasecmp(str1.c_str(), str2.c_str()) < 0);
+}
+//---------------------------------------------------------------------------
+CONFIGFILE::CONFIGFILE(const std::string & fn, bool nook)
+ : param_val(StringCaseCmp),
+ fileName(fn),
+ error(0),
+ changed(false)
+{
+std::ifstream f(fileName.c_str());
if (!f)
{
- error = -1;
+ if (!nook)
+ error = -1;
return;
}
-string line, parameter, value;
-
-unsigned long pos;
-bool emptyLine;
-unsigned char c;
-
-while (!feof(f))
+std::string line;
+while (getline(f, line))
{
- line.erase(line.begin(), line.end());
-
- c = fgetc(f);
- while (!feof(f))
- {
- //printf("%c", c);
- if (c == '\n')
- break;
- line.push_back(c);
- c = fgetc(f);
- }
-
- pos = line.find('#');
- if (pos != string::npos)
+ size_t pos = line.find('#');
+ if (pos != std::string::npos)
line.resize(pos);
- emptyLine = true;
- for (unsigned int i = 0; i < line.size(); i++)
- {
- if (line[i] != ' ' && line[i] != '\t' && line[i] != '\n' && line[i] != '\r')
- {
- emptyLine = false;
- break;
- }
- }
- if (emptyLine)
- {
+ if (line.find_first_not_of(" \t\r") == std::string::npos)
continue;
- }
- pos = line.find("=");
- if (pos == string::npos)
+ pos = line.find_first_of('=');
+ if (pos == std::string::npos)
{
- fclose(f);
error = -1;
- //printf("%s find(=) error\n", __FILE__);
return;
}
- parameter = line.substr(0, pos);
- //transform(parameter.begin(), parameter.end(), parameter.begin(), tolower);
- value = line.substr(pos + 1);
- //cout << parameter << "==" << value << endl;
+
+ std::string parameter = Trim(line.substr(0, pos));
+ std::string value = Trim(line.substr(pos + 1));
param_val[parameter] = value;
- //cout << parameter << "==" << param_val[parameter] << endl;
}
-
-fclose(f);
}
//---------------------------------------------------------------------------
CONFIGFILE::~CONFIGFILE()
{
-
+Flush();
}
//---------------------------------------------------------------------------
-const string & CONFIGFILE::GetFileName() const
+const std::string & CONFIGFILE::GetFileName() const
{
return fileName;
}
//---------------------------------------------------------------------------
-int CONFIGFILE::Error()
+int CONFIGFILE::Error() const
{
int e = error;
error = 0;
return e;
}
//---------------------------------------------------------------------------
-int CONFIGFILE::FindParameter(const string ¶meter, string * value) const
-{
-it = param_val.find(parameter);
-if (it == param_val.end())
- return -1;
-
-*value = param_val[parameter];
-return 0;
-}
-//---------------------------------------------------------------------------
-int CONFIGFILE::Flush()
-{
-fstream f(fileName.c_str(), ios::out);
-if (!f.is_open())
- {
- error = EIO;
- return EIO;
- }
-
-it = param_val.begin();
-while (it != param_val.end())
- {
- f << it->first << "=" << it->second << endl;
- it++;
- }
-
-f.close();
-
-return 0;
-}
-//---------------------------------------------------------------------------
-int CONFIGFILE::ReadString(const string & param, char * str, int * maxLen, const char * defaultVal) const
+int CONFIGFILE::ReadString(const std::string & param, std::string * val, const std::string & defaultVal) const
{
-it = param_val.find(param);
-// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
+const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator 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
-{
-it = param_val.find(param);
-// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
-
-if (it != param_val.end())
- {
- // þÔÏ-ÔÏ ÓÔÏÉÔ
- *val = param_val[param];
+ *val = it->second;
return 0;
}
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 std::string & param, const std::string &val)
{
param_val[param] = val;
-Flush();
-return 0;
+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
{
-it = param_val.find(param);
+const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
if (it != param_val.end())
{
char *res;
- *val = strtol(param_val[param].c_str(), &res, 10);
+ *val = strtol(it->second.c_str(), &res, 10);
if (*res != 0)
{
*val = defaultVal; //Error!
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
{
-it = param_val.find(param);
-// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
+const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
if (it != param_val.end())
{
- // þÔÏ-ÔÏ ÓÔÏÉÔ
char *res;
- *val = strtol(param_val[param].c_str(), &res, 10);
+ *val = static_cast<int>(strtol(it->second.c_str(), &res, 10));
if (*res != 0)
{
*val = defaultVal; //Error!
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
{
-it = param_val.find(param);
-// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
+const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
if (it != param_val.end())
{
- // þÔÏ-ÔÏ ÓÔÏÉÔ
char *res;
- *val = strtoul(param_val[param].c_str(), &res, 10);
+ *val = static_cast<unsigned int>(strtoul(it->second.c_str(), &res, 10));
if (*res != 0)
{
*val = defaultVal; //Error!
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
{
-it = param_val.find(param);
-// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
+const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
if (it != param_val.end())
{
- // þÔÏ-ÔÏ ÓÔÏÉÔ
char *res;
- *val = strtol(param_val[param].c_str(), &res, 10);
+ *val = strtol(it->second.c_str(), &res, 10);
if (*res != 0)
{
*val = defaultVal; //Error!
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
{
-it = param_val.find(param);
-// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
+const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
if (it != param_val.end())
{
- // þÔÏ-ÔÏ ÓÔÏÉÔ
char *res;
- *val = strtoul(param_val[param].c_str(), &res, 10);
+ *val = strtoul(it->second.c_str(), &res, 10);
if (*res != 0)
{
*val = defaultVal; //Error!
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
{
-it = param_val.find(param);
-// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
+const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
if (it != param_val.end())
{
- // þÔÏ-ÔÏ ÓÔÏÉÔ
char *res;
- *val = strtoll(param_val[param].c_str(), &res, 10);
+ *val = strtoll(it->second.c_str(), &res, 10);
if (*res != 0)
{
*val = defaultVal; //Error!
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
{
-it = param_val.find(param);
-// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
+const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
if (it != param_val.end())
{
- // þÔÏ-ÔÏ ÓÔÏÉÔ
char *res;
- *val = strtoull(param_val[param].c_str(), &res, 10);
+ *val = strtoull(it->second.c_str(), &res, 10);
if (*res != 0)
{
*val = defaultVal; //Error!
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
{
-it = param_val.find(param);
-// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
+const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
if (it != param_val.end())
{
- // þÔÏ-ÔÏ ÓÔÏÉÔ
char *res;
- *val = (short)strtol(param_val[param].c_str(), &res, 10);
+ *val = (short)strtol(it->second.c_str(), &res, 10);
if (*res != 0)
{
*val = defaultVal; //Error!
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
{
-it = param_val.find(param);
-// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
+const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
if (it != param_val.end())
{
- // þÔÏ-ÔÏ ÓÔÏÉÔ
char *res;
- *val = (short)strtoul(param_val[param].c_str(), &res, 10);
+ *val = (short)strtoul(it->second.c_str(), &res, 10);
if (*res != 0)
{
*val = defaultVal; //Error!
return -1;
}
//---------------------------------------------------------------------------
-int CONFIGFILE::WriteInt(const string & param, int64_t val)
+void CONFIGFILE::WriteInt(const std::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;
+}
+//---------------------------------------------------------------------------
+void CONFIGFILE::WriteTime(const std::string & param, time_t val)
+{
+std::stringstream ss;
+ss<<val;
+param_val[param] = ss.str();
+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
{
-it = param_val.find(param);
-// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
+const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
if (it != param_val.end())
{
- // þÔÏ-ÔÏ ÓÔÏÉÔ
char *res;
- *val = strtod(param_val[param].c_str(), &res);
+ *val = strtod(it->second.c_str(), &res);
if (*res != 0)
{
- //cout << param << "=" << param_val[param] << " Error!!!\n";
*val = defaultVal; //Error!
return EINVAL;
}
return 0;
}
-//cout << "îÉÞÅÇÏ ÎÅÔ!!!\n";
-
*val = defaultVal;
return -1;
}
//---------------------------------------------------------------------------
-int CONFIGFILE::WriteDouble(const string & param, double val)
+void CONFIGFILE::WriteDouble(const std::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
+{
+std::ofstream f(path.c_str());
+if (!f.is_open())
+ {
+ error = EIO;
+ return EIO;
+ }
+
+std::map<std::string, std::string, StringCaseCmp_t>::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;
+}
+//---------------------------------------------------------------------------