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;
}
相关推荐
王嘉俊92518 分钟前
HarmonyOS 微服务与 OpenHarmony 开发:构建模块化与开源生态应用
微服务·开源·harmonyos·arkts·开发·鸿蒙
-森屿安年-42 分钟前
STL 容器:stack
开发语言·c++
charlee441 小时前
最小二乘问题详解6:梯度下降法
c++·梯度下降·雅可比矩阵·非线性最小二乘·参数拟合
房开民1 小时前
OpenCV C++ 中,访问图像像素三种常用方法
c++·opencv·计算机视觉
报错小能手2 小时前
C++笔记(面向对象)深赋值 浅赋值
c++·笔记·学习
Maple_land2 小时前
编译器的“隐形约定”与本地变量:解锁Linux变量体系的关键密码
linux·运维·服务器·c++·centos
OC溥哥9993 小时前
C++2D地铁跑酷代码
开发语言·c++
紫荆鱼3 小时前
设计模式-状态模式(State)
c++·后端·设计模式·状态模式
CoderJia程序员甲3 小时前
GitHub 热榜项目 - 日榜(2025-10-27)
ai·开源·大模型·github·ai教程
深思慎考3 小时前
微服务即时通讯系统(服务端)——Speech 语音模块开发(2)
linux·c++·微服务·云原生·架构·语音识别·聊天室项目