【C++】| 简易实现一个ini配置文件解析器

=========》测试对照Iniparser 4《=========

一、简介

.ini文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式;
	- 后缀名也不一定是".ini"也可以是".cfg",".conf ”或者是".txt";
1.1 格式
INI文件由参数、节、注释组成。
# comments
; comments
[section]	------		节
name=value	------		(键 = 值)	------		参数
name=value	------		(键 = 值)	------		参数
name=value	------		(键 = 值)	------		参数

[section]	------		节
name=value	------		(键 = 值)	------		参数
1.2 参数
每一个参数都有一个name和一个value;

【如】:
name=value	------		(键 = 值)		------		参数
1.3 节
所有的参数都与一个节结合;
节一般独占一行且左右有[];
节后面的参数都属于该节的参数;
一个节的开始即是另一个节的结束;

【如】:
[section]	
1.4 注释
以;或#开头,一般用于对内容加以解释;

【如】:
;巴拉巴拉
#拔拉拔拉
#
# Twisted.ini
# This file is meant for regression tests

# Different blank settings around the equal sign
[blanks]
a=1
b=1;
c=1; comment
d=1# comment

e =1
f =1;
g =1; comment
h =1# comment

i= 1
j= 1;
k= 1; comment
l= 1# comment

m = 1
n = 1;
o = 1; comment
p = 1# comment

q=1 ;
r=1 ; comment
s=1 ;comment
t=1 #comment

# Empty values
[empty]
a = ''
b = ""

c = '' ;
d = "" ;

e = '' ; comment
f = "" ; comment

g =
h = ;
i = ; comment
j = # comment

k=
l=;
m=;comment
n=#

# Peculiar values
[peculiar]
a=';';
b='#'#
c=';';comment
d='#'#comment
e=\;
f=\#
g=\;comment
h=\#comment
i=;;
j=##
k=;;;;;;;;;;
l=##########

# Quotes
[quotes]
s1='
s2=''
s3='''
s4=''''

d1="
d2=""
d3="""
d4=""""

m1='"'
m2="'"

h1=hello'world
h2='hello'world
h3='hello'world'

h4=hello"world
h5="hello"world
h6="hello"world"

# Section names
[a]
[ b]
[c ]
[ d ]
[ begin    end ]
[ open[ ]

# Multi-line inputs
[multi]
a = begin\
end
b = begin \
end
c = begin \
 end
d = 1\
2\
3\
4
e = 1 \ 
    2 \ 
    3 \  
    4
f = 1 ; \
hidden = because of the preceding backslash multi-lining the comment ;
visible = 1
g = 1 #\
and now this comment is hidden too \
and this one too
h = 1
multi \
line \
key = 1
multi \
line \
key = \
multi \
line \
value ;
# end of file

二、代码实现

=========》代码地址《=========

while(getline(ifs, line))
	{
        // 判断是否为空行
        if(line == "") continue;
        // 判断是否为注释
        if(line[0] == '#') continue;

        // 判断是否为节
        if(line[0] == '[') {
            line = strip(line);
            key = line.substr(1, line.size()-2);
            key = strip(key);
            std::cout << "[" << key << "]=UNDEF" << std::endl; 
        // 处理键值
        }else {
            line = strip_right(line);
            if(isContinue) {
                if(line[line.size()-1] == '\\') {
                    val += line.substr(0, line.size()-1);
                    continue;
                }else {
                    val += line;
                    isContinue = false;
                }
            }else {
                pos = line.find("=");
                if(pos == std::string::npos) {
                    if(line[line.size() - 1] == '\\') {
                        kkey = line.substr(0, line.size()-1);
                        isContinue_kkey = true;
                        continue; 
                    }else {
                        std::cout << "[error]: " << kkey << std::endl;
                        continue;
                    }
                }
                if(isContinue_kkey){
                    kkey += line.substr(0, pos);
                    isContinue_kkey = false;
                }else {
                    kkey = line.substr(0, pos);
                }
                kkey = strip(kkey);

                val = line.substr(pos + 1, line.size()-pos); 
                if(val[val.size() - 1] == '\\') {
                    isContinue = true;
                    val = val.substr(0, val.size()-1);
                    continue; 
                }
            }
            val = strip(val);
            do {
                /** 处理单引号 */
                if(val[0] == '\''){
                    if(val.size() != 2 && val == std::string(val.size(), '\'')) 
                        break;
                    pos = val.find('\'', 1);
                    if(pos != std::string::npos) {
                        val = val.substr(1, pos-1);
                        break;
                    }
                }
                /** 处理双引号 */
                if(val[0] == '\"'){
                    if(val.size() != 2 && val == std::string(val.size(), '\"')) 
                        break;
                    pos = val.find('\"', 1);
                    if(pos != std::string::npos) {
                        val = val.substr(1, pos-1);
                        break;
                    }
                }
                /** 处理注释 */
                pos = val.find(';');
                if(pos != std::string::npos) {
                    val = val.substr(0, pos); 
                }
                pos = val.find('#');
                if(pos != std::string::npos) {
                    val = val.substr(0, pos); 
                }
            }while(0);

            val = strip_right(val);
            m_dict[key][kkey] = val;
            std::cout << "[" << key << ":" << kkey << "]=[" << val << "]" << std::endl;
        }
	}
2.1 测试结果

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jxiepc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值