当前位置:网站首页>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
边栏推荐
- scroll-view實現下拉刷新(避免onload進入頁面初始refresher-triggered為true觸發下拉問題)
- Linx link, first level directory, redirection, CP and MV
- Explanation of terms in Polymer Physics
- Understanding of flexible array in C language
- SkyWalking分布式系统应用程序性能监控工具-上
- Create database table db.create in flask project_ all()
- docker redis
- A little understanding of encoder
- SQL case multi conditional usage
- IDEA设置 自动导包删无用包
猜你喜欢

Sparksql design and introduction, 220722,

docker redis

爬虫requests模块的基本使用

128. Longest continuous sequence

Polymer synthesis technology

1000 okaleido tiger launched binance NFT, triggering a rush to buy

Linkerd service grid survey notes

罗克韦尔AB PLC RSLogix5000中的位指令使用方法介绍

Determination of host byte order

OSI open system interconnection model and tcp/ip model
随机推荐
Tutorial on principles and applications of database system (052) -- data integrity of MySQL (XIV): crosstab query (row column conversion)
IDEA设置 自动导包删无用包
1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮
免费学习机器学习交易的资源
B tree and b+ tree
A little understanding of encoder
Solve the problem that MySQL inserts Chinese garbled code into the table
About redis: there is still a risk of data loss after redis sets data persistence
好大夫问诊-俞驰-口腔信息
Concurrent programming 1-2
MGRE实验
*offer--2
Graphic pipeline (I) post-processing stage alpha test template test depth test mix
Create.Img image file
HCIP实验
网络 类型
Error running ‘XXX‘: Command line is too long. Shorten command line for AudioTest or also ...
Idea setting automatic package import and useless package deletion
Description of TCP packet sticking problem code
Notes to Chapter 2 of kubernetes in action