X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/8d18bde0f40cac840d25b98e48b1a8dfcc2157ff..53aac66e1b06ed0887a36b6224d0078bdfe4cc53:/stglibs/conffiles.lib/conffiles.cpp diff --git a/stglibs/conffiles.lib/conffiles.cpp b/stglibs/conffiles.lib/conffiles.cpp index 9c8c46e8..b247d041 100644 --- a/stglibs/conffiles.lib/conffiles.cpp +++ b/stglibs/conffiles.lib/conffiles.cpp @@ -45,32 +45,23 @@ bool StringCaseCmp(const string & str1, const string & str2) return (strcasecmp(str1.c_str(), str2.c_str()) < 0); } //--------------------------------------------------------------------------- -CONFIGFILE::CONFIGFILE(const string & fn) +CONFIGFILE::CONFIGFILE(const string & fn, bool nook) : param_val(StringCaseCmp), fileName(fn), error(0) { ifstream f(fileName.c_str()); -//FILE * f = fopen(fileName.c_str(), "rt"); if (!f) { - error = -1; + if (!nook) + error = -1; return; } string line; while (getline(f, line)) { - /*unsigned char c = fgetc(f); - while (!feof(f)) - { - if (c == '\n') - break; - line.push_back(c); - c = fgetc(f); - }*/ - size_t pos = line.find('#'); if (pos != string::npos) line.resize(pos); @@ -78,20 +69,6 @@ while (getline(f, line)) if (line.find_first_not_of(" \t\r") == string::npos) continue; - /*bool 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) - { - continue; - }*/ - pos = line.find_first_of('='); if (pos == string::npos) { @@ -107,6 +84,7 @@ while (getline(f, line)) //--------------------------------------------------------------------------- CONFIGFILE::~CONFIGFILE() { +Flush(); } //--------------------------------------------------------------------------- const string & CONFIGFILE::GetFileName() const @@ -114,14 +92,14 @@ const string & CONFIGFILE::GetFileName() const return fileName; } //--------------------------------------------------------------------------- -int CONFIGFILE::Error() +int CONFIGFILE::Error() const { int e = error; error = 0; return e; } //--------------------------------------------------------------------------- -int CONFIGFILE::Flush() +int CONFIGFILE::Flush() const { ofstream f(fileName.c_str()); if (!f.is_open()) @@ -133,8 +111,8 @@ if (!f.is_open()) map::const_iterator it = param_val.begin(); while (it != param_val.end()) { - f << it->first << "=" << it->second << endl; - it++; + f << it->first << "=" << it->second << "\n"; + ++it; } f.close(); @@ -175,18 +153,10 @@ 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; } //--------------------------------------------------------------------------- int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const @@ -385,14 +355,11 @@ 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; } //--------------------------------------------------------------------------- int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const @@ -417,12 +384,10 @@ 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(); -return 0; } //---------------------------------------------------------------------------