File: README.md

package info (click to toggle)
libfyaml 0.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,328 kB
  • sloc: ansic: 62,193; asm: 8,692; sh: 1,628; makefile: 581; python: 23
file content (281 lines) | stat: -rw-r--r-- 5,211 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# libfyaml Examples

This directory contains practical examples demonstrating various features of libfyaml.

## Building the Examples

### Using CMake (Recommended)

```bash
cd examples
mkdir build && cd build
cmake ..
cmake --build .
```

The compiled examples will be in the `build/` directory.

### Using Make

```bash
cd examples
make
```

The compiled examples will be in the current directory.

### Manual Compilation

```bash
gcc -o quick-start quick-start.c $(pkg-config --cflags --libs libfyaml)
gcc -o basic-parsing basic-parsing.c $(pkg-config --cflags --libs libfyaml)
# ... and so on for other examples
```

## Examples Overview

### 1. quick-start.c

Example showing:
- Parsing YAML from a file
- Extracting values using `fy_document_scanf()` (path-based queries)
- Modifying the document with `fy_document_insert_at()`
- Emitting the updated YAML

**Usage**:
```bash
./quick-start [config.yaml]
```

**Required YAML structure** (for config.yaml):
```yaml
server:
  host: localhost
  port: 8080
```

---

### 2. basic-parsing.c

Basic YAML parsing and different output formats:
- Parse YAML from file
- Emit as compact flow format
- Emit as standard block YAML
- Emit as JSON

**Usage**:
```bash
./basic-parsing [file.yaml]
```

Works with any valid YAML file.

---

### 3. path-queries.c

Query YAML documents using path expressions:
- Extract multiple values with `fy_document_scanf()`
- Query individual nodes with `fy_node_by_path()`
- Compare node values with strings
- Handle missing paths gracefully

**Usage**:
```bash
./path-queries
```

This example uses inline YAML, so no external file is needed.

---

### 4. document-manipulation.c

Example of document manipulation:
- Read existing values from YAML
- Update values (replace invoice number)
- Add new fields (spouse, delivery address)
- Emit with sorted keys

**Usage**:
```bash
./document-manipulation [invoice.yaml]
```

**Expected YAML structure** (for invoice.yaml):
```yaml
invoice: 34843
date: 2001-01-23
bill-to:
  given: Chris
  family: Dumars
  address:
    lines: |
      458 Walkman Dr.
      Suite #292
```

If no file is provided, the example uses built-in default data.

---

### 5. event-streaming.c

Demonstrates the low-level event-based API:
- Create parser and set input
- Process events one by one
- Handle different event types (scalars, mappings, sequences)
- Memory-efficient parsing for large files

**Usage**:
```bash
./event-streaming [file.yaml]
```

This is the most memory-efficient way to parse YAML, suitable for very large files.

---

### 6. build-from-scratch.c

Create YAML documents from scratch:
- Create empty document
- Build complex structures using `fy_node_buildf()` with printf-style formatting
- Add fields programmatically
- Emit as both JSON and YAML

**Usage**:
```bash
./build-from-scratch
```

No input file needed - generates a complete configuration document.

---

## Sample YAML Files

Sample YAML files are provided for testing:

- **config.yaml** - Simple server configuration
- **invoice.yaml** - Invoice document for manipulation example

Create these files in the examples directory, or the programs will use sensible defaults.

### Sample config.yaml

```yaml
server:
  host: localhost
  port: 8080
  ssl: true
  max_connections: 100

database:
  host: db.example.com
  port: 5432
  name: production_db

logging:
  level: info
  file: /var/log/app.log
```

### Sample invoice.yaml

```yaml
invoice: 34843
date: 2001-01-23
bill-to:
  given: Chris
  family: Dumars
  address:
    lines: |
      458 Walkman Dr.
      Suite #292
    city: Royal Oak
    state: MI
    postal: 48046
ship-to:
  given: Chris
  family: Dumars
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.
```

## API Reference

For complete API documentation, visit: https://pantoniou.github.io/libfyaml/

## Common Patterns

### Error Handling

All examples demonstrate proper error handling:
```c
struct fy_document *fyd = fy_document_build_from_file(NULL, "file.yaml");
if (!fyd) {
    fprintf(stderr, "Failed to parse YAML\n");
    return EXIT_FAILURE;
}

// ... use document ...

fy_document_destroy(fyd);  // Always clean up
```

### Path-Based Queries

Extract multiple values efficiently:
```c
int count = fy_document_scanf(fyd,
    "/path1 %s "
    "/path2 %d",
    string_var, &int_var);

if (count != 2) {
    // Handle error
}
```

### Document Modification

Add or update fields:
```c
fy_document_insert_at(fyd, "/parent/path", FY_NT,
    fy_node_buildf(fyd, "key: value"));
```

### Output Formatting

Choose output mode based on needs:
```c
// Compact JSON
fy_emit_document_to_fp(fyd, FYECF_MODE_JSON, stdout);

// Pretty YAML with sorted keys
fy_emit_document_to_fp(fyd, FYECF_MODE_BLOCK | FYECF_SORT_KEYS, stdout);

// One-line flow format
fy_emit_document_to_fp(fyd, FYECF_MODE_FLOW_ONELINE, stdout);
```

## License

All examples are released under the MIT License, same as libfyaml.

Copyright (c) 2019-2025 Pantelis Antoniou <pantelis.antoniou@konsulko.com>