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;
}
相关推荐
StrokeAce1 小时前
linux桌面软件(wps)内嵌到主窗口后的关闭问题
linux·c++·qt·wps·窗口内嵌
家有狸花4 小时前
VSCODE驯服日记(三):配置C++环境
c++·ide·vscode
dengqingrui1235 小时前
【树形DP】AT_dp_p Independent Set 题解
c++·学习·算法·深度优先·图论·dp
C++忠实粉丝5 小时前
前缀和(8)_矩阵区域和
数据结构·c++·线性代数·算法·矩阵
ZZZ_O^O5 小时前
二分查找算法——寻找旋转排序数组中的最小值&点名
数据结构·c++·学习·算法·二叉树
小飞猪Jay8 小时前
C++面试速通宝典——13
jvm·c++·面试
rjszcb9 小时前
一文说完c++全部基础知识,IO流(二)
c++
威桑9 小时前
记一次控件提升后,运行却不显示的Bug
qt
小字节,大梦想9 小时前
【C++】二叉搜索树
数据结构·c++
吾名招财10 小时前
yolov5-7.0模型DNN加载函数及参数详解(重要)
c++·人工智能·yolo·dnn