c++实现ini配置文件的解析

关键存储数据结构

 

用的是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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值