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;
}
相关推荐
William_wL_9 分钟前
【C++】类和对象(下)
c++
William_wL_1 小时前
【C++】内存管理
c++
星星火柴9361 小时前
笔记 | C++面向对象高级开发
开发语言·c++·笔记·学习
DolphinScheduler社区1 小时前
真实迁移案例:从 Azkaban 到 DolphinScheduler 的选型与实践
java·大数据·开源·任务调度·azkaban·海豚调度·迁移案例
悲伤小伞2 小时前
Linux_Socket_UDP
linux·服务器·网络·c++·网络协议·udp
八个程序员2 小时前
自定义函数(C++)
开发语言·c++·算法
猫头虎3 小时前
昆仑芯 X HAMi X 百度智能云 | 昆仑芯 P800 XPU/vXPU 双模式算力调度方案落地
人工智能·百度·开源·aigc·文心一言·gpu算力·agi
微露清风4 小时前
系统性学习C++-第十讲-stack 和 quene
java·c++·学习
报错小能手4 小时前
C++笔记(面向对象)静态联编和动态联编
开发语言·c++·算法
WBluuue4 小时前
AtCoder Beginner Contest 430(ABCDEF)
c++·算法