本篇文章给大家谈谈c语言yaml解析,以及yaml c语言对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、C语言操作yaml配置文件通用操作工具2、yaml基本语法3、yaml文件用c语言如何解析出键值对啊?谢谢4、YAML用法详解
C语言操作yaml配置文件通用操作工具
在go语言中使用viper之类的库很方便的处理yaml配置文件,但是在c语言中就比较麻烦,经过一番思索和借助强大的github,发现了一个libyaml c库,但是网上的例子都比较麻烦,而且比较繁琐,就想法作了一个相对比较容易配置的解析应用,可以简单地类似viper 的模式进行配置实现不同的配置文件读取。如你的配置文件很复杂请按格式修改KeyValue 全局变量,欢迎大家一起完善
库请自行下载 GitHub – yaml/libyaml: Canonical source repository for LibYAML
直接上代码
yaml示例文件
%YAML 1.1
—
mqtt:
subtopic: “Control/#”
pubtopic: “bbt”
qos: 1
serveraddress: “tcp://192.168.0.25:1883”
clientid: “kvm_test”
writelog: false
writetodisk: false
outputfile: “./receivedMessages.txt”
hearttime: 30
#ifndef __CONFIG_H__
#define __CONFIG_H__
#ifdef __cplusplus
extern “C” {
#endif
/************************/
/* Minimum YAML version */
/************************/
#define YAML_VERSION_MAJOR 1
#define YAML_VERSION_MINOR 1
#define STRUCT_TYPE_NAME 100
#define INT_TYPE_NAME 101
#define STRING_TYPE_NAME 102
#define BOOL_TYPE_NAME 103
#define FLOAT_TYPE_NAME 104
#define MAP_TYPE_NAME 105
#define LIST_TYPE_NAME 106
typedef struct{
char *key;
void *value;
int valuetype;
char *parent;
}KeyValue,*pKeyValue;
#ifdef __cplusplus
}
#endif
#endif
#include
#include
#include
#include
#include
#include
#include
#include “config.h”
typedef struct {
char *SUBTOPIC; //string `yaml:”subtopic” mapstructure:”subtopic”` //”topic1″
char *PUBTOPIC; //string `yaml:”pubtopic” mapstructure:”pubtopic”`
int QOS; //byte `yaml:”qos” mapstructure:”qos”` //1
char *SERVERADDRESS; //string `yaml:”serveraddress” mapstructure:”serveraddress”` //= “tcp://mosquitto:1883”
char *CLIENTID; //string `yaml:”clientid” mapstructure:”clientid”` //= “mqtt_subscriber”
int HEARTTIME; //int `yaml:”hearttime” mapstructure:”hearttime”`
// CommandLocalPath string `yam:”commanlocalpath”`
}mqttSection,*pmqttSection;
typedef struct {
mqttSection Mqtt;// `yaml:”mqtt” mapstructure:”mqtt”`
// KVM kvmSection `yaml:”kvm” mapstructure:”kvm”`
}ConfigT;
ConfigT config;
static KeyValue webrtcconfig[]={
{“mqtt”,config,STRUCT_TYPE_NAME,NULL},
{“subtopic”,(config.Mqtt.SUBTOPIC),STRING_TYPE_NAME,”mqtt”},
{“pubtopic”,(config.Mqtt.PUBTOPIC),STRING_TYPE_NAME,”mqtt”},
{“qos”,(config.Mqtt.QOS),INT_TYPE_NAME,”mqtt”},
{“serveraddress”,(config.Mqtt.SERVERADDRESS),STRING_TYPE_NAME,”mqtt”},
{“clientid”,(config.Mqtt.CLIENTID),STRING_TYPE_NAME,”mqtt”},
{“hearttime”,(config.Mqtt.HEARTTIME),INT_TYPE_NAME,”mqtt”},
{NULL,NULL,0,NULL},
};
int printConfig(ConfigT * pconfig){
if(pconfig==NULL) return -1;
printf(“mqtt:r “);
if(pconfig-Mqtt.SUBTOPIC!=NULL) {printf(“subtopic: %sr “,pconfig-Mqtt.SUBTOPIC); }
if(pconfig-Mqtt.SUBTOPIC!=NULL) {printf(“pubtopic: %sr “,pconfig-Mqtt.PUBTOPIC); }
printf(“qos: %dr “,config.Mqtt.QOS);
if(pconfig-Mqtt.SERVERADDRESS!=NULL) {printf(“serveraddress: %sr “,pconfig-Mqtt.SERVERADDRESS); }
if(pconfig-Mqtt.CLIENTID!=NULL) {printf(“clientid: %sr “,pconfig-Mqtt.CLIENTID); }
printf(“hearttime: %dr “,config.Mqtt.HEARTTIME);
}
int freeConfig(ConfigT * pconfig){
if(pconfig==NULL) return -1;
if(pconfig-Mqtt.SERVERADDRESS!=NULL) {free(pconfig-Mqtt.SERVERADDRESS); }
if(pconfig-Mqtt.CLIENTID!=NULL) {free(pconfig-Mqtt.CLIENTID); }
if(pconfig-Mqtt.SUBTOPIC!=NULL) {free(pconfig-Mqtt.SUBTOPIC); }
}
char currentkey[100];
void getvalue(yaml_event_t event,pKeyValue *ppconfigs){
char *value = (char *)event.data.scalar.value;
pKeyValue pconfig=*ppconfigs;
char *pstringname;
while(pconfig-key!=NULL){
if(currentkey[0]!=0){
if(!strcmp(currentkey,pconfig-key))
{
switch(pconfig-valuetype){
case STRING_TYPE_NAME:
pstringname=strdup(value);
printf(“get string value %sr “,pstringname);
*((char**)pconfig-value)=pstringname;
memset(currentkey, 0, sizeof(currentkey));
break;
case INT_TYPE_NAME:
*((int*)(pconfig-value))=atoi(value);
memset(currentkey, 0, sizeof(currentkey));
break;
case BOOL_TYPE_NAME:
if(!strcmp(value,”true”)) *((bool*)(pconfig-value))=true;
else *((bool*)(pconfig-value))=false;
memset(currentkey, 0, sizeof(currentkey));
break;
case FLOAT_TYPE_NAME:
*((float*)(pconfig-value))=atof(value);
memset(currentkey, 0, sizeof(currentkey));
break;
case STRUCT_TYPE_NAME:
case MAP_TYPE_NAME:
case LIST_TYPE_NAME:
memset(currentkey, 0, sizeof(currentkey));
strncpy(currentkey,value,strlen(value));
break;
default:
break;
}
break;
}
//continue;
}else{
if(!strcmp(value,pconfig-key)){
strncpy(currentkey,pconfig-key,strlen(pconfig-key));
break;
}
}
pconfig++;
}
}
int Load_YAML_Config( char *yaml_file, KeyValue *(configs[]) )
{
struct stat filecheck;
yaml_parser_t parser;
yaml_event_t event;
bool done = 0;
unsigned char type = 0;
unsigned char sub_type = 0;
if (stat(yaml_file, filecheck) != false )
{
printf(“[%s, line %d] Cannot open configuration file ‘%s’! %s”, __FILE__, __LINE__, yaml_file, strerror(errno) );
return -1;
}
FILE *fh = fopen(yaml_file, “r”);
if (!yaml_parser_initialize(parser))
{
printf(“[%s, line %d] Failed to initialize the libyaml parser. Abort!”, __FILE__, __LINE__);
return -1;
}
if (fh == NULL)
{
printf(“[%s, line %d] Failed to open the configuration file ‘%s’ Abort!”, __FILE__, __LINE__, yaml_file);
return -1;
}
memset(currentkey, 0, sizeof(currentkey));
/* Set input file */
yaml_parser_set_input_file(parser, fh);
while(!done)
{
if (!yaml_parser_parse(parser, event))
{
/* Useful YAML vars: parser.context_mark.line+1, parser.context_mark.column+1, parser.problem, parser.problem_mark.line+1, parser.problem_mark.column+1 */
printf( “[%s, line %d] libyam parse error at line %ld in ‘%s'”, __FILE__, __LINE__, parser.problem_mark.line+1, yaml_file);
}
if ( event.type == YAML_DOCUMENT_START_EVENT )
{
//yaml file first line is version
//%YAML 1.1
//—
yaml_version_directive_t *ver = event.data.document_start.version_directive;
if ( ver == NULL )
{
printf( “[%s, line %d] Invalid configuration file. Configuration must start with “%%YAML 1.1″”, __FILE__, __LINE__);
}
int major = ver-major;
int minor = ver-minor;
if (! (major == YAML_VERSION_MAJOR minor == YAML_VERSION_MINOR) )
{
printf( “[%s, line %d] Configuration has a invalid YAML version. Must be 1.1 or above”, __FILE__, __LINE__);
return -1;
}
}
else if ( event.type == YAML_STREAM_END_EVENT )
{
done = true;
}
else if ( event.type == YAML_MAPPING_END_EVENT )
{
sub_type = 0;
}
else if ( event.type == YAML_SCALAR_EVENT )
{
getvalue(event,configs);
}
}
return 0;
}
int main(int argc, char *argv[]){
pKeyValue pconfig=webrtcconfig[0];
Load_YAML_Config(“../../etc/kvmagent.yml”,pconfig);
printConfig(config);
freeConfig(config);
}
yaml基本语法
YAML:YAML Ain’t Markup Language,即YAML不是标记语言。不过,在开发的这种语言时,YAML的意思其实是:”Yet Another Markup Language”(仍是一种标记语言)
YAML是一个可读性高的用来表达资料序列的格式。YAML参考了其他多种语言,包括:XML、C语言、 Python、Perl以及电子邮件格式RFC2822等。Clark Evans在2001年在首次发表了这种语言,另外Ingy döt Net与Oren Ben-Kiki也是这语言的共同设计者,目前很多最新的软件比较流行采用此格式的文件存放配置信息,如:ubuntu,anisble,docker,kubernetes等
YAML 官方网站:
ansible 官网:
yaml语言特性归纳如下
1、大小写敏感
2、使用缩进表示层级关系
3、禁止使用tab缩进,只能使用空格键
4、缩进长度没有限制,只要元素对齐就表示这些元素属于一个层级
5、使用#表示注释
6、字符串可以不用引号标注
YAML 支持以下常用几种数据类型:
key对应value
标量是最基本的,不可再分的值,包括:
字典由多个key与value构成,key和value之间用 :分隔, 并且 : 后面有一个空格,所有k/v可以放在一行,或者每个 k/v 分别放在不同行
格式:
使用缩进方式
范例:
列表由多个元素组成,每个元素放在不同行,且元素前均使用”-“打头,并且 – 后有一个空格, 或者将所有元素用 [ ] 括起来放在同一行
也可以写成以 – 开头的多行
数据里面也可以包含字典
不同行,行以-开头,后面有一个空格# A list of tasty fruits
范例:YAML 表示一个家庭
XML:Extensible Markup Language,可扩展标记语言,可用于数据交换和配置
JSON:JavaScript Object Notation, JavaScript 对象表记法,主要用来数据交换或配置,不支持注释
YAML:YAML Ain’t Markup Language YAML 不是一种标记语言, 主要用来配置,大小写敏感,不支持tab
yaml文件用c语言如何解析出键值对啊?谢谢
有一个C语言的解析包:LibYAML 是一个 C 语言的包,用来解析 YAML 1.1 数据。
当前版本是 LibYAML: 0.1.5 (2014-02-04).
YAML用法详解
近期在调试SONIC上基于Ansible的PTF测试平台时,使用到了YANML配置语言,此处稍作解释,汇总一下YAML的语法及基本使用方法。
YAML ( /ˈjæməl/ ,尾音类似 camel 骆驼)是一个可读性高,用来表达数据序列的格式。YAML参考了其他多种语言,包括: C语言 、 Python 、 Perl ,并从 XML 、电子邮件的数据格式(RFC 2822 )中获得灵感。上述介绍引用自 维基百科 ,通俗的讲,YAML是专门用来写配置文件的语言,非常简洁和强大,远比 JSON/XML格式方便。YAML允许在层次结构中存储结构化数据。YAML 旨在以人为和机器可读,并且开销最小。YAML 规范可以在 yaml.org 找到,官方还提供了一个便捷的 YAML小抄 。
再次强调,字符串不需要包在引号之内。有两种方法书写多行文字(multi-line strings),一种可以保存新行(使用|字符),另一种可以折叠新行(使用字符)
和保存新行不同的是,换行字符会被转换成空白字符。而引领空白字符则会被自动消去。
常量值最基本的数字、字符串、布尔值等等
在python中,会将上述键值对解析为一个字典
上述键值对可以嵌套,从而实现更加复杂的数据结构:
在python中,会将其解析为嵌套的字典结构:
在python中,会将其解析为字典嵌套列表的复合数据结构:
为了维持文件的简洁,并避免数据输入的错误,YAML提供了结点参考(*)和散列合并()参考到其他结点标签的锚点标记()。参考会将树状结构加入锚点标记的内容,并可以在所有数据结构中运作(可以参考上面”ship-to”的示例)合并只有散列表可以使用,可以将键值自锚点标记复制到指定的散列表中。当数据被instantiate合并和参考会被剖析器自动展开。
c语言yaml解析的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于yaml c语言、c语言yaml解析的信息别忘了在本站进行查找喔。