什么是最简单的方法来分析INI文件中的C++?
题
我试图分析INI文件,使用C++。任何什么技巧,是最好的方式来实现这一目标?我应该使用的Windows API工具INI文件处理(与我完全陌生的),开放源码解决方案或试图进行分析手动?
解决方案
你可以使用的Windows API功能,例如 GetPrivateProfileString() 和 GetPrivateProfileInt().
其他提示
如果你需要一个交叉平台方案,尽量提高的 程序的选择 图书馆。
我从来没有分析ini文件,所以我不能太具体关于这个问题。
但我有一个建议:
不要重新发明车轮,只要一个现有满足您的要求
http://en.wikipedia.org/wiki/INI_file#Accessing_INI_files
http://sdl-cfg.sourceforge.net/
http://sourceforge.net/projects/libini/
http://www.codeproject.com/KB/files/config-file-parser.aspx
祝你好运:)
我用 SimpleIni.它的跨平台。
如果你已经在使用脱
QSettings my_settings("filename.ini", QSettings::IniFormat);
然后读取值
my_settings.value("GroupName/ValueName", <<DEFAULT_VAL>>).toInt()
还有一些其他的转换,转换你INI价值观纳入这两个标准类型和脱类型。看看脱文件QSettings的更多信息。
这个问题是有点老了,但我将我的答案。我已经测试了各种INI类(你可以看到他们上我 网站)和我还使用simpleIni因为我想要的工作INI文件,在windows和退缩。窗口的GetPrivateProfileString()仅适用于注册表上退缩。
这是非常容易阅读simpleIni.这里是一个例子:
#include "SimpleIni\SimpleIni.h"
CSimpleIniA ini;
ini.SetUnicode();
ini.LoadFile(FileName);
const char * pVal = ini.GetValue(section, entry, DefaultStr);
inih 是一个简单的ini parser写在C、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;非常星等语法。我喜欢它在XML配置文件。
如果你有兴趣的平台便携性,你也可以尝试提升。PropertyTree.它支持ini为persistancy格式,虽然财产树我要1级别深刻的只。
除非你打算使该应用程序的跨平台,使用的Windows API调将是最好的路要走。只是忽视注意在API文件关于是只提供了16位的程序兼容性。
也许晚回答..但是,值得了解的选择。。如果你需要一个交叉平台方案,肯定你可以试试能说会道,,其有趣..(https://developer.gnome.org/glib/stable/glib-Key-value-file-parser.html)
我知道这个问题是非常古老的,但是我来到这因为我需要的东西交叉平台,为linux,win32...我写的功能如下,它是一个单一的功能,可以分析INI文件,希望其他人将会发现它很有用。
规则和注意事项:buf分析必须是一个空终止串。载你ini file into char列串并呼吁这个函数分析。部分姓名必须有[]括号,如此[MySection],也值和部必须开始在一条线上没有领先的空间。它将分析文件,与Windows 或与Linux 线的结局。评论应使用#/或/和开始在顶部的文件,没有评论应该与INI entry数据。报价和蜱调整从两端的返回串。空间仅仅是修剪,如果他们在外面的报价。串不需要有报价,空格调整,如果报价是失踪。你还可以提取数字或其他数据,例如如果你有一个漂浮只是执行atof(ret)在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
}