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

相关推荐
cyber_两只龙宝29 分钟前
Nginx--企业高性能web服务器高级配置详解
linux·运维·nginx·云原生
如若1232 小时前
AutoDL云服务器 NVIDIA 570驱动 EGL渲染修复全记录
运维·服务器·python
i建模2 小时前
Omarchy设置防火墙
linux·运维
晚秋大魔王2 小时前
泰拉瑞亚手机版服务器部署
运维·服务器·泰拉瑞亚
敲代码的哈吉蜂2 小时前
高可用集群Keepalived
运维·服务器·网络·数据库
S-码农3 小时前
Linux ——条件变量
linux·开发语言
爱写代码的liding3 小时前
linux安装软件过程中报找不到某些动态链接.so文件
linux
青衫码上行3 小时前
【项目部署】Spring Boot项目部署的四种方式
java·linux·服务器·spring boot·后端·docker·腾讯云
盟接之桥3 小时前
盟接之桥说制造:从客供的外在共生到内在的身心合一
运维·服务器·网络·人工智能·制造
火车叼位3 小时前
让 SSL 证书管理更简单:k ssl 命令实战手册
运维