tomlc99开源库使用

下载地址:GitHub - cktan/tomlc99: TOML C library

1.加载tomlc99库

只需要在工程当中添加toml.h / toml.c这两个文件就可以了

2.使用tomlc99库解析toml文件

以下是从文件中获取值的常用步骤:

  1. 解析 TOML 文件。
  2. 遍历并找到 TOML 中的表。
  3. 从表中提取值。
  4. 释放分配的内存。

下面是解析示例表中的值的示例。

cpp 复制代码
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "toml.h"

static void error(const char* msg, const char* msg1)
{
    fprintf(stderr, "ERROR: %s%s\n", msg, msg1?msg1:"");
    exit(1);
}


int main()
{
    FILE* fp;
    char errbuf[200];

    // 1. Read and parse toml file
    fp = fopen("sample.toml", "r");
    if (!fp) {
        error("cannot open sample.toml - ", strerror(errno));
    }

    toml_table_t* conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
    fclose(fp);

    if (!conf) {
        error("cannot parse - ", errbuf);
    }

    // 2. Traverse to a table.
    toml_table_t* server = toml_table_in(conf, "server");
    if (!server) {
        error("missing [server]", "");
    }

    // 3. Extract values
    toml_datum_t host = toml_string_in(server, "host");
    if (!host.ok) {
        error("cannot read server.host", "");
    }

    toml_array_t* portarray = toml_array_in(server, "port");
    if (!portarray) {
        error("cannot read server.port", "");
    }

    printf("host: %s\n", host.u.s);
    printf("port: ");
    for (int i = 0; ; i++) {
        toml_datum_t port = toml_int_at(portarray, i);
        if (!port.ok) break;
        printf("%d ", (int)port.u.i);
    }
    printf("\n");

    // 4. Free memory
    free(host.u.s);
    toml_free(conf);
    return 0;
}
3.访问表内容

TOML 表是使用字符串键进行查找的字典。在一般情况下,表上的所有访问函数都命名为 toml_*_in(...);在正常情况下,您知道密钥及其内容类型,并且可以进行检索使用以下函数之一:

cpp 复制代码
toml_string_in(tab, key);
toml_bool_in(tab, key);
toml_int_in(tab, key);
toml_double_in(tab, key);
toml_timestamp_in(tab, key);
toml_table_in(tab, key);
toml_array_in(tab, key);

您还可以使用整数索引查询表中的键:

cpp 复制代码
toml_table_t* tab = toml_parse_file(...);
for (int i = 0; ; i++) {
    const char* key = toml_key_in(tab, i);
    if (!key) break;
    printf("key %d: %s\n", i, key);
}
4.访问阵列内容

可以使用整数索引对 TOML 数组进行去引用。通常,数组上的所有访问方法都命名为 toml_*_at();要获取数组的大小,请执行以下操作:

cpp 复制代码
int size = toml_array_nelem(arr);

若要获取数组的内容,请使用有效的索引并调用以下函数之一:

cpp 复制代码
toml_string_at(arr, idx);
toml_bool_at(arr, idx);
toml_int_at(arr, idx);
toml_double_at(arr, idx);
toml_timestamp_at(arr, idx);
toml_table_at(arr, idx);
toml_array_at(arr, idx);
相关推荐
5ycode27 分钟前
dify项目结构说明与win11本地部署
后端·开源
呼啸长风2 小时前
FastKV的轻量化回归
android·开源·github
小和尚同志13 小时前
全网影视一网打尽!8.2K Star 的 LibreTV 让你甩开追剧烦恼
开源·github
说私域13 小时前
开源链动2+1模式与AI智能名片融合下的S2B2C商城小程序源码:重构大零售时代新生态
人工智能·重构·开源
国服第二切图仔13 小时前
基于文心开源大模型ERNIE-4.5-0.3B-Paddle私有化部署并构建一个企业智能客服系统
百度·开源·智能客服·知识库·文心大模型·paddle·gitcode
码字的字节16 小时前
阿里开源WebSailor:超越闭源模型的网络智能体新星
开源·阿里·websailor·网络智能体
CCF ODC16 小时前
第三届CCF夜莺开源创新论坛成功举办,夜莺V8正式发布
开源
算家计算17 小时前
6 亿参数玩转 20 + 语言!OuteTTS-1.0-0.6B本地部署教程,轻量模型也能 hold 住跨语言合成
人工智能·开源
Codebee18 小时前
OneCode 组件服务通用协议栈:构建企业级低代码平台的技术基石
前端·前端框架·开源
SkyrimCitadelValinor20 小时前
Git【开源分布式版本控制工具】安装-配置-常用指令-Git远程仓库-IDEA使用Git
分布式·git·gitee·开源·项目管理