Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/meffie/libyaml-examples
C language examples demonstrating how to use libyaml to emit and parse yaml formatted data
https://github.com/meffie/libyaml-examples
libyaml yaml
Last synced: about 1 month ago
JSON representation
C language examples demonstrating how to use libyaml to emit and parse yaml formatted data
- Host: GitHub
- URL: https://github.com/meffie/libyaml-examples
- Owner: meffie
- License: unlicense
- Created: 2019-04-10T16:28:39.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-01T20:14:36.000Z (about 2 years ago)
- Last Synced: 2023-03-27T12:46:24.950Z (almost 2 years ago)
- Topics: libyaml, yaml
- Language: C
- Homepage:
- Size: 23.4 KB
- Stars: 23
- Watchers: 2
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# libyaml examples
This is a small set of c language example programs to demonstrate how to use
the [libyaml library](http://pyyaml.org/wiki/LibYAML).## How to build
On Debian/Ubuntu, install the following packages.
$ sudo apt install gcc make libyaml-dev
Build the binaries with `make`.
$ make
## Emitter example
`emit.c` is a basic example to demonstrate how to convert raw c structs to a
yaml stream using the libyaml emitter API. The example data to be converted is
a list of lists.struct fruit {
struct fruit *next;
char *name;
char *color;
int count;
struct variety *varieties;
};
struct variety {
struct variety *next;
char *name;
char *color;
bool seedless;
};These structs are populated with some example data and emitted as yaml:
$ ./emit
---
fruit:
- name: apple
color: red
count: 12
varieties:
- name: macintosh
color: red
seedless: false
- name: granny smith
color: green
seedless: false
- name: red delicious
color: red
seedless: false
- name: orange
color: orange
count: 3
varieties:
- name: naval
color: orange
seedless: false
- name: clementine
color: orange
seedless: true
- name: valencia
color: orange
seedless: false
- name: bannana
color: yellow
count: 4
varieties:
- name: cavendish
color: yellow
seedless: true
- name: plantain
color: green
seedless: true
- name: mango
color: green
count: 1
varieties:
- name: honey
color: yellow
seedless: false
...## Parser example
`parser.c` is a basic example to demonstrate how to convert a specified yaml
grammar to c data structures using the libyaml emitter API. The `parser`
program can read yaml generated by the `emit` program.$ cat fruit.yaml
---
fruit:
- name: apple
color: red
count: 12
varieties:
- name: macintosh
color: red
seedless: false
- name: granny smith
color: green
seedless: false
- name: orange
color: orange
count: 3
varieties:
- name: naval
color: orange
seedless: false
- name: clementine
color: orange
seedless: true
...$ ./parse < fruit.yaml
fruit: name=apple, color=red, count=12
variety: name=macintosh, color=red, seedless=false
variety: name=granny smith, color=green, seedless=false
fruit: name=orange, color=orange, count=3
variety: name=naval, color=orange, seedless=false
variety: name=clementine, color=orange, seedless=true## Scanner example
`scan.c` is a general purpose libyaml parser example which scans and prints the
libyaml parser events. This can be useful for debugging yaml parsers.$ ./scan < fruit.yaml
stream-start-event (1)
document-start-event (3)
mapping-start-event (9)
scalar-event (6) = {value="fruit", length=5}
sequence-start-event (7)
mapping-start-event (9)
scalar-event (6) = {value="name", length=4}
scalar-event (6) = {value="apple", length=5}
scalar-event (6) = {value="color", length=5}
scalar-event (6) = {value="red", length=3}
scalar-event (6) = {value="count", length=5}
scalar-event (6) = {value="12", length=2}
scalar-event (6) = {value="varieties", length=9}
sequence-start-event (7)
mapping-start-event (9)
scalar-event (6) = {value="name", length=4}
scalar-event (6) = {value="macintosh", length=9}
scalar-event (6) = {value="color", length=5}
scalar-event (6) = {value="red", length=3}
scalar-event (6) = {value="seedless", length=8}
scalar-event (6) = {value="false", length=5}
mapping-end-event (10)
mapping-start-event (9)
scalar-event (6) = {value="name", length=4}
scalar-event (6) = {value="granny smith", length=12}
scalar-event (6) = {value="color", length=5}
scalar-event (6) = {value="green", length=5}
scalar-event (6) = {value="seedless", length=8}
scalar-event (6) = {value="false", length=5}
mapping-end-event (10)
sequence-end-event (8)
mapping-end-event (10)
mapping-start-event (9)
scalar-event (6) = {value="name", length=4}
scalar-event (6) = {value="orange", length=6}
scalar-event (6) = {value="color", length=5}
scalar-event (6) = {value="orange", length=6}
scalar-event (6) = {value="count", length=5}
scalar-event (6) = {value="3", length=1}
scalar-event (6) = {value="varieties", length=9}
sequence-start-event (7)
mapping-start-event (9)
scalar-event (6) = {value="name", length=4}
scalar-event (6) = {value="naval", length=5}
scalar-event (6) = {value="color", length=5}
scalar-event (6) = {value="orange", length=6}
scalar-event (6) = {value="seedless", length=8}
scalar-event (6) = {value="false", length=5}
mapping-end-event (10)
mapping-start-event (9)
scalar-event (6) = {value="name", length=4}
scalar-event (6) = {value="clementine", length=10}
scalar-event (6) = {value="color", length=5}
scalar-event (6) = {value="orange", length=6}
scalar-event (6) = {value="seedless", length=8}
scalar-event (6) = {value="true", length=4}
mapping-end-event (10)
sequence-end-event (8)
mapping-end-event (10)
mapping-start-event (9)
scalar-event (6) = {value="name", length=4}
scalar-event (6) = {value="bannana", length=7}
scalar-event (6) = {value="color", length=5}
scalar-event (6) = {value="yellow", length=6}
scalar-event (6) = {value="count", length=5}
scalar-event (6) = {value="4", length=1}
mapping-end-event (10)
mapping-start-event (9)
scalar-event (6) = {value="name", length=4}
scalar-event (6) = {value="mango", length=5}
scalar-event (6) = {value="color", length=5}
scalar-event (6) = {value="green", length=5}
scalar-event (6) = {value="count", length=5}
scalar-event (6) = {value="1", length=1}
mapping-end-event (10)
sequence-end-event (8)
mapping-end-event (10)
document-end-event (4)
stream-end-event (2)