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文件里面的内容被成功解析出来了:

相关推荐
施努卡机器视觉4 小时前
SNK施努卡铜箔包装线:从拔轴到入库,全流程自动化怎么实现?
运维·自动化
treesforest5 小时前
2026年,IP地理位置精准查询的几个硬核技术变化
运维·网络·网络协议·tcp/ip·ip
工业机器人销售服务5 小时前
在珠宝首饰加工中,遨博协作机器人配合微力控技术,实现宝石的自动化镶嵌
运维·机器人·自动化
JuiceFS5 小时前
降低数据存储成本:JuiceFS v1.4 分层存储设计解析
运维·后端
运维行者_5 小时前
云计算连接性与互操作性
服务器·开发语言·网络·web安全·网络基础设施
红茶要加冰5 小时前
linux的例行性工作——计划任务
linux·运维·服务器
byxdaz5 小时前
Linux中查看硬件信息
linux·运维
darkdragonking5 小时前
由一次构建 OpenEuler 22.03 dnf源所了解到的
linux·运维·服务器
米高梅狮子5 小时前
Ceph 分布式存储 部署
linux·运维·数据库·分布式·ceph·docker·华为云
WUYOUGYLU5 小时前
云服务器怎么选、怎么用,才不花冤枉钱
运维·服务器