• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 45,  comments - 232,  trackbacks - 0
            1工程:跨平臺INI文件讀寫API(C++版本)
            版本: 0.2.1
            授權方式:GNU GPL
            著作權所有(c) 2007 Midapex
                本程序為自由軟件;您可依據自由軟件基金會所發表的GNU通用公共授權條款規定,就本程序再為發布與/或修改;無論您依據的是本授權的第二版或(您自行選擇的)任一日后發行的版本。
               本程序是基于使用目的而加以發布,然而不負任何擔保責任;亦無對適售性或特定目的適用性所為的默示性擔保。詳情請參照GNU通用公共授權。
            源代碼下載地址:http://www.shnenglu.com/Files/dyj057/IniFileCppV0.2.1.zip
            描述:
            應大家要求,發布C++版本,它是對C語言版本的簡單封裝,更方便大家使用。

            C語言版本地址:http://www.shnenglu.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 天下無雙 閱讀(6564) 評論(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');

            這是為什么呢  回復  更多評論
              

            常用鏈接

            留言簿(15)

            隨筆分類

            隨筆檔案

            相冊

            我的其它領域Blog

            搜索

            •  

            積分與排名

            • 積分 - 205756
            • 排名 - 130

            最新評論

            閱讀排行榜

            評論排行榜

            久久一区二区三区免费| 精品久久久久久无码人妻蜜桃| 久久国产AVJUST麻豆| 久久精品免费全国观看国产| 午夜精品久久久久久久| 国产美女久久久| 要久久爱在线免费观看| 久久久久亚洲av无码专区导航| 久久se这里只有精品| 亚洲午夜久久久久久久久久| 91久久精品电影| 欧美日韩精品久久久免费观看| 狠狠色丁香久久婷婷综| 久久精品国产亚洲AV忘忧草18| 国内精品久久久久久野外| 亚洲天堂久久久| 精品久久久久久久久久中文字幕| 无码专区久久综合久中文字幕| 久久久久亚洲AV成人网人人软件| 亚洲国产精品18久久久久久| 亚洲国产精品无码久久久久久曰| 久久99国产精品99久久 | 国产精品久久久亚洲| 国产日韩欧美久久| 国产精品久久久久久久| 国产亚洲美女精品久久久2020| 久久综合九色欧美综合狠狠 | 久久久久久久尹人综合网亚洲| 久久久久国产视频电影| 99精品久久精品一区二区| 亚洲色欲久久久综合网| 欧美国产成人久久精品| 久久亚洲高清综合| 久久婷婷五月综合色99啪ak| 国产精品欧美亚洲韩国日本久久| 免费观看久久精彩视频| 久久综合综合久久97色| 伊人久久大香线蕉精品| 久久精品亚洲欧美日韩久久| 国产激情久久久久影院老熟女免费 | 欧美色综合久久久久久|