inih开源库使用

源码下载:GitHub - benhoyt/inih: Simple .INI file parser in C, good for embedded systems

1.使用说明

它主要包含以下几个比较重要的文件:

  • ini.c/ini.h:C语言解析ini文件的实现;
  • cpp目录下的INIReader.cpp/INIReader.h:C++解析ini文件的实现;
  • examples目录下的test.ini/ini_example.c/INIReaderExample.cpp:C/C++使用示例。
2.C++示例

ini文件如下所示:

复制代码
[protocol]
version=6

[user]
name = Bob Smith
email = bob@smith.com
active = true
pi = 3.14159

代码示例如下:

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;
}
3.C语言示例

ini文件如下所示:

cpp 复制代码
[protocol]
version=6

[user]
name = Bob Smith
email = bob@smith.com
active = true
pi = 3.14159

代码示例如下:

cpp 复制代码
/* 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;
}
相关推荐
狂师3 分钟前
我开发了一款免费开源的测试开发刷题学习网站
面试·开源·测试
A7bert7774 分钟前
【MobileSAM部署至RK3588】环境适配→转换RKNN→开发板部署
c++·人工智能·深度学习·目标检测·ubuntu
青少儿编程课堂20 分钟前
蓝桥杯·全国青少年信息素养大赛·电子学会C/C++考级:分层图最短路解析
c++·python·算法·bfs·信息学竞赛
CServer_0139 分钟前
开源!汽车零部件仓储的数字孪生协同方案
物联网·开源·汽车·制造
分布式存储与RustFS42 分钟前
K8s 集群备份落到自建对象存储:Velero 接 RustFS 配置指南
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
miofly1 小时前
GitHub 日榜趋势速报 | 2026-11-04
开源·github
1024奇点9 小时前
【仓颉语言入门 · 第二篇 第 22 课】
开发语言·ide·开源
FlightYe11 小时前
视界原理之3D视频(四):裸眼、眼镜和头显
android·linux·c++·3d·ffmpeg·音视频
by2099912 小时前
C++ 智能指针的所有权与使用
开发语言·c++·经验分享
冬奇Lab12 小时前
一天一个开源项目(第252篇):Lightpanda —— 用 Zig 从零写的 AI 专用无头浏览器,内存省 16 倍、速度快 9 倍
人工智能·开源·资讯