写程序时,经常会遇到配置文件的问题。解决的方法很多,这里给出一个自己写的配置文件类。目前还存在一些缺陷,待有机会再改进。
/*
* =====================================================================================
*
* Filename: cfg.h
*
* Description: Configure File Operator
*
* Version: 1.0
* Created: 05/21/2008 01:58:23 AM
* Revision: none
* Compiler: gcc
*
* Author: huys (hu), huys03@gmail.com
* Company: HUYS .Inc
*
* =====================================================================================
*/
#ifndef HU_CFG_H
#define HU_CFG_H
#include "global.h"
//
typedef enum tagOptionType {
DISABLE = 0,
ENABLE,
UNKNOWN,
NOVALUE,
NUMVALUE,
STRVALUE
} Option_t;
//
class UConfig
{
public:
UConfig();
UConfig(const char *filename);
~UConfig();
int setFile(const char *filename);
Option_t getOption(const string &optName);
double getOption(const string &optName, double &value);
int getOption(const string &optName, int &value);
string getOption(const string &optName, string &value);
int writeNewFile(const char *filename);
int read();
private:
string getOptionString(const string &optName);
string upper(const string &str);
Option_t checkOptionValue(const string &optValue);
string m_cfg_file;
string m_file_buffer;
};
#endif // HU_CFG_H
#include "cfg.h"
UConfig::UConfig()
: m_cfg_file("default.cfg")
{
;
}
UConfig::UConfig(const char *filename)
: m_cfg_file(filename)
{
m_file_buffer = "";
}
UConfig::~UConfig()
{
;
}
int UConfig::setFile(const char *filename)
{
m_cfg_file = filename;
return 0;
}
Option_t UConfig::getOption(const string &optName)
{
string optionValue = getOptionString(optName);
return checkOptionValue(optionValue);
}
int UConfig::writeNewFile(const char *filename)
{
ofstream ofs(filename, ios_base::out);
if (!ofs)
{
// Error
cout << "Error ofstream!" << endl;
return 1;
}
ofs << m_file_buffer << endl;
return 0;
}
int UConfig::read()
{
ifstream ifs(m_cfg_file.c_str(), ios_base::in);
if (!ifs)
{
// Error
cout << "Error in openning the config file!" << endl;
return 1;
}
string tmp;
while( getline(ifs,tmp) )
{
if (tmp[0] == '#' || tmp[0] == '/0' )
{
continue; // ignore
}
else
{
m_file_buffer += tmp;
m_file_buffer.append("/n");
}
}
return 0;
}
string UConfig::getOptionString(const string &optName)
{
#ifdef HU_DEBUG
cout << "--> Search For: " << optName << endl;
#endif // HU_DEBUG
string optionValue = "";
string searchStr = optName;
searchStr.append("=");
string::size_type location_start = m_file_buffer.find(searchStr, 0);
if (location_start == string::npos)
{
cout << "Cannot find it" << endl;
optionValue.clear();
return optionValue;
}
string::size_type location_end;
location_end = m_file_buffer.find_first_of('/n', location_start);
string::size_type str_length = location_end - location_start;
string optStr;
optStr = m_file_buffer.substr(location_start, str_length);
#ifdef HU_DEBUG
cout << "Option_String: " << optStr << endl;
#endif // HU_DEBUG
if (!optStr.compare(searchStr))
{
cout << "Option is not set!" << endl;
optionValue = "/n";
return optionValue;
}
optionValue = optStr.substr(optStr.find('=')+1);
#ifdef HU_DEBUG
cout << "Option_Value: " << optionValue << endl;
#endif // HU_DEBUG
return optionValue;
}
int UConfig::getOption(const string &optName, int &value)
{
string optionValue = getOptionString(optName);
if (checkOptionValue(optionValue) == NUMVALUE)
{
istringstream iss(optionValue);
iss >> value;
}
else
{
value = 0;
}
return value;
}
double UConfig::getOption(const string &optName, double &value)
{
string optionValue = getOptionString(optName);
if (checkOptionValue(optionValue) == NUMVALUE )
{
istringstream iss(optionValue);
iss >> value;
}
else
{
value = 0.0;
}
return value;
}
string UConfig::getOption(const string &optName, string &value)
{
string optionValue = getOptionString(optName);
if (checkOptionValue(optionValue) == STRVALUE )
{
value = optionValue;
}
else
{
value = "";
}
return value;
}
Option_t UConfig::checkOptionValue(const string &optValue)
{
if (optValue.empty())
{
#ifdef HU_DEBUG
cout << "Option_type: unknown!" << endl;
#endif // HU_DEBUG
return UNKNOWN;
}
if (!optValue.compare("/n"))
{
#ifdef HU_DEBUG
cout << "Option_type: NoValue!" << endl;
#endif // HU_DEBUG
return NOVALUE;
}
if (!this->upper(optValue).compare("DISABLE"))
{
#ifdef HU_DEBUG
cout << "Option_type: disable!" << endl;
#endif // HU_DEBUG
return DISABLE;
}
if (!this->upper(optValue).compare("ENABLE"))
{
#ifdef HU_DEBUG
cout << "Option_type: enable!" << endl;
#endif // HU_DEBUG
return ENABLE;
}
if (optValue.find_first_of("1234567890.")==0)
{
#ifdef HU_DEBUG
cout << "Option_type: A numerical value!" << endl;
#endif // HU_DEBUG
return NUMVALUE;
}
#ifdef HU_DEBUG
cout << "Option_type: A string value!" << endl;
#endif // HU_DEBUG
return STRVALUE;
}
//
string UConfig::upper(const string &str)
{
string tmp = str;
for (unsigned int i=0; i<str.length(); i++)
{
tmp[i] = toupper(tmp[i]);
}
return tmp;
}