读取配置文件
本帖最后由 古月不傲 于 2020-12-31 17:06 编辑#ifndef __WIND_CONFIG_H__
#define __WIND_CONFIG_H__
#include <iostream>
#include <string>
#include <map>
#include <fstream>
#include <cstring>
#include <utility>
#include "../_include/wind_log.h"
#include "../_include/wind_misc.h"
// 配置文件相关
namespace wind_config
{
class CConfig
{
public:
CConfig() = default;
CConfig(const CConfig &) = delete;
CConfig &operator =(const CConfig &) = delete;
public:
static CConfig &get_instance()
{
static CConfig obj;
return obj;
}
~CConfig()
{
}
public:
// 加载配置文件
int load_config(const std::string &fileName)
{
if (fileName.size() == 0)
{
wind_log::CLog::get_instance().write_log("fileName.size() failed, errno:%d, at %s:%s.%d",
errno, __FILE__, __func__, __LINE__);
return -1;
}
std::ifstream inFile;
inFile.open(fileName.c_str(), std::ios::binary | std::ios::in);
// 如果打开文件失败
if (inFile.is_open() == false)
{
wind_log::CLog::get_instance().write_log("inFile.is_open() failed, errno:%d, at %s:%s.%d",
errno, __FILE__, __func__, __LINE__);
return -1;
}
char buf{};
// 逐行解析
while (inFile.eof() == false)
{
inFile.getline(buf, 512);
// 空行不处理
if (strlen(buf) == 0)
continue;
// 开头是特殊字符不处理
if (buf == '\0' || buf == '#' || buf == '['
|| buf == ' ' || buf == '\t' || buf == '\n')
continue;
// 截取键值对
char *pEqual = strchr(buf, '=');
// 无效行不处理
if (pEqual == nullptr)
continue;
char tempKey{};
char tempValue{};
strncpy(tempKey, buf, pEqual - buf);
strcpy(tempValue, pEqual+1);
// 去掉左右空格
wind_misc::lTrim(tempKey);
wind_misc::rTrim(tempKey);
wind_misc::lTrim(tempValue);
wind_misc::rTrim(tempValue);
// 插入map
this->m_cfgItem.insert(std::pair<std::string, std::string>(tempKey, tempValue));
}
inFile.close();
return 0;
}
// 获取值
int get_value(const std::string &str)
{
if (str.size() == 0)
{
wind_log::CLog::get_instance().write_log("str_size() failed, errno:%d, at %s:%s.%d",
errno, __FILE__, __func__, __LINE__);
return -1;
}
auto it = this->m_cfgItem.find(str.c_str());
if (it != this->m_cfgItem.end())
return std::atoi(it->second.c_str());
return -1;
}
private:
std::map<std::string, std::string> m_cfgItem;
};
}
#endif
# 子进程的个数
child_process_num = 2
# 需要监听端口的个数
listen_port_count = 2
# 需要监听的端口
listenPort0 = 80
listenPort1 = 5578
# 允许连接连入的数量
allow_connections_num = 1024
页:
[1]