From 8d18bde0f40cac840d25b98e48b1a8dfcc2157ff Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Fri, 10 Dec 2010 15:44:38 +0200 Subject: [PATCH] =?utf8?q?=D0=9C=D0=BE=D0=B4=D0=B8=D1=84=D0=B8=D0=BA=D0=B0?= =?utf8?q?=D1=86=D0=B8=D1=8F=20=D0=B1=D0=B8=D0=B1=D0=BB=D0=B8=D0=BE=D1=82?= =?utf8?q?=D0=B5=D0=BA=D0=B8=20conffiles?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Слегка переписан класс CONFIGFILE для использования библиотек C++ --- stglibs/conffiles.lib/conffiles.cpp | 113 ++++++++++++---------------- stglibs/conffiles.lib/conffiles.h | 47 +++++------- 2 files changed, 68 insertions(+), 92 deletions(-) diff --git a/stglibs/conffiles.lib/conffiles.cpp b/stglibs/conffiles.lib/conffiles.cpp index a6fbe6d6..9c8c46e8 100644 --- a/stglibs/conffiles.lib/conffiles.cpp +++ b/stglibs/conffiles.lib/conffiles.cpp @@ -33,7 +33,6 @@ #include #include -#include #include "conffiles.h" #include "common.h" @@ -46,14 +45,13 @@ bool StringCaseCmp(const string & str1, const string & str2) return (strcasecmp(str1.c_str(), str2.c_str()) < 0); } //--------------------------------------------------------------------------- -CONFIGFILE::CONFIGFILE(const string &fn): -param_val(StringCaseCmp) +CONFIGFILE::CONFIGFILE(const string & fn) + : param_val(StringCaseCmp), + fileName(fn), + error(0) { -fileName = fn; -f = fopen(fn.c_str(), "rt"); - -error = 0; -param_val.clear(); +ifstream f(fileName.c_str()); +//FILE * f = fopen(fileName.c_str(), "rt"); if (!f) { @@ -61,31 +59,26 @@ if (!f) return; } -string line, parameter, value; - -unsigned long pos; -bool emptyLine; -unsigned char c; - -while (!feof(f)) +string line; +while (getline(f, line)) { - line.erase(line.begin(), line.end()); - - c = fgetc(f); + /*unsigned char c = fgetc(f); while (!feof(f)) { - //printf("%c", c); if (c == '\n') break; line.push_back(c); c = fgetc(f); - } + }*/ - pos = line.find('#'); + size_t pos = line.find('#'); if (pos != string::npos) line.resize(pos); - emptyLine = true; + 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') @@ -97,30 +90,23 @@ while (!feof(f)) if (emptyLine) { continue; - } + }*/ - pos = line.find("="); + pos = line.find_first_of('='); if (pos == 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; + + string parameter = line.substr(0, pos); + string value = line.substr(pos + 1); param_val[parameter] = value; - //cout << parameter << "==" << param_val[parameter] << endl; } - -fclose(f); } //--------------------------------------------------------------------------- CONFIGFILE::~CONFIGFILE() { - } //--------------------------------------------------------------------------- const string & CONFIGFILE::GetFileName() const @@ -137,14 +123,14 @@ return e; //--------------------------------------------------------------------------- int CONFIGFILE::Flush() { -fstream f(fileName.c_str(), ios::out); +ofstream f(fileName.c_str()); if (!f.is_open()) { error = EIO; return EIO; } -it = param_val.begin(); +map::const_iterator it = param_val.begin(); while (it != param_val.end()) { f << it->first << "=" << it->second << endl; @@ -155,7 +141,7 @@ f.close(); return 0; } -//--------------------------------------------------------------------------- +/*//--------------------------------------------------------------------------- int CONFIGFILE::ReadString(const string & param, char * str, int * maxLen, const char * defaultVal) const { it = param_val.find(param); @@ -172,29 +158,29 @@ if (it != param_val.end()) 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); +const map::const_iterator it(param_val.find(param)); // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ if (it != param_val.end()) { // þÔÏ-ÔÏ ÓÔÏÉÔ - *val = param_val[param]; + *val = it->second; return 0; } *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) { @@ -205,12 +191,12 @@ return 0; //--------------------------------------------------------------------------- int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const { -it = param_val.find(param); +const map::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! @@ -225,14 +211,14 @@ return -1; //--------------------------------------------------------------------------- int CONFIGFILE::ReadInt(const string & param, int * val, int defaultVal) const { -it = param_val.find(param); +const map::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! @@ -247,14 +233,14 @@ return -1; //--------------------------------------------------------------------------- int CONFIGFILE::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const { -it = param_val.find(param); +const map::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! @@ -269,14 +255,14 @@ return -1; //--------------------------------------------------------------------------- int CONFIGFILE::ReadLongInt(const string & param, long int * val, long int defaultVal) const { -it = param_val.find(param); +const map::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! @@ -291,14 +277,14 @@ return -1; //--------------------------------------------------------------------------- int CONFIGFILE::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const { -it = param_val.find(param); +const map::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! @@ -313,14 +299,14 @@ return -1; //--------------------------------------------------------------------------- int CONFIGFILE::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const { -it = param_val.find(param); +const map::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! @@ -335,14 +321,14 @@ return -1; //--------------------------------------------------------------------------- int CONFIGFILE::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const { -it = param_val.find(param); +const map::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! @@ -357,14 +343,14 @@ return -1; //--------------------------------------------------------------------------- int CONFIGFILE::ReadShortInt(const string & param, short int * val, short int defaultVal) const { -it = param_val.find(param); +const map::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! @@ -379,14 +365,14 @@ return -1; //--------------------------------------------------------------------------- int CONFIGFILE::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const { -it = param_val.find(param); +const map::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! @@ -411,25 +397,22 @@ return 0; //--------------------------------------------------------------------------- int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const { -it = param_val.find(param); +const map::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; } diff --git a/stglibs/conffiles.lib/conffiles.h b/stglibs/conffiles.lib/conffiles.h index 83a16d9b..d912d9ab 100644 --- a/stglibs/conffiles.lib/conffiles.h +++ b/stglibs/conffiles.lib/conffiles.h @@ -22,7 +22,7 @@ * Author : Boris Mikhailenko */ - /* +/* $Revision: 1.5 $ $Date: 2009/06/22 16:00:38 $ */ @@ -44,46 +44,39 @@ typedef bool (*StringCaseCmp_t)(const string & str1, const string & str2); class CONFIGFILE { -private: - mutable map param_val; - mutable map::iterator it; - - FILE * f; - int Flush(); - //int ReadFile(); - string fileName; - int error; - public: - CONFIGFILE(const string &fn); + CONFIGFILE(const string & fn); ~CONFIGFILE(); const string & GetFileName() const; // 5 ÆÕÎËÃÉÉ Read* ×ÏÚ×ÒÁÝÁÀÔ 0 ÐÒÉ ÕÓÐÅÛÎÏÍ ÓÞÉÔÙ×ÁÎÉÉ // É EINVAL ÐÒÉ ÏÔÓÕÔÓ×ÉÉ ÐÁÒÁÍÅÔÒÁ É ×ÙÓÔÁ×ÌÑÀÔ defaulValue - int ReadString(const string & param, char * val, int * maxLen, const char * defaultVal) const; + //int ReadString(const string & param, char * val, int * maxLen, const char * defaultVal) const; int ReadString(const string & param, string * val, const string & defaultVal) const; - - int ReadTime (const string & param, time_t *, time_t) const; - - int ReadShortInt (const string & param, short int *, short int) const; - int ReadInt (const string & param, int *, int) const; - int ReadLongInt (const string & param, long int *, long int) const; + int ReadTime(const string & param, time_t *, time_t) const; + int ReadShortInt(const string & param, short int *, short int) const; + int ReadInt(const string & param, int *, int) const; + int ReadLongInt(const string & param, long int *, long int) const; int ReadLongLongInt(const string & param, int64_t *, int64_t) const; - - int ReadUShortInt (const string & param, unsigned short int *, unsigned short int) const; - int ReadUInt (const string & param, unsigned int *, unsigned int) const; - int ReadULongInt (const string & param, unsigned long int *, unsigned long int) const; + int ReadUShortInt(const string & param, unsigned short int *, unsigned short int) const; + int ReadUInt(const string & param, unsigned int *, unsigned int) const; + int ReadULongInt(const string & param, unsigned long int *, unsigned long int) const; int ReadULongLongInt(const string & param, uint64_t *, uint64_t) const; + int ReadDouble(const string & param, double * val, double defaultVal) const; - int ReadDouble (const string & param, double * val, double defaultVal) const; - - int WriteString(const string & param, const char * val); + //int WriteString(const string & param, const char * val); int WriteString(const string & param, const string & val); - int WriteInt (const string & param, int64_t val); + int WriteInt(const string & param, int64_t val); int WriteDouble(const string & param, double val); int Error(); + +private: + int Flush(); + + map param_val; + string fileName; + int error; }; //--------------------------------------------------------------------------- #endif -- 2.43.2