Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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)