File: basic-parsing.c

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 (50 lines) | stat: -rw-r--r-- 1,174 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
/*
 * basic-parsing.c - Basic YAML parsing example
 *
 * Demonstrates:
 * - Parsing YAML from a file
 * - Basic error handling
 * - Emitting in different output modes
 *
 * Copyright (c) 2019-2025 Pantelis Antoniou <pantelis.antoniou@konsulko.com>
 *
 * SPDX-License-Identifier: MIT
 */

#include <stdlib.h>
#include <stdio.h>
#include <libfyaml.h>

int main(int argc, char *argv[])
{
	struct fy_document *fyd = NULL;
	const char *input_file = (argc > 1) ? argv[1] : "config.yaml";

	// Parse YAML from file
	fyd = fy_document_build_from_file(NULL, input_file);
	if (!fyd) {
		fprintf(stderr, "Failed to parse YAML from %s\n", input_file);
		return EXIT_FAILURE;
	}

	printf("Successfully parsed: %s\n\n", input_file);

	// Emit as flow-oneline (compact format)
	printf("Compact format:\n");
	fy_emit_document_to_fp(fyd, FYECF_MODE_FLOW_ONELINE, stdout);
	printf("\n\n");

	// Emit as block (standard YAML)
	printf("Block format:\n");
	fy_emit_document_to_fp(fyd, FYECF_MODE_BLOCK, stdout);
	printf("\n");

	// Emit as JSON
	printf("JSON format:\n");
	fy_emit_document_to_fp(fyd, FYECF_MODE_JSON, stdout);
	printf("\n");

	fy_document_destroy(fyd);

	return EXIT_SUCCESS;
}