📄 properties.cpp
字号:
#include "nlkit/Properties.h"
#include "nlkit/lexical_cast.h"
#include "nlkit/PropertiesException.h"
#include <string.h>
#include <strings.h>
#include <fstream>
#include <stdlib.h>
#define MAX_LINE_LENGTH 1024
using namespace std;
using namespace nlkit;
Properties::Properties(const char* fileName)
{
fromFile(fileName);
}
Properties::Properties(const std::string& fileName)
{
fromFile(fileName.c_str());
}
Properties::~Properties()
{
reset();
}
void Properties::fromFile(const std::string& fileName)
{
fromFile(fileName.c_str());
}
void Properties::fromFile(const char* fileName)
{
m_prefix = "";
//LOG4CPLUS_DEBUG(logger, "open filename="<<fileName);
fstream file(fileName, ios::in);
string pathName = baseDir(fileName);
//LOG4CPLUS_DEBUG(logger, "baseDir="<<pathName);
char tmpStr[MAX_LINE_LENGTH];
if (file.is_open())
{
while (!file.eof())
{
file.getline(tmpStr, MAX_LINE_LENGTH);
//LOG4CPLUS_DEBUG(logger, "cfg line="<<tmpStr);
int ret = parserLine(tmpStr);
switch(ret)
{
case IS_NORMAL:
if (!m_prefix.empty())
m_name = m_prefix + m_name;
m_ProferiesMap[m_name]=m_value;
//LOG4CPLUS_DEBUG(logger, "["<<m_name<<"]="<<m_ProferiesMap[m_name]);
break;
case IS_INCLUDE:
regularization(pathName, m_name);
//LOG4CPLUS_DEBUG(logger, "regularization="<<m_name);
if (m_name != fileName) //避免自包含
fromFile(m_name);
break;
case IS_GROUP:
m_prefix = m_name + ".";
break;
default :
break;
}
}
}
else
{
THROW_EXCEPTION(PropertiesException, string(fileName) + " can not opened.");
}
}
void Properties::reset()
{
m_ProferiesMap.clear();
}
int Properties::parserLine(char* lineStr)
{
m_name = "";
m_value = "";
char *head = strim(lineStr);
///去除注释
char *p = strchr(head, '#');
if (NULL != p)
{
*p = '\0';
}
if ('\0' == head[0])
return IS_COMMENT;
if (strncmp(head, "include", 7) == 0)
{
m_name = strim(head+7);
// printf("include=%s", m_name.c_str());
return IS_INCLUDE;
}
if (strncmp(head, "[", 1) == 0)
{
p = strchr(head, ']');
if (NULL == p)
return IS_BAD;
*p = '\0';
m_name = head+1;
return IS_GROUP;
}
p = strchr(head, '=');
if (NULL == p)
return IS_BAD;
*p = '\0';
p++;
m_name = strim(head);
m_value = strim(p);
//printf("%s=%s\n", m_name.c_str(), m_value.c_str());
parserValue(m_value);
//printf("after parserValue %s=%s\n", m_name.c_str(), m_value.c_str());
return IS_NORMAL;
}
std::string Properties::baseDir(const std::string& fileName)
{
string::size_type pos = fileName.rfind("/");
if (string::npos == pos)
{
//取当前工作目录
return "./";
}
else
{
return fileName.substr(0, pos+1);
}
}
void Properties::regularization(const std::string& pathName, std::string& name)
{
if ('/' == name[0])
{
return ;
}
else
{
name.insert(0, pathName);
}
}
char *Properties::strim(char *str)
{
char *head = str;
char *tail = str + strlen(str);
while( *head == ' ' || *head == '\t' ) head++; //跳过空格或TAB
if (head != tail)
{
tail--;
while( *tail == ' ' || *tail == '\t' || *tail == 0x0d) tail--;
*(++tail) = '\0';
}
return head;
}
void Properties::parserValue(std::string& m_value)
{
string::size_type ePos = string::npos;
string::size_type bPos = m_value.find("$(");
while (string::npos != bPos)
{
ePos = m_value.find(")", bPos+2);
if (string::npos != ePos)
{
string varName = m_value.substr(bPos+2, ePos-bPos+1-3);
string varValue = "";
if (!getString(varName, varValue))
{
//如果变量不在配置文件中,则取环境变量
char* val = getenv(varName.c_str());
if (val) varValue = val;
}
//LOG4CPLUS_DEBUG(logger, "parserValue: "<<varName<<"="<<varValue)
m_value.replace(bPos, ePos-bPos+1, varValue);
//LOG4CPLUS_DEBUG(logger, "parserValue: m_value="<<m_value)
ePos += varValue.size() - (ePos - bPos);
bPos = m_value.find("$(", ePos+1);
}
}
}
int Properties::getInt(const std::string& profertyName) const
{
return lexical_cast<int>(getString(profertyName));
}
int Properties::getInt(const std::string& profertyName, int defaultValue) const
{
return lexical_cast<int>(getString(profertyName, lexical_cast<string>(defaultValue)));
}
void Properties::setInt(const std::string& profertyName, int value)
{
m_ProferiesMap[profertyName] = lexical_cast<string>(value);
return;
}
std::string Properties::getString(const std::string& profertyName) const
{
string value;
if (getString(profertyName, value))
return value;
THROW_EXCEPTION(PropertiesException, profertyName + " is not found.");
}
std::string Properties::getString(const std::string& profertyName, const std::string& defaultValue) const
{
string value;
if (getString(profertyName, value))
return value;
return defaultValue;
}
void Properties::setString(const std::string& profertyName, const std::string& value)
{
m_ProferiesMap[profertyName] = value;
return;
}
bool Properties::getBool(const std::string& profertyName, bool defaultValue) const
{
string value;
if (getString(profertyName, value))
{
if (value == "0" || 0 == strcasecmp(value.c_str(), "no") ||
0 == strcasecmp(value.c_str(), "disable") ||
0 == strcasecmp(value.c_str(), "false"))
{
return false;
}
return true;
}
return defaultValue;
}
bool Properties::getBool(const std::string& profertyName) const
{
string value = getString(profertyName);
if (value == "0" || 0 == strcasecmp(value.c_str(), "no") ||
0 == strcasecmp(value.c_str(), "disable") ||
0 == strcasecmp(value.c_str(), "false"))
{
return false;
}
return true;
}
void Properties::setBool(const std::string& profertyName, bool value)
{
m_ProferiesMap[profertyName] = lexical_cast<string>(value);
return;
}
bool Properties::getString(const std::string& profertyName, std::string& value) const
{
bool ret;
storIter_t iter;
iter = m_ProferiesMap.find(profertyName);
if (ret = (iter != m_ProferiesMap.end()))
value = iter->second;
return ret;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -