質問

C++を使用してINIファイルを解析しようとしています。これを達成するための最良の方法について何かヒントはありますか?INI ファイル処理には Windows API ツール (私にはまったく馴染みがありません)、オープンソース ソリューションを使用するべきでしょうか、それとも手動で解析してみるべきでしょうか?

役に立ちましたか?

解決

次のような Windows API 関数を使用できます。 GetPrivateProfileString() そして GetPrivateProfileInt().

他のヒント

クロスプラットフォームのソリューションが必要な場合は、Boost のソリューションを試してください。 プログラムオプション 図書館。

私は ini ファイルを解析したことがないので、この問題についてはあまり詳しく言えません。
しかし、私からのアドバイスが 1 つあります。
既存のホイールが要件を満たしている限り、ホイールを再発明する必要はありません

http://en.wikipedia.org/wiki/INI_file#INI_files へのアクセス
http://sdl-cfg.sourceforge.net/
http://sourceforge.net/projects/libini/
http://www.codeproject.com/KB/files/config-file-parser.aspx

幸運を :)

私が使う シンプルイニ. 。クロスプラットフォームです。

すでに Qt を使用している場合

QSettings my_settings("filename.ini", QSettings::IniFormat);

次に値を読み取ります

my_settings.value("GroupName/ValueName", <<DEFAULT_VAL>>).toInt()

INI 値を標準型と Qt 型の両方に変換するコンバーターは他にもたくさんあります。詳細については、QSettings の Qt ドキュメントを参照してください。

この質問は少し古いですが、回答を投稿します。さまざまな INI クラスをテストしました (これらは、 Webサイト) また、Windows と winCE の両方で INI ファイルを操作したいので、simpleIni も使用します。Windows の GetPrivateProfileString() は、winCE 上のレジストリでのみ機能します。

simpleIni を使用すると非常に読みやすくなります。以下に例を示します。

#include "SimpleIni\SimpleIni.h"    
CSimpleIniA ini;
ini.SetUnicode();
ini.LoadFile(FileName);
const char * pVal = ini.GetValue(section, entry, DefaultStr);

イニ は C で書かれた単純な ini パーサーであり、C++ ラッパーも付属しています。使用例:

#include "INIReader.h"    

INIReader reader("test.ini");

std::cout << "version="
          << reader.GetInteger("protocol", "version", -1) << ", name="
          << reader.Get("user", "name", "UNKNOWN") << ", active="
          << reader.GetBoolean("user", "active", true) << "\n";

著者は既存のライブラリのリストも持っています ここ.

やってみました libconfig;非常に JSON に似た構文です。私は XML 設定ファイルよりもこれを好みます。

プラットフォームの移植性に興味がある場合は、Boost.PropertyTree を試すこともできます。永続フォーマットとして ini をサポートしますが、プロパティ ツリーの深さは 1 レベルのみです。

アプリをクロスプラットフォームにする予定がない限り、Windows API 呼び出しを使用するのが最善の方法です。16 ビット アプリとの互換性のためにのみ提供されているという API ドキュメントの注記は無視してください。

たぶん遅い答えかもしれません..しかし、オプションを知る価値があります..クロスプラットフォームソリューションが必要な場合は、間違いなくGLIBを試すことができます、興味深いです..(https://developer.gnome.org/glib/stable/glib-Key-value-file-parser.html)

この質問が非常に古いことは知っていますが、Linux、win32 のクロスプラットフォームのものが必要だったので、この質問にたどり着きました。以下の関数を書きました。これは INI ファイルを解析できる単一の関数です。他の人にも役立つと思います。

ルールと注意事項:解析する buf は NULL で終了する文字列である必要があります。ini ファイルを char 配列文字列にロードし、この関数を呼び出して解析します。この [MySection] のように、セクション名は [] 括弧で囲む必要があります。また、値とセクションは先頭にスペースのない行で始まる必要があります。Windows または Linux の行末を使用してファイルを解析します。コメントは # または // を使用し、ファイルの先頭から始める必要があります。コメントを INI エントリ データと混合しないでください。引用符とティックは、返される文字列の両端から切り取られます。スペースは引用符の外側にある場合にのみトリミングされます。文字列に引用符を付ける必要はありません。引用符がない場合は空白が切り取られます。たとえば、float がある場合は、ret バッファーで atof(ret) を実行するだけで、数値やその他のデータを抽出することもできます。

//  -----note: no escape is nessesary for inner quotes or ticks-----
//  -----------------------------example----------------------------
//  [Entry2]
//  Alignment   = 1
//  LightLvl=128
//  Library     = 5555
//  StrValA =  Inner "quoted" or 'quoted' strings are ok to use
//  StrValB =  "This a "quoted" or 'quoted' String Value"
//  StrValC =  'This a "tick" or 'tick' String Value'
//  StrValD =  "Missing quote at end will still work
//  StrValE =  This is another "quote" example
//  StrValF =  "  Spaces inside the quote are preserved "
//  StrValG =  This works too and spaces are trimmed away
//  StrValH =
//  ----------------------------------------------------------------
//12oClocker super lean and mean INI file parser (with section support)
//set section to 0 to disable section support
//returns TRUE if we were able to extract a string into ret value
//NextSection is a char* pointer, will be set to zero if no next section is found
//will be set to pointer of next section if it was found.
//use it like this... char* NextSection = 0;  GrabIniValue(X,X,X,X,X,&NextSection);
//buf is data to parse, ret is the user supplied return buffer
BOOL GrabIniValue(char* buf, const char* section, const char* valname, char* ret, int retbuflen, char** NextSection)
{
    if(!buf){*ret=0; return FALSE;}

    char* s = buf; //search starts at "s" pointer
    char* e = 0;   //end of section pointer

    //find section
    if(section)
    {
        int L = strlen(section);
        SearchAgain1:
        s = strstr(s,section); if(!s){*ret=0; return FALSE;}    //find section
        if(s > buf && (*(s-1))!='\n'){s+=L; goto SearchAgain1;} //section must be at begining of a line!
        s+=L;                                                   //found section, skip past section name
        while(*s!='\n'){s++;} s++;                              //spin until next line, s is now begining of section data
        e = strstr(s,"\n[");                                    //find begining of next section or end of file
        if(e){*e=0;}                                            //if we found begining of next section, null the \n so we don't search past section
        if(NextSection)                                         //user passed in a NextSection pointer
        { if(e){*NextSection=(e+1);}else{*NextSection=0;} }     //set pointer to next section
    }

    //restore char at end of section, ret=empty_string, return FALSE
    #define RESTORE_E     if(e){*e='\n';}
    #define SAFE_RETURN   RESTORE_E;  (*ret)=0;  return FALSE

    //find valname
    int L = strlen(valname);
    SearchAgain2:
    s = strstr(s,valname); if(!s){SAFE_RETURN;}             //find valname
    if(s > buf && (*(s-1))!='\n'){s+=L; goto SearchAgain2;} //valname must be at begining of a line!
    s+=L;                                                   //found valname match, skip past it
    while(*s==' ' || *s == '\t'){s++;}                      //skip spaces and tabs
    if(!(*s)){SAFE_RETURN;}                                 //if NULL encounted do safe return
    if(*s != '='){goto SearchAgain2;}                       //no equal sign found after valname, search again
    s++;                                                    //skip past the equal sign
    while(*s==' '  || *s=='\t'){s++;}                       //skip spaces and tabs
    while(*s=='\"' || *s=='\''){s++;}                       //skip past quotes and ticks
    if(!(*s)){SAFE_RETURN;}                                 //if NULL encounted do safe return
    char* E = s;                                            //s is now the begining of the valname data
    while(*E!='\r' && *E!='\n' && *E!=0){E++;} E--;         //find end of line or end of string, then backup 1 char
    while(E > s && (*E==' ' || *E=='\t')){E--;}             //move backwards past spaces and tabs
    while(E > s && (*E=='\"' || *E=='\'')){E--;}            //move backwards past quotes and ticks
    L = E-s+1;                                              //length of string to extract NOT including NULL
    if(L<1 || L+1 > retbuflen){SAFE_RETURN;}                //empty string or buffer size too small
    strncpy(ret,s,L);                                       //copy the string
    ret[L]=0;                                               //null last char on return buffer
    RESTORE_E;
    return TRUE;

    #undef RESTORE_E
    #undef SAFE_RETURN
}

使い方...例....

char sFileData[] = "[MySection]\r\n"
"MyValue1 = 123\r\n"
"MyValue2 = 456\r\n"
"MyValue3 = 789\r\n"
"\r\n"
"[MySection]\r\n"
"MyValue1 = Hello1\r\n"
"MyValue2 = Hello2\r\n"
"MyValue3 = Hello3\r\n"
"\r\n";
char str[256];
char* sSec = sFileData;
char secName[] = "[MySection]"; //we support sections with same name
while(sSec)//while we have a valid sNextSec
{
    //print values of the sections
    char* next=0;//in case we dont have any sucessful grabs
    if(GrabIniValue(sSec,secName,"MyValue1",str,sizeof(str),&next)) { printf("MyValue1 = [%s]\n",str); }
    if(GrabIniValue(sSec,secName,"MyValue2",str,sizeof(str),0))     { printf("MyValue2 = [%s]\n",str); }
    if(GrabIniValue(sSec,secName,"MyValue3",str,sizeof(str),0))     { printf("MyValue3 = [%s]\n",str); }
    printf("\n");
    sSec = next; //parse next section, next will be null if no more sections to parse
}

結局、このスレッドでは言及されていないinippを使用しました。

https://github.com/mcmtroffaes/inipp

MIT ライセンスのヘッダーのみの実装で、プロジェクトに追加するのに十分簡単で、使用するのは 4 行でした。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top