VC列挙プロファイルのSection格納Map
コンフィギュレーションファイルの横行する開発任務、各種のコンフィギュレーションファイルは本当にうんざりして、しかしまた仕方がありません!
必要:
コンフィギュレーションファイルのいずれかのSection列挙をSTLのMAPに格納し、後続の操作を行う.
実装:
GetPrivateProfileSectionを使用してSectionの下のすべての値を取得し、文字列処理を使用してMAPに格納します.
関数のプロトタイプは次のとおりです(Unicodeバージョンと非Unicodeバージョンに分けられます).
GetPrivateProfileSection(LPCSTR lpAppName,//section名LPSTR lpReturnedString,//戻り文字列キャッシュDWORD nSize,//キャッシュサイズLPCSTR lpFileName//プロファイルパス);
実際に読み込まれた文字数を返します.
注意:GetPrivateProfileSection関数で読み出すSectionはlpReturnedStringに文字列として格納され、キー値ペア間のスペースがフィルタリングされ、改行文字は'0'で置き換えられ、末尾は'00'.ここは問題が起こりやすい.
プロファイルを仮定します.iniの内容は以下の通りです.
[Section] key1=value1 key2=value2 key3=value3 key4=value4 key5=value5
次のようになります.
GetPrivateProfileSectionNameの使い方をまとめます.
GetPrivateProfileSectionNameの機能は、プロファイル内のすべてのSectionの名前を列挙することです.関数のプロトタイプは次のとおりです.
GetPrivateProfileSectionNamesA(LPSTR lpszReturnBuffer,//格納名バッファDWORD nSize,//バッファサイズLPCSTR lpFileName//プロファイル名);
注意:読み出したSection名は同様にlpReturnedStringに文字列として格納、キー値ペア間のスペースはフィルタリングされ、改行文字は'0'で置換、末尾は'00'.ここは問題が起こりやすい.
使用例:
必要:
コンフィギュレーションファイルのいずれかのSection列挙をSTLのMAPに格納し、後続の操作を行う.
実装:
GetPrivateProfileSectionを使用してSectionの下のすべての値を取得し、文字列処理を使用してMAPに格納します.
関数のプロトタイプは次のとおりです(Unicodeバージョンと非Unicodeバージョンに分けられます).
GetPrivateProfileSection(LPCSTR lpAppName,//section名LPSTR lpReturnedString,//戻り文字列キャッシュDWORD nSize,//キャッシュサイズLPCSTR lpFileName//プロファイルパス);
実際に読み込まれた文字数を返します.
注意:GetPrivateProfileSection関数で読み出すSectionはlpReturnedStringに文字列として格納され、キー値ペア間のスペースがフィルタリングされ、改行文字は'0'で置き換えられ、末尾は'00'.ここは問題が起こりやすい.
プロファイルを仮定します.iniの内容は以下の通りです.
[Section] key1=value1 key2=value2 key3=value3 key4=value4 key5=value5
次のようになります.
map<int,string>strMap;
void StrReplace(string src,string s)
{
string::size_type pos=0;
string::size_type start_pos = 0;
// s src
while((pos=src.find_first_of(s,pos))!=string::npos)
{
string temp = src.substr(start_pos,pos-start_pos);
string::size_type n_pos = 0;
// '=' src
while((n_pos=temp.find_first_of('=',n_pos)) != string::npos)
{
string key,value;
key = temp.substr(0,n_pos);
value = temp.substr(n_pos+1,temp.length());
n_pos++;
strMap.insert(make_pair<int,string>(atoi(key.c_str()),value));
}
start_pos = (++pos);
}
}
int ReadPaperTypeProperty()
{
char chSection[1024] = {0};
int nSize = GetPrivateProfileSection("Section",chSection,1024,"property.ini");
// '\0'
while (strlen(chSection) < nSize)
{
chSection[strlen(chSection)] = ',';
}
string strSection = chSection;
StrReplace(strSection,",");
return 0;
}
GetPrivateProfileSectionNameの使い方をまとめます.
GetPrivateProfileSectionNameの機能は、プロファイル内のすべてのSectionの名前を列挙することです.関数のプロトタイプは次のとおりです.
GetPrivateProfileSectionNamesA(LPSTR lpszReturnBuffer,//格納名バッファDWORD nSize,//バッファサイズLPCSTR lpFileName//プロファイル名);
注意:読み出したSection名は同様にlpReturnedStringに文字列として格納、キー値ペア間のスペースはフィルタリングされ、改行文字は'0'で置換、末尾は'00'.ここは問題が起こりやすい.
使用例:
int GetSectionNames()
{
char chSectionNames[1024] = {0};
int nSize = GetPrivateProfileSectionNames(chSectionNames,sizeof(chSectionNames),property.ini" );
// '\0'
while (strlen(chSectionNames) < nSize)
{
chSectionNames[strlen(chSectionNames)] = ',';
}
MessageBox(NULL,chSectionNames,"",MB_OK);
return nSize;
}