C/C++:使用inih开源项目实现读取ini文件内容
1、inih项目下载地址https://github.com/benhoyt/inih2、使用说明它主要包含以下几个比较重要的文件:ini.c/ini.h:C语言解析ini文件的实现;cpp目录下的INIReader.cpp/INIReader.h:C++解析ini文件的实现;examples目录下的test.ini/ini_example.c/INIReaderExample.cpp:C/C++
·
1、inih项目下载地址
https://github.com/benhoyt/inih
2、使用说明
它主要包含以下几个比较重要的文件:
- ini.c/ini.h:C语言解析ini文件的实现;
- cpp目录下的INIReader.cpp/INIReader.h:C++解析ini文件的实现;
- examples目录下的test.ini/ini_example.c/INIReaderExample.cpp:C/C++使用示例。
根据README.md描述,项目是用C语言编写的ini文件解析器,只有几页代码,设计的比较小而且简单,所以对嵌入式系统中非常有用。此外,编译时还提供了一些自定义的宏选项,可以根据自己的需求来选择性地设置,更多详细信息参考README.md。
但是需要注意的一点是,该项目只是提供读取ini文件内容的接口,没有提供修改写入的函数接口。(inih-r53版本注)
3、参考示例
① 配置文件(examples/test.ini)
; Test config file for ini_example.c and INIReaderTest.cpp
[protocol] ; Protocol configuration
version=6 ; IPv6
[user]
name = Bob Smith ; Spaces around '=' are stripped
email = bob@smith.com ; And comments (like this) ignored
active = true ; Test a boolean
pi = 3.14159 ; Test a floating point number
② C语言示例(examples/ini_example.c)
/* Example: parse a simple configuration file */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../ini.h"
typedef struct
{
int version;
const char* name;
const char* email;
} configuration;
static int handler(void* user, const char* section, const char* name,
const char* value)
{
configuration* pconfig = (configuration*)user;
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
if (MATCH("protocol", "version")) {
pconfig->version = atoi(value);
} else if (MATCH("user", "name")) {
pconfig->name = strdup(value);
} else if (MATCH("user", "email")) {
pconfig->email = strdup(value);
} else {
return 0; /* unknown section/name, error */
}
return 1;
}
int main(int argc, char* argv[])
{
configuration config;
if (ini_parse("test.ini", handler, &config) < 0) {
printf("Can't load 'test.ini'\n");
return 1;
}
printf("Config loaded from 'test.ini': version=%d, name=%s, email=%s\n",
config.version, config.name, config.email);
free((void*)config.name);
free((void*)config.email);
return 0;
}
输出结果:
Config loaded from 'test.ini': version=6, name=Bob Smith, email=bob@smith.com
③ C++示例(examples/INIReaderExample.cpp)
// Example that shows simple usage of the INIReader class
#include <iostream>
#include "../cpp/INIReader.h"
int main()
{
INIReader reader("../examples/test.ini");
if (reader.ParseError() < 0) {
std::cout << "Can't load 'test.ini'\n";
return 1;
}
std::cout << "Config loaded from 'test.ini': version="
<< reader.GetInteger("protocol", "version", -1) << ", name="
<< reader.Get("user", "name", "UNKNOWN") << ", email="
<< reader.Get("user", "email", "UNKNOWN") << ", pi="
<< reader.GetReal("user", "pi", -1) << ", active="
<< reader.GetBoolean("user", "active", true) << "\n";
std::cout << "Has values: user.name=" << reader.HasValue("user", "name")
<< ", user.nose=" << reader.HasValue("user", "nose") << "\n";
std::cout << "Has sections: user=" << reader.HasSection("user")
<< ", fizz=" << reader.HasSection("fizz") << "\n";
return 0;
}
输出结果:
Config loaded from 'test.ini': version=6, name=Bob Smith, email=bob@smith.com, pi=3.14159, active=1
Has values: user.name=1, user.nose=0
Has sections: user=1, fizz=0
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献5条内容
所有评论(0)