=========》测试对照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 测试结果