当前位置:网站首页>Parsing yaml configuration files using C language and libcyaml Library
Parsing yaml configuration files using C language and libcyaml Library
2022-07-24 01:18:00 【llzhang_ fly】
C Linguistic yaml Library has libyaml and libcyaml (The Official YAML Web Site)
Personally feel libcyaml Work well ,libcyaml Project address :GitHub - tlsa/libcyaml: C library for reading and writing YAML.
libcyaml There is no difficulty in compiling , Just compile according to the instructions , It should be noted that installation in advance libyaml-dev:
sudo apt install libyaml-dev
The following is a direct example :
~/test
-include
-cyaml
cyaml.h
-lib
libcyaml.a
1.yaml
main.c
Makefile
1.yaml
# slave 1
Name: "drive_1"
Pos: 1
Vendor_ID: 0x00000A1E
Product_ID: 0x00000305
Sdo:
- Description: "To disable [AL. 090.1], set [Pr. PC41.0 [AL. 090.1 Homing incomplete] detection selection] to '1' (disabled)."
Index: 0x2129
SubIndex: 0x00
BitLen: 32
Value: 1
- Description: "Pr. PD01.2_Input signal automatic ON selection"
Index: 0x2181
SubIndex: 0x00
BitLen: 32
Value: 0x00000C00
- Description: "Reset alarm. Writing '1EA5h' resets an alarm."
Index: 0x2A46
SubIndex: 0x00
BitLen: 16
Value: 0x1EA5
main.c
/*
* SPDX-License-Identifier: ISC
*
* Copyright (C) 2018 Michael Drake <[email protected]>
*/
#include <stdlib.h>
#include <stdio.h>
#include <cyaml/cyaml.h>
/******************************************************************************
* C data structure for storing a project plan.
*
* This is what we want to load the YAML into.
******************************************************************************/
/* Structure for storing a sdo item */
struct sdo {
char *description;
int *index;
int *subindex;
int *bitlen;
int *value;
};
/* Structure for storing a slave */
struct slave {
char *name;
int *pos;
int *vendor_id;
int *product_id;
struct sdo *sdos;
unsigned sdos_count;
};
/******************************************************************************
* CYAML schema to tell libcyaml about both expected YAML and data structure.
*
* (Our CYAML schema is just a bunch of static const data.)
******************************************************************************/
/* The sdo mapping's field definitions schema is an array.
*
* All the field entries will refer to their parent mapping's structure,
* in this case, `struct task`.
*/
static const cyaml_schema_field_t sdo_fields_schema[] = {
/* The first field in the mapping is description.
*
* YAML key: "Description".
* C structure member for this key: "description".
*
* Its CYAML type is string pointer, and we have no minimum or maximum
* string length constraints.
*/
CYAML_FIELD_STRING_PTR(
"Description", CYAML_FLAG_POINTER,
struct sdo, description, 0, CYAML_UNLIMITED),
CYAML_FIELD_UINT("Index", CYAML_FLAG_DEFAULT, struct sdo, index),
CYAML_FIELD_UINT("SubIndex", CYAML_FLAG_DEFAULT, struct sdo, subindex),
CYAML_FIELD_UINT("BitLen", CYAML_FLAG_DEFAULT, struct sdo, bitlen),
CYAML_FIELD_UINT("Value", CYAML_FLAG_DEFAULT, struct sdo, value),
/* The field array must be terminated by an entry with a NULL key.
* Here we use the CYAML_FIELD_END macro for that. */
CYAML_FIELD_END
};
/* The value for a sdo is a mapping.
*
* Its fields are defined in sdo_fields_schema.
*/
static const cyaml_schema_value_t sdo_schema = {
CYAML_VALUE_MAPPING(CYAML_FLAG_DEFAULT,
struct sdo, sdo_fields_schema),
};
/* The plan mapping's field definitions schema is an array.
*
* All the field entries will refer to their parent mapping's structure,
* in this case, `struct plan`.
*/
static const cyaml_schema_field_t slave_fields_schema[] = {
CYAML_FIELD_STRING_PTR(
"Name", CYAML_FLAG_POINTER,
struct slave, name, 0, CYAML_UNLIMITED),
CYAML_FIELD_UINT(
"Pos", CYAML_FLAG_DEFAULT,
struct slave, pos),
CYAML_FIELD_UINT(
"Vendor_ID", CYAML_FLAG_DEFAULT,
struct slave, vendor_id),
CYAML_FIELD_UINT(
"Product_ID", CYAML_FLAG_DEFAULT,
struct slave, product_id),
CYAML_FIELD_SEQUENCE(
"Sdo", CYAML_FLAG_POINTER,
struct slave, sdos,
&sdo_schema, 0, CYAML_UNLIMITED),
/* The field array must be terminated by an entry with a NULL key.
* Here we use the CYAML_FIELD_END macro for that. */
CYAML_FIELD_END
};
/* Top-level schema. The top level value for the plan is a mapping.
*
* Its fields are defined in plan_fields_schema.
*/
static const cyaml_schema_value_t slave_schema = {
CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER,
struct slave, slave_fields_schema),
};
/******************************************************************************
* Actual code to load and save YAML doc using libcyaml.
******************************************************************************/
/* Our CYAML config.
*
* If you want to change it between calls, don't make it const.
*
* Here we have a very basic config.
*/
static const cyaml_config_t config = {
.log_fn = cyaml_log, /* Use the default logging function. */
.mem_fn = cyaml_mem, /* Use the default memory allocator. */
.log_level = CYAML_LOG_WARNING, /* Logging errors and warnings only. */
};
/* Main entry point from OS. */
int main(int argc, char *argv[])
{
cyaml_err_t err;
struct slave *slave;
enum {
ARG_PROG_NAME,
ARG_PATH_IN,
ARG_PATH_OUT,
ARG__COUNT,
};
// /* Handle args */
// if (argc != ARG__COUNT) {
// fprintf(stderr, "Usage:\n");
// fprintf(stderr, " %s <INPUT> <OUTPUT>\n", argv[ARG_PROG_NAME]);
// return EXIT_FAILURE;
// }
/* Load input file. */
err = cyaml_load_file(argv[ARG_PATH_IN], &config,
&slave_schema, (void **) &slave, NULL);
if (err != CYAML_OK) {
fprintf(stderr, "ERROR: %s\n", cyaml_strerror(err));
return EXIT_FAILURE;
}
/* Use the data. */
printf("Project: %s\n", slave->name);
for (unsigned i = 0; i < slave->sdos_count; i++) {
printf("%u. Description: %s\n", i + 1, slave->sdos[i].description);
printf("Index: 0x%x\n", slave->sdos[i].index);
printf("SubIndex: %d\n", slave->sdos[i].subindex);
printf("BitLen: %d\n", slave->sdos[i].bitlen);
printf("Value: 0x%x\n", slave->sdos[i].value);
}
// /* Modify the data */
// plan->tasks[0].estimate.days += 3;
// plan->tasks[0].estimate.weeks += 1;
// /* Save data to new YAML file. */
// err = cyaml_save_file(argv[ARG_PATH_OUT], &config,
// &plan_schema, plan, 0);
// if (err != CYAML_OK) {
// fprintf(stderr, "ERROR: %s\n", cyaml_strerror(err));
// cyaml_free(&config, &plan_schema, plan, 0);
// return EXIT_FAILURE;
// }
/* Free the data */
cyaml_free(&config, &slave_schema, slave, 0);
return EXIT_SUCCESS;
}
Makefile
INCLUDE = -I include
CFLAGS += $(INCLUDE)
CFLAGS += -std=c11 -Wall -Wextra -pedantic \
-Wconversion -Wwrite-strings -Wcast-align -Wpointer-arith \
-Winit-self -Wshadow -Wstrict-prototypes -Wmissing-prototypes \
-Wredundant-decls -Wundef -Wvla -Wdeclaration-after-statement
LDFLAGS += -lyaml -Llib -lcyaml
all: main
main: main.c
$(CC) $(CPPFLAGS) $(CFLAGS) -o [email protected] $^ $(LDFLAGS)
————————————————
Copyright notice : This paper is about CSDN Blogger 「liu_jiankang」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/weixin_41573966/article/details/125060965
边栏推荐
- Talk about moment of inertia
- C language force deduction question 53 of the largest subarray sum. Dynamic programming and divide and conquer
- Jianzhi offer 05 two stacks to realize the queue
- Tutorial on principles and applications of database system (052) -- data integrity of MySQL (XIV): crosstab query (row column conversion)
- Error running ‘XXX‘: Command line is too long. Shorten command line for AudioTest or also ...
- RIP---路由信息协议
- 【复盘】关于我在错误的时间选错了技术这件事
- c语言支持yaml配置文件通用方法
- Kotlin基础从入门到进阶系列讲解(基础篇)关键字:suspend
- RPM build has installed the dependent package, but it still reports an error. There is no module provided in the package
猜你喜欢

Sparksql design and introduction, 220722,
出于数据安全考虑 荷兰教育部要求学校暂停使用Chrome浏览器

B tree and b+ tree

Graphic pipeline (I) post-processing stage alpha test template test depth test mix

Kubernets déploiement du tableau de bord (interface visuelle)
For data security reasons, the Dutch Ministry of Education asked schools to suspend the use of Chrome browser

SCM learning notes 1 -- data download and environment construction (based on Baiwen STM32F103 series tutorials)

Sublime text 3 汉化+添加常用插件

HCIA的复习

OSPF experiment
随机推荐
Kubernetes deployment dashboard (visual interface)
*offer--2
为什么博途V17及以下的HMI面板不能与1500固件版本2.9或1200版本4.5 的CPU建立连接?
New infrastructure of enterprise data in the era of digital transformation | love Analysis Report
【FreeSwitch开发实践】专栏简介
Understanding of flexible array in C language
Concurrent programming 1-2
Talk about moment of inertia
High voltage technical examination questions with answers
Redis - configuration and Application
What the hell is ThreadLocal doing?
HCIA的复习
High voltage technology test questions and answers
scroll-view实现下拉刷新(避免onload进入页面初始refresher-triggered为true触发下拉问题)
What impact does the European "gas shortage" have on China?
How to troubleshoot the problem that VPN server cannot forward
HCIP第六天_特殊区域综合实验
Intelligent video monitoring solutions for elderly care institutions, using new technologies to help the intelligent supervision of nursing homes
Redis - basic concept
2022全球开发者薪资曝光:中国排第19名,平均年薪23,790美元