posts - 45,  comments - 232,  trackbacks - 0
1工程:跨平台INI文件读写API(C++版本)
版本: 0.2.1
授权方式:GNU GPL
著作权所有(c) 2007 Midapex
    本程序为自由软件;您可依据自由软件基金会所发表的GNU通用公共授权条款规定,就本程序再为发布与/或修改;无论您依据的是本授权的第二版或(您自行选择的)任一日后发行的版本。
   本程序是基于使用目的而加以发布,然而不负任何担保责任;亦无对适售性或特定目的适用性所为的默示性担保。详情请参照GNU通用公共授权。
源代码下载地址:http://www.cppblog.com/Files/dyj057/IniFileCppV0.2.1.zip
描述:
应大家要求,发布C++版本,它是对C语言版本的简单封装,更方便大家使用。

C语言版本地址:http://www.cppblog.com/dyj057/archive/2007/12/07/37958.html
已测试通过的开发环境:
WinXP+VS2005
Ubuntu 7.10 + g++4.1
arm-linux-gcc3.3.4

项目特点:
1.使用标准C库函数,支持Windows、Linux、Unix等多平台。
2.实现小巧精致,长期开源支持。
使用示例代码如下:
为了得到如下的配置:

   [student]

   name=Tony

   age=20

   [teacher]

   name=Tom

   age=50


可以运行如下的代码:

    1 #include "IniFile.h"

    2 #include <iostream>

    3 using namespace std;

    4 

    5 int main()

    6 {

    7     cout << "Midapex IniFile API 0.2.1" << endl;

    8     string name_key = "name";

    9     string age_key = "age";

   10 

   11     //using ini file path init a IniFile object

   12     IniFile ini("myconfig.ini");

   13 

   14     //set current section

   15     ini.setSection("student");

   16 

   17     if(!ini.write(name_key,"Tony"))

   18     {

   19         cout << "write name to ini file fail"<<endl;

   20         return -1;

   21     }

   22 

   23     if(!ini.write(age_key,20))

   24     {

   25         cout << "write age to ini file fail"<<endl;

   26         return -1;

   27     }

   28 

   29     cout << "[" << ini.getSection()<< "]" <<endl;

   30     cout << name_key << "=" << ini.readStr(name_key,"") << endl;

   31     cout << age_key << "=" << ini.readInt(age_key,-1) << endl;

   32 

   33     ini.setSection("teacher");

   34 

   35     if(!ini.write(name_key,"Tom"))

   36     {

   37         cout << "write name to ini file fail"<<endl;

   38         return -1;

   39     }

   40 

   41     if(!ini.write(age_key,50))

   42     {

   43         cout << "write age to ini file fail"<<endl;

   44         return -1;

   45     }

   46 

   47     cout << "[" << ini.getSection()<< "]" <<endl;

   48     cout << name_key << "=" << ini.readStr(name_key,"") << endl;

   49     cout << age_key << "=" << ini.readInt(age_key,-1) << endl;

   50 

   51     return 0;

   52 }

   53 

posted on 2007-12-09 12:12 天下无双 阅读(6395) 评论(14)  编辑 收藏 引用 所属分类: C/C++

FeedBack:
# re: 发布跨平台INI文件读写API(C++版本) V0.2.1
2007-12-11 13:45 | <a href=http://minidx.com>minidxer</a>
不错的模块,不过现在相对来说XML用得比较多一些  回复  更多评论
  
# re: 发布跨平台INI文件读写API(C++版本) V0.2.1
2007-12-12 15:30 | adonais
帮忙测试一下,好像不错的样子  回复  更多评论
  
# re: 发布跨平台INI文件读写API(C++版本) V0.2.1
2007-12-12 22:45 | 天下无双
XML复杂了一点,特别是针对于一些小型的应用来说。  回复  更多评论
  
# re: 发布跨平台INI文件读写API(C++版本) V0.2.1
2007-12-19 13:59 | Khan's Notebook
嘿嘿. 我写过类似的...
symbian, wince, linux 都有. 我blog上有代码. 可以交流下..  回复  更多评论
  
# re: 发布跨平台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;
}  回复  更多评论
  
# re: 发布跨平台INI文件读写API(C++版本) V0.2.1
2008-03-25 15:03 | 豆花鱼片
上面的代码有误, 应该这个:


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] && '\t' != p[j] && '=' != p[j])
valid = 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;

valid = j+1;
while(' ' == p[valid] || '\t' == p[valid])
valid++;
*value_s = valid;
*value_e = i;

return 1;
}
}
}
}

i++;
}
}
}
else
{
i++;
}
}
return 0;
}

  回复  更多评论
  
# re: 发布跨平台INI文件读写API(C++版本) V0.2.1
2008-04-26 16:09 | luoweiliuz
测试时发现问题,例如我在循环写
[name]
Row1=J ???????被大10 的替换
Row2=B
Row3=C
Row4=D
Row5=E
Row6=F
Row7=G
Row8=H
Row9=I
Row10=J ???????这个地方开始以后没有输出,但是覆盖了 Row1 的值
Row11=K
  回复  更多评论
  
# re: 发布跨平台INI文件读写API(C++版本) V0.2.1
2008-05-09 13:05 | wuxunfeng
谢谢楼主的共享。
有个问题想问一下。如果 INI文件中,同一个SECTION中,有几个同样的KEY,那么读取的时候,会读取哪个KEY呢,可以循环读取吗.例:
[SECTION]
key=data
key=data
key=data
key1=data2  回复  更多评论
  
# re: 发布跨平台INI文件读写API(C++版本) V0.2.1
2008-06-28 16:20 | 西红柿蛋汤
luoweiliuz提到的问题今天我们也有遇到,解决方法:

IniFile::parse_file中做如下修改: //if(strcmp(key,p+newline_start,j-newline_start)==0)
if(strncmp(key,p+newline_start,strlen(key))==0)
  回复  更多评论
  
# re: 发布跨平台INI文件读写API(C++版本) V0.2.1
2008-08-11 14:23 | Null
有两个地方需要修改,否则无法正确读写以下配置
[Test]
key=1
key2=2

[Test2]
key=3
key2=4

在 int IniFile::parse_file
//if (0 == strncmp(p + section_start, section, i - section_start))
if (i - section_start == strlen(section) && 0 == strncmp(p + section_start, section, strlen(section)))

//if(strncmp(key,p+newline_start,j-newline_start)==0)
if (j - newline_start == strlen(key) && strncmp(key, p + newline_start, strlen(key)) == 0)

原因:
原来的代码只是比较前面几个字符相同,那么就返回 0,
如上面的 [Test] 与 [Test2],前几个字符是相同的,但是 Test!=Test2
同理 key!=key2
所以,比较的时候,首先要比较长度,长度相等了,才比较内容。怎样就不会有问题了。  回复  更多评论
  
# re: 发布跨平台INI文件读写API(C++版本) V0.2.1
2009-05-08 18:21 | someone
在网上看到不少写这个功能的代码,提个问题,linux中配置多数都是ini文件,难道没有一个公用的代码库,还要每个软件都去实现一遍吗?  回复  更多评论
  
# re: 发布跨平台INI文件读写API(C++版本) V0.2.1
2013-05-29 10:42 | peach5460
能否兼容不同的字符集?我试一下  回复  更多评论
  
# re: 发布跨平台INI文件读写API(C++版本) V0.2.1
2013-09-09 20:17 | h
写入的话和当成纯txt没差。  回复  更多评论
  
# re: 发布跨平台INI文件读写API(C++版本) V0.2.1
2014-12-18 14:19 | xinyangme

在c++中
int IniFile::write_profile_string(const char *section, const char *key, const char *value, const char *file)

//out = fopen(file, 'w');

error: invalid conversion from 'char' to 'const char*' [-fpermissive]
out = fopen(file, 'r');

这是为什么呢  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



常用链接

留言簿(15)

随笔分类

随笔档案

相册

我的其它领域Blog

搜索

  •  

积分与排名

  • 积分 - 202565
  • 排名 - 128

最新评论

阅读排行榜

评论排行榜