关键存储数据结构
用的是map嵌套
//
// Created by zhanglei on 19-8-14.
//
#ifndef LOGSENTRY_INIFILECONFIG_H
#define LOGSENTRY_INIFILECONFIG_H
using std::map;
using std::string;
using std::iterator;
#define MAXLINE 1024 * 8
#endif //LOGSENTRY_INIFILECONFIG_H
namespace service {
class IniFileConfig {
public:
struct unit{
string key;
string value;
};
map<string,string>configUnit;
map<string,map <string,string>>mContent;
bool readConfig(std::string &filename);
ssize_t readLine(int fd,char* buf,size_t manxLine);
virtual int onGetConfig(map<string,map <string,string>>Config);
private:
int fileFd;
};
}
//
// Created by zhanglei on 19-8-14.
//
#include <vector>
#include "include/MainService.h"
using std::string;
//读取配置文件
bool service::IniFileConfig::readConfig(string &filename) {
if (access(filename.c_str(), R_OK) == -1) {
LOG_TRACE(LOG_WARING, false, "Log",
"IniFileConfig::readConfig" << __LINE__ << ":" << filename << " is not file;");
return false;
}
//打开配置文件进行读取
fileFd = open(filename.c_str(), O_RDWR);
if (!fileFd) {
LOG_TRACE(LOG_WARING, false, "Log",
"IniFileConfig::readConfig" << __LINE__ << ":" << filename << " open failed;");
return false;
}
//开始循环一个一个字节的读取配置文件,加载入map中
char buf[MAXLINE];
string config_buffer;
string section;
size_t len;
struct unit un;
while ((readLine(fileFd, buf, 1024 * 8))) {
len = strlen(buf);
if(len>0)
{
config_buffer = buf;
if(config_buffer.find('[') == 0 && config_buffer.find(']') == len-1)
{
//解析出他的值
section = config_buffer.substr(1,len-2);
}else if (config_buffer.find('=') != string::npos)
{
un.key = config_buffer.substr(0,config_buffer.find('='));
un.value = config_buffer.substr(config_buffer.find('=')+1);
configUnit[un.key] = un.value;
mContent[section] = configUnit;
}
}
}
this->onGetConfig(mContent);
}
int service::IniFileConfig::onGetConfig(map<string,map <string,string>>Config) {
}
//按照行来读取
ssize_t service::IniFileConfig::readLine(int fd, char *buf, size_t maxLine) {
bzero(buf, maxLine);
ssize_t n;
n = 0;
char c;
ssize_t res;
while ((res = read(fd, &c, 1))) {
if (res == -1) {
//如果说被信号中断那么就要继续运行不要停
if(errno == EINTR)
{
continue;
}
return -1;
}
n++;
if (c == '\n') {
return n;
}
*buf++ = c;
//到达行数的最大值了再进行累加就要越界了
if (n == maxLine) {
return n;
}
}
return 0;
}