挺不错的, 我在你的代码上修改了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;
}
回复 更多评论