Linux下inih库的使用

一、inih库简介

inih(INI Not Invented Here)是一个用C语言编写的简单.INI文件解析器。它仅有几页代码,设计上力求精简轻巧,因此特别适合嵌入式系统。该工具基本兼容Python ConfigParser风格的.INI文件格式,包括RFC 822风格的多行语法和name:value条目。其github地址为:https://github.com/benhoyt/inih

二、下载inih源码

https://github.com/benhoyt/inih/releases 下载inih源码:

进入源码目录,可以看到其目录结构如下:

三、inih的使用例程

inih源码目录的examples目录下存放着其C语言和C++的使用例程:

以C++为例,编写一个解析test.ini文件的示例。test.ini文件的内容如下:

bash 复制代码
; 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
trillion = 1000000000000 ; Test 64-bit integers

INIReaderExample.cpp:

cpp 复制代码
#include <iostream>
#include "INIReader.h"

int main()
{
    INIReader reader("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) << ", unsigned version="
              << reader.GetUnsigned("protocol", "version", -1) << ", trillion="
              << reader.GetInteger64("user", "trillion", -1) << ", unsigned trillion="
              << reader.GetUnsigned64("user", "trillion", -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";

    std::cout << "Sections:\n";
    std::vector<std::string> sections = reader.Sections();
    for (std::vector<std::string>::const_iterator it = sections.begin(); it != sections.end(); ++it) {
        std::cout << "- " << *it << "\n";
    }

    for (std::vector<std::string>::const_iterator it = sections.begin(); it != sections.end(); ++it) {
        std::cout << "Keys in section [" << *it << "]:\n";
        std::vector<std::string> keys = reader.Keys(*it);
        for (std::vector<std::string>::const_iterator kit = keys.begin(); kit != keys.end(); ++kit) {
            std::cout << "- " << *kit << "\n";
        }
    }

    return 0;
}

编译:

bash 复制代码
g++ INIReaderExample.cpp ./Inih/ini.c ./Inih/INIReader.cpp -o main -g -I./ -I./Inih

运行,效果如下。可以看到test.ini文件里面的内容被成功解析出来了:

相关推荐
fengyehongWorld7 分钟前
TeraTerm ttl脚本登录wsl
linux·teraterm
乌托邦的逃亡者1 小时前
Linux中如何检测IP冲突
linux·运维·tcp/ip
一曦的后花园1 小时前
linux搭建promethes并对接node-exporter指标
linux·运维·服务器
乌托邦的逃亡者1 小时前
CentOS/Openeuler主机中,为一个网卡设置多个IP地址
linux·运维·网络·tcp/ip·centos
拾贰_C2 小时前
【OpenClaw | openai | QQ】 配置QQ qot机器人
运维·人工智能·ubuntu·面试·prompt
桌面运维家2 小时前
服务器进程异常监控:快速定位与排障实战指南
运维·服务器
@CLoudbays_Martin112 小时前
UniApp是否能够接入SDK游戏盾呢?
服务器·网络·网络协议·tcp/ip·安全
念恒123062 小时前
进程控制---自定义Shell
linux·c语言
风曦Kisaki3 小时前
# Linux Shell 编程入门 Day02:条件测试、if 判断、循环与随机数
linux·运维·chrome
木雷坞3 小时前
视觉算法环境 Docker 镜像拉取失败排查
运维·人工智能·docker·容器