yaml官网
http://yaml.org/
yaml下载官网
http://pyyaml.org/wiki/LibYAML
下载网址
http://pyyaml.org/download/libyaml
在我写这篇文章的时候这个网站已经无法访问了,不清楚是什么原因,我最后得到的版本是yaml-0.1.7.tar.gz
源码下载地址
https://download.csdn.net/download/zero_gg/10146741?web=web
编译源码
1、解压源码包
rt@ubuntu:~/yaml/yaml-0.1.7$ tar xvf yaml-0.1.7.tar.gz
rt@ubuntu:~/yaml/yaml-0.1.7$ cd yaml-0.1.7/
2、生成makefile文件
rt@ubuntu:~/yaml/yaml-0.1.7$ ./configure --prefix=/home/rt/opt/yaml
其中--prefix=为安装路径,如果需要安装到自己系统中就不要添加后面的参数,直接./configure即可,后面的make install需要sudo权限。
3、编译、安装
rt@ubuntu:~/yaml/yaml-0.1.7$ make
rt@ubuntu:~/yaml/yaml-0.1.7$ make install
此时在安装路径下就有生成的文件了。
例子程序
1、获取软件版本号 test-version.c
#include
#include
#include
#ifdef NDEBUG
#undef NDEBUG
#endif
#include
int
main(void)
{
int major = -1;
int minor = -1;
int patch = -1;
char buf[64];
yaml_get_version(&major, &minor, &patch);
sprintf(buf, "%d.%d.%d", major, minor, patch);
assert(strcmp(buf, yaml_get_version_string()) == 0);
printf("version = %s\n", yaml_get_version_string());
/* Print structure sizes. */
printf("sizeof(token) = %d\n", sizeof(yaml_token_t));
printf("sizeof(event) = %d\n", sizeof(yaml_event_t));
printf("sizeof(parser) = %d\n", sizeof(yaml_parser_t));
return 0;
}
执行gcc test-version.c -o version -lyaml -I/home/rt/opt/yaml/include -L/home/rt/opt/yaml/lib
运行结果
rt@ubuntu:~/yaml$ ./version
version = 0.1.7
sizeof(token) = 40
sizeof(event) = 56
sizeof(parser) = 248
读取并解析yaml文件 run-scanner.c
Invoice.yaml
%TAG !yaml! tag:yaml.org,2002:
---
!e!foo "bar"
--- !
invoice: 34843
date : 2001-01-23
bill-to: &id001
given : Chris
family : Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city : Royal Oak
state : MI
postal : 48046
ship-to: *id001
product:
- sku : BL394D
quantity : 4
description : Basketball
price : 450.00
- sku : BL4438H
quantity : 1
description : Super Hoop
price : 2392.00
tax : 251.42
total: 4443.52
comments:
Late afternoon is best.
Backup contact is Nancy
Billsmer @ 338-4338.
run-scanner.c
#include
#include
#include
#ifdef NDEBUG
#undef NDEBUG
#endif
#include
int
main(int argc, char *argv[])
{
int number;
if (argc < 2) {
printf("Usage: %s file1.yaml ...\n", argv[0]);
return 0;
}
for (number = 1; number < argc; number ++)
{
FILE *file;
yaml_parser_t parser;
yaml_token_t token;
int done = 0;
int count = 0;
int error = 0;
fflush(stdout);
file = fopen(argv[number], "rb");
assert(file);
assert(yaml_parser_initialize(&parser));
yaml_parser_set_input_file(&parser, file);
while (!done)
{
if (!yaml_parser_scan(&parser, &token)) {
error = 1;
break;
}
switch(token.type)
{
case YAML_STREAM_START_TOKEN:
printf("YAML_STREAM_START_TOKEN: encoding=%d\n", token.data.stream_start.encoding);
break;
case YAML_STREAM_END_TOKEN:
printf("YAML_STREAM_END_TOKEN\n");
break;
case YAML_VERSION_DIRECTIVE_TOKEN:
printf("YAML_VERSION_DIRECTIVE_TOKEN: major=%d, minor=%d\n", token.data.version_directive.major, token.data.version_directive.minor);
break;
case YAML_TAG_DIRECTIVE_TOKEN:
printf("YAML_TAG_DIRECTIVE_TOKEN: handle=%s, prefix=%s\n", token.data.tag_directive.handle, token.data.tag_directive.prefix);
break;
case YAML_DOCUMENT_START_TOKEN:
printf("YAML_DOCUMENT_START_TOKEN\n");
break;
case YAML_DOCUMENT_END_TOKEN:
printf("YAML_DOCUMENT_END_TOKEN\n");
break;
case YAML_BLOCK_SEQUENCE_START_TOKEN:
printf("YAML_BLOCK_SEQUENCE_START_TOKEN\n");
break;
case YAML_BLOCK_MAPPING_START_TOKEN:
printf("YAML_BLOCK_MAPPING_START_TOKEN\n");
break;
case YAML_BLOCK_END_TOKEN:
printf("YAML_BLOCK_END_TOKEN\n");
break;
case YAML_FLOW_SEQUENCE_START_TOKEN:
printf("YAML_FLOW_SEQUENCE_START_TOKEN\n");
break;
case YAML_FLOW_SEQUENCE_END_TOKEN:
printf("YAML_FLOW_SEQUENCE_END_TOKEN\n");
break;
case YAML_FLOW_MAPPING_START_TOKEN:
printf("YAML_FLOW_MAPPING_START_TOKEN\n");
break;
case YAML_FLOW_MAPPING_END_TOKEN:
printf("YAML_FLOW_MAPPING_END_TOKEN\n");
break;
case YAML_BLOCK_ENTRY_TOKEN:
printf("YAML_BLOCK_ENTRY_TOKEN\n");
break;
case YAML_FLOW_ENTRY_TOKEN:
printf("YAML_FLOW_ENTRY_TOKEN\n");
break;
case YAML_KEY_TOKEN:
printf("YAML_KEY_TOKEN\n");
break;
case YAML_VALUE_TOKEN:
printf("YAML_VALUE_TOKEN\n");
break;
case YAML_ALIAS_TOKEN:
printf("YAML_ALIAS_TOKEN: value=%s\n", token.data.alias.value);
break;
case YAML_ANCHOR_TOKEN:
printf("YAML_ANCHOR_TOKEN: value=%s\n", token.data.anchor.value);
break;
case YAML_TAG_TOKEN:
printf("YAML_TAG_TOKEN: handle=%s, suffix=%s\n", token.data.tag.handle, token.data.tag.suffix);
break;
case YAML_SCALAR_TOKEN:
{
printf("YAML_SCALAR_TOKEN: value=%s, length=%lu, style=%d\n", token.data.scalar.value, token.data.scalar.length, token.data.scalar.style);
}
break;
}
done = (token.type == YAML_STREAM_END_TOKEN);
yaml_token_delete(&token);
count ++;
}
yaml_parser_delete(&parser);
assert(!fclose(file));
printf("[%d] Scanning '%s': ", number, argv[number]);
printf("%s (%d tokens)\n", (error ? "FAILURE" : "SUCCESS"), count);
}
return 0;
}
执行gcc run-scanner.c -o scanfile -lyaml -I/home/rt/opt/yaml/include -L/home/rt/opt/yaml/lib
rt@ubuntu:~/yaml$ ./scanfile Invoice.yaml
YAML_STREAM_START_TOKEN: encoding=1
YAML_TAG_DIRECTIVE_TOKEN: handle=!yaml!, prefix=tag:yaml.org,2002:
YAML_DOCUMENT_START_TOKEN
YAML_TAG_TOKEN: handle=!e!, suffix=foo
YAML_SCALAR_TOKEN: value=bar, length=3, style=3
YAML_DOCUMENT_START_TOKEN
YAML_TAG_TOKEN: handle=, suffix=tag:clarkevans.com,2002:invoice
YAML_BLOCK_MAPPING_START_TOKEN
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=invoice, length=7, style=1
YAML_VALUE_TOKEN
YAML_SCALAR_TOKEN: value=34843, length=5, style=1
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=date, length=4, style=1
YAML_VALUE_TOKEN
YAML_SCALAR_TOKEN: value=2001-01-23, length=10, style=1
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=bill-to, length=7, style=1
YAML_VALUE_TOKEN
YAML_ANCHOR_TOKEN: value=id001
YAML_BLOCK_MAPPING_START_TOKEN
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=given, length=5, style=1
YAML_VALUE_TOKEN
YAML_SCALAR_TOKEN: value=Chris, length=5, style=1
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=family, length=6, style=1
YAML_VALUE_TOKEN
YAML_SCALAR_TOKEN: value=Dumars, length=6, style=1
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=address, length=7, style=1
YAML_VALUE_TOKEN
YAML_BLOCK_MAPPING_START_TOKEN
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=lines, length=5, style=1
YAML_VALUE_TOKEN
YAML_SCALAR_TOKEN: value=458 Walkman Dr.
Suite #292
, length=27, style=4
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=city, length=4, style=1
YAML_VALUE_TOKEN
YAML_SCALAR_TOKEN: value=Royal Oak, length=9, style=1
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=state, length=5, style=1
YAML_VALUE_TOKEN
YAML_SCALAR_TOKEN: value=MI, length=2, style=1
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=postal, length=6, style=1
YAML_VALUE_TOKEN
YAML_SCALAR_TOKEN: value=48046, length=5, style=1
YAML_BLOCK_END_TOKEN
YAML_BLOCK_END_TOKEN
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=ship-to, length=7, style=1
YAML_VALUE_TOKEN
YAML_ALIAS_TOKEN: value=id001
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=product, length=7, style=1
YAML_VALUE_TOKEN
YAML_BLOCK_SEQUENCE_START_TOKEN
YAML_BLOCK_ENTRY_TOKEN
YAML_BLOCK_MAPPING_START_TOKEN
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=sku, length=3, style=1
YAML_VALUE_TOKEN
YAML_SCALAR_TOKEN: value=BL394D, length=6, style=1
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=quantity, length=8, style=1
YAML_VALUE_TOKEN
YAML_SCALAR_TOKEN: value=4, length=1, style=1
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=description, length=11, style=1
YAML_VALUE_TOKEN
YAML_SCALAR_TOKEN: value=Basketball, length=10, style=1
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=price, length=5, style=1
YAML_VALUE_TOKEN
YAML_SCALAR_TOKEN: value=450.00, length=6, style=1
YAML_BLOCK_END_TOKEN
YAML_BLOCK_ENTRY_TOKEN
YAML_BLOCK_MAPPING_START_TOKEN
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=sku, length=3, style=1
YAML_VALUE_TOKEN
YAML_SCALAR_TOKEN: value=BL4438H, length=7, style=1
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=quantity, length=8, style=1
YAML_VALUE_TOKEN
YAML_SCALAR_TOKEN: value=1, length=1, style=1
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=description, length=11, style=1
YAML_VALUE_TOKEN
YAML_SCALAR_TOKEN: value=Super Hoop, length=10, style=1
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=price, length=5, style=1
YAML_VALUE_TOKEN
YAML_SCALAR_TOKEN: value=2392.00, length=7, style=1
YAML_BLOCK_END_TOKEN
YAML_BLOCK_END_TOKEN
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=tax, length=3, style=1
YAML_VALUE_TOKEN
YAML_SCALAR_TOKEN: value=251.42, length=6, style=1
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=total, length=5, style=1
YAML_VALUE_TOKEN
YAML_SCALAR_TOKEN: value=4443.52, length=7, style=1
YAML_KEY_TOKEN
YAML_SCALAR_TOKEN: value=comments, length=8, style=1
YAML_VALUE_TOKEN
YAML_SCALAR_TOKEN: value=Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338., length=68, style=1
YAML_BLOCK_END_TOKEN
YAML_STREAM_END_TOKEN
[1] Scanning 'Invoice.yaml': SUCCESS (112 tokens)