c语言头文件inistd,发布跨平台INI文件读写API(C++版本) V0.2.1

2008-03-25 14:02 | 豆花鱼片

挺不错的, 我在你的代码上修改了IniFile::parse_file方法,

使其能分析

name = value

这种=两边带有空格或\t的格式:

int IniFile::parse_file(const char *section, const char *key, const char *buf,int *sec_s,int *sec_e,

int *key_s,int *key_e, int *value_s, int *value_e)

{

const char *p = buf;

int i=0;

assert(buf!=NULL);

assert(section != NULL && strlen(section));

assert(key != NULL && strlen(key));

*sec_e = *sec_s = *key_e = *key_s = *value_s = *value_e = -1;

while( !end_of_string(p[i]) ) {

//find the section

if( ( 0==i || newline(p[i-1]) ) && left_barce(p[i]) )

{

int section_start=i+1;

//find the ']'

do {

i++;

} while( !right_brace(p[i]) && !end_of_string(p[i]));

if( 0 == strncmp(p+section_start,section, i-section_start)) {

int newline_start=0;

i++;

//Skip over space char after ']'

while(isspace(p[i])) {

i++;

}

//find the section

*sec_s = section_start;

*sec_e = i;

while( ! (newline(p[i-1]) && left_barce(p[i]))

&& !end_of_string(p[i]) ) {

int j=0;

//get a new line

newline_start = i;

while( !newline(p[i]) && !end_of_string(p[i]) ) {

i++;

}

//now i is equal to end of the line

j = newline_start;

int valid = j;

if('#' != p[j]) //skip over comment

{

while(j < i && p[j]!='=') {

j++;

if('=' == p[j]) {

if(strncmp(key,p+newline_start,valid-newline_start)==0)

{

//find the key ok

*key_s = newline_start;

*key_e = j-1;

while(' ' == p[valid] || '\t' == p[valid])

valid++;

*value_s = valid;

*value_e = i;

return 1;

}

}

if(' ' != p[j] && '\t' != p[j])

valid = j;

}

}

i++;

}

}

}

else

{

i++;

}

}

return 0;

}  回复  更多评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值