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;
}
相关推荐
阿巴~阿巴~43 分钟前
JsonCpp:C++ JSON处理利器
linux·网络·c++·json·tcp·序列化和反序列化
Codebee1 小时前
深度解析AI编程技术:从原理到实践,手把手教你落地
人工智能·设计模式·开源
zmzb01032 小时前
C++课后习题训练记录Day38
开发语言·c++
獭.獭.2 小时前
C++ -- STL【string的使用】
c++·string·auto
福尔摩斯张2 小时前
Linux进程间通信(IPC)机制深度解析与实践指南
linux·运维·服务器·数据结构·c++·算法
lijiatu100862 小时前
C++ 类成员变量声明语法错误
java·开发语言·c++
zore_c2 小时前
【C语言】带你层层深入指针——指针详解2
c语言·开发语言·c++·经验分享·笔记
cookies_s_s3 小时前
项目--协程库(C++)前置知识篇
linux·服务器·c++
zmzb01033 小时前
C++课后习题训练记录Day39
数据结构·c++·算法
qq_310658513 小时前
mediasoup源码走读(二)环境搭建与 Demo 运行
服务器·c++·音视频