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);
相关推荐
有事没事实验室13 小时前
CSS 浮动与定位以及定位中z-index的堆叠问题
前端·css·开源
空空隆隆13 小时前
我的白板工具支持 Markdown 转思维导图啦!
开源·白板工具·mermaid图表
Dontla17 小时前
BootCDN介绍(Bootstrap主导的前端开源项目免费CDN加速服务)
前端·开源·bootstrap
特立独行的猫a19 小时前
uni-app 开发HarmonyOS的鸿蒙影视项目分享:从实战案例到开源后台
uni-app·开源·harmonyos·鸿蒙·影视
高建伟-joe20 小时前
内容安全:使用开源框架Caffe实现上传图片进行敏感内容识别
人工智能·python·深度学习·flask·开源·html5·caffe
StarRocks_labs1 天前
StarRocks MCP Server 开源发布:为 AI 应用提供强大分析中枢
数据库·starrocks·人工智能·开源·olap·mcp
MWHLS1 天前
【以及好久没上号的闲聊】Unity记录8.1-地图-重构与优化
unity·开源
LunarCod1 天前
Ubuntu使用Docker搭建SonarQube企业版(含破解方法)
linux·运维·服务器·ubuntu·docker·开源·sonarqube
无心水2 天前
【程序员AI入门:模型】19.开源模型工程化全攻略:从选型部署到高效集成,LangChain与One-API双剑合璧
人工智能·langchain·开源·ai入门·程序员ai开发入门·程序员的 ai 开发第一课·程序员ai入门
zkmall2 天前
商业架构 2.0 时代:ZKmall开源商城前瞻性设计如何让 B2B2C 平台领先同行 10 年?
架构·开源