Слегка переписан класс CONFIGFILE для использования библиотек C++
#include <cstdlib>
#include <fstream>
#include <cstdlib>
#include <fstream>
#include "conffiles.h"
#include "common.h"
#include "conffiles.h"
#include "common.h"
return (strcasecmp(str1.c_str(), str2.c_str()) < 0);
}
//---------------------------------------------------------------------------
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");
-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);
if (c == '\n')
break;
line.push_back(c);
c = fgetc(f);
if (c == '\n')
break;
line.push_back(c);
c = fgetc(f);
+ size_t pos = line.find('#');
if (pos != string::npos)
line.resize(pos);
if (pos != string::npos)
line.resize(pos);
+ 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')
for (unsigned int i = 0; i < line.size(); i++)
{
if (line[i] != ' ' && line[i] != '\t' && line[i] != '\n' && line[i] != '\r')
if (emptyLine)
{
continue;
if (emptyLine)
{
continue;
+ pos = line.find_first_of('=');
if (pos == string::npos)
{
if (pos == string::npos)
{
- //printf("%s find(=) error\n", __FILE__);
- 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;
param_val[parameter] = value;
- //cout << parameter << "==" << param_val[parameter] << endl;
}
//---------------------------------------------------------------------------
CONFIGFILE::~CONFIGFILE()
{
}
//---------------------------------------------------------------------------
CONFIGFILE::~CONFIGFILE()
{
}
//---------------------------------------------------------------------------
const string & CONFIGFILE::GetFileName() const
}
//---------------------------------------------------------------------------
const string & CONFIGFILE::GetFileName() const
//---------------------------------------------------------------------------
int CONFIGFILE::Flush()
{
//---------------------------------------------------------------------------
int CONFIGFILE::Flush()
{
-fstream f(fileName.c_str(), ios::out);
+ofstream f(fileName.c_str());
if (!f.is_open())
{
error = EIO;
return EIO;
}
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 << endl;
while (it != param_val.end())
{
f << it->first << "=" << it->second << endl;
-//---------------------------------------------------------------------------
+/*//---------------------------------------------------------------------------
int CONFIGFILE::ReadString(const string & param, char * str, int * maxLen, const char * defaultVal) const
{
it = param_val.find(param);
int CONFIGFILE::ReadString(const string & param, char * str, int * maxLen, const char * defaultVal) const
{
it = param_val.find(param);
strncpy(str, defaultVal, *maxLen);
*maxLen = strlen(defaultVal);
return -1;
strncpy(str, defaultVal, *maxLen);
*maxLen = strlen(defaultVal);
return -1;
//---------------------------------------------------------------------------
int CONFIGFILE::ReadString(const string & param, string * val, const string & defaultVal) const
{
//---------------------------------------------------------------------------
int CONFIGFILE::ReadString(const string & param, string * val, const string & defaultVal) const
{
-it = param_val.find(param);
+const map<string, string>::const_iterator it(param_val.find(param));
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
if (it != param_val.end())
{
// þÔÏ-ÔÏ ÓÔÏÉÔ
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
if (it != param_val.end())
{
// þÔÏ-ÔÏ ÓÔÏÉÔ
- *val = param_val[param];
return 0;
}
*val = defaultVal;
return -1;
}
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 char * val)
{
WriteString(param, string(val));
return 0;
//---------------------------------------------------------------------------
int CONFIGFILE::WriteString(const string & param, const string &val)
{
//---------------------------------------------------------------------------
int CONFIGFILE::WriteString(const string & param, const string &val)
{
//---------------------------------------------------------------------------
int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const
{
//---------------------------------------------------------------------------
int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const
{
-it = param_val.find(param);
+const map<string, string>::const_iterator it(param_val.find(param));
if (it != param_val.end())
{
char *res;
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!
if (*res != 0)
{
*val = defaultVal; //Error!
//---------------------------------------------------------------------------
int CONFIGFILE::ReadInt(const string & param, int * val, int defaultVal) const
{
//---------------------------------------------------------------------------
int CONFIGFILE::ReadInt(const string & param, int * val, int defaultVal) const
{
-it = param_val.find(param);
+const map<string, string>::const_iterator it(param_val.find(param));
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
if (it != param_val.end())
{
// þÔÏ-ÔÏ ÓÔÏÉÔ
char *res;
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
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!
if (*res != 0)
{
*val = defaultVal; //Error!
//---------------------------------------------------------------------------
int CONFIGFILE::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const
{
//---------------------------------------------------------------------------
int CONFIGFILE::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const
{
-it = param_val.find(param);
+const map<string, string>::const_iterator it(param_val.find(param));
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
if (it != param_val.end())
{
// þÔÏ-ÔÏ ÓÔÏÉÔ
char *res;
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
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!
if (*res != 0)
{
*val = defaultVal; //Error!
//---------------------------------------------------------------------------
int CONFIGFILE::ReadLongInt(const string & param, long int * val, long int defaultVal) const
{
//---------------------------------------------------------------------------
int CONFIGFILE::ReadLongInt(const string & param, long int * val, long int defaultVal) const
{
-it = param_val.find(param);
+const map<string, string>::const_iterator it(param_val.find(param));
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
if (it != param_val.end())
{
// þÔÏ-ÔÏ ÓÔÏÉÔ
char *res;
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
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!
if (*res != 0)
{
*val = defaultVal; //Error!
//---------------------------------------------------------------------------
int CONFIGFILE::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const
{
//---------------------------------------------------------------------------
int CONFIGFILE::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const
{
-it = param_val.find(param);
+const map<string, string>::const_iterator it(param_val.find(param));
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
if (it != param_val.end())
{
// þÔÏ-ÔÏ ÓÔÏÉÔ
char *res;
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
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!
if (*res != 0)
{
*val = defaultVal; //Error!
//---------------------------------------------------------------------------
int CONFIGFILE::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const
{
//---------------------------------------------------------------------------
int CONFIGFILE::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const
{
-it = param_val.find(param);
+const map<string, string>::const_iterator it(param_val.find(param));
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
if (it != param_val.end())
{
// þÔÏ-ÔÏ ÓÔÏÉÔ
char *res;
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
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!
if (*res != 0)
{
*val = defaultVal; //Error!
//---------------------------------------------------------------------------
int CONFIGFILE::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const
{
//---------------------------------------------------------------------------
int CONFIGFILE::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const
{
-it = param_val.find(param);
+const map<string, string>::const_iterator it(param_val.find(param));
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
if (it != param_val.end())
{
// þÔÏ-ÔÏ ÓÔÏÉÔ
char *res;
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
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!
if (*res != 0)
{
*val = defaultVal; //Error!
//---------------------------------------------------------------------------
int CONFIGFILE::ReadShortInt(const string & param, short int * val, short int defaultVal) const
{
//---------------------------------------------------------------------------
int CONFIGFILE::ReadShortInt(const string & param, short int * val, short int defaultVal) const
{
-it = param_val.find(param);
+const map<string, string>::const_iterator it(param_val.find(param));
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
if (it != param_val.end())
{
// þÔÏ-ÔÏ ÓÔÏÉÔ
char *res;
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
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!
if (*res != 0)
{
*val = defaultVal; //Error!
//---------------------------------------------------------------------------
int CONFIGFILE::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const
{
//---------------------------------------------------------------------------
int CONFIGFILE::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const
{
-it = param_val.find(param);
+const map<string, string>::const_iterator it(param_val.find(param));
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
if (it != param_val.end())
{
// þÔÏ-ÔÏ ÓÔÏÉÔ
char *res;
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
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!
if (*res != 0)
{
*val = defaultVal; //Error!
//---------------------------------------------------------------------------
int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const
{
//---------------------------------------------------------------------------
int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const
{
-it = param_val.find(param);
+const map<string, string>::const_iterator it(param_val.find(param));
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
if (it != param_val.end())
{
// þÔÏ-ÔÏ ÓÔÏÉÔ
char *res;
// îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
if (it != param_val.end())
{
// þÔÏ-ÔÏ ÓÔÏÉÔ
char *res;
- *val = strtod(param_val[param].c_str(), &res);
+ *val = strtod(it->second.c_str(), &res);
- //cout << param << "=" << param_val[param] << " Error!!!\n";
*val = defaultVal; //Error!
return EINVAL;
}
return 0;
}
*val = defaultVal; //Error!
return EINVAL;
}
return 0;
}
-//cout << "îÉÞÅÇÏ ÎÅÔ!!!\n";
-
*val = defaultVal;
return -1;
}
*val = defaultVal;
return -1;
}
* Author : Boris Mikhailenko <stg34@ua.fm>
*/
* Author : Boris Mikhailenko <stg34@ua.fm>
*/
$Revision: 1.5 $
$Date: 2009/06/22 16:00:38 $
*/
$Revision: 1.5 $
$Date: 2009/06/22 16:00:38 $
*/
-private:
- mutable map<string, string, StringCaseCmp_t> param_val;
- mutable map<string, string>::iterator it;
-
- FILE * f;
- int Flush();
- //int ReadFile();
- string fileName;
- int error;
-
- CONFIGFILE(const string &fn);
+ CONFIGFILE(const string & fn);
~CONFIGFILE();
const string & GetFileName() const;
// 5 ÆÕÎËÃÉÉ Read* ×ÏÚ×ÒÁÝÁÀÔ 0 ÐÒÉ ÕÓÐÅÛÎÏÍ ÓÞÉÔÙ×ÁÎÉÉ
// É EINVAL ÐÒÉ ÏÔÓÕÔÓ×ÉÉ ÐÁÒÁÍÅÔÒÁ É ×ÙÓÔÁ×ÌÑÀÔ defaulValue
~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 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 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 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 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();
int WriteDouble(const string & param, double val);
int Error();
+
+private:
+ int Flush();
+
+ map<string, string, StringCaseCmp_t> param_val;
+ string fileName;
+ int error;
};
//---------------------------------------------------------------------------
#endif
};
//---------------------------------------------------------------------------
#endif