c/c++ 简单的解析ini配置文件程序

来源:互联网 发布:算法导论16.1 2 编辑:程序博客网 时间:2024/05/22 10:27

现在基本使用gflags,所以不怎么使用这个读取ini对应的程序或库了,

由于ini一般比较方便,所以程序中涉及到的配置文件一般都用conf.ini

针对ini的解析网上也有很多,其实只要你会写程序,完全可以自己定制一个(如果你认为有必要的话,不过我建议不要重复造轮子的好)

下面这个是本地编译可用的一个例子,是不是不是那么麻烦?

conf.ini

[s1]                                                                                                                                                                                                              
a=1
b=2
c=abc/efg/t.log
[s2]
e=5
f=m_abc


iniParser.cpp

#include "iniParser.h"


int INIParser::parser(){
        FILE* fp = fopen(inifile, "r");
        if(NULL == fp){
                return -1;
        }   

        std::string SECTION("");
        while(!feof(fp)){
                char _buf[MAX_BUF_SIZE] = {0};
                char* gret = fgets(_buf, MAX_BUF_SIZE, fp);
                        if(NULL == gret){
                                continue;
                        }   
                std::string sbuf(_buf);
                sbuf.erase(0, sbuf.find_first_not_of(" "));
                sbuf.erase(sbuf.find_last_not_of(" ")+1);

                //skip comments
                if(comments.end() != find(comments.begin(), comments.end(), sbuf[0])){
                        continue;
                }   

                if('[' == sbuf[0]){
                        sbuf.erase(0, sbuf.find_first_not_of("["));
                        sbuf.erase(sbuf.find_last_not_of("]")-1);
                        SECTION = sbuf;

                }else{
                        size_t pos = sbuf.find("=");
                        if(std::string::npos == pos){
                                continue;
                        }   

                        std::string key = sbuf.substr(0, pos);
                        std::string value = sbuf.substr(pos+1);
                        value.erase(value.find_last_not_of("\r\n")+1);
                        inimap[SECTION][key] = value;
                }   
        }   

        fclose(fp);

        return 0;
}

ssmap& INIParser::getKeyValBySection(const char* section){
        return inimap[std::string(section)];
}


iniParser.h

#ifndef __INIPARSER_H__                                                                                                                                                                                           
#define __INIPARSER_H__


#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <algorithm>


typedef std::map<std::string, std::string> ssmap;
typedef std::map<std::string, ssmap> INIMAP;
#define MAX_BUF_SIZE  512


class INIParser {
        public:
                INIParser(const char* inifile_name){
                        inifile = inifile_name;

                        comments.push_back('#');
                        comments.push_back(';');
                }   
                ~INIParser(){}

        public:
                int parser();
                ssmap& getKeyValBySection(const char* section);
        public:
                const char* inifile;
                INIMAP inimap;
                std::vector<char>comments;
    
};
#endif


原创粉丝点击