File: quick-start.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 (70 lines) | stat: -rw-r--r-- 1,734 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
/*
 * quick-start.c - Quick start example for libfyaml
 *
 * Demonstrates:
 * - Parsing YAML from a file
 * - Extracting values using path-based scanf
 * - Modifying the document
 * - Emitting as YAML
 *
 * 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;
	unsigned int port;
	char hostname[256];
	int ret = EXIT_FAILURE;
	const char *input_file = (argc > 1) ? argv[1] : "config.yaml";

	// Parse YAML from string or file
	fyd = fy_document_build_from_file(NULL, input_file);
	if (!fyd) {
		fprintf(stderr, "Failed to parse %s\n", input_file);
		fprintf(stderr, "Note: You can create config.yaml or pass a file as argument\n");
		goto cleanup;
	}

	// Extract values using path-based scanf
	int count = fy_document_scanf(fyd,
		"/server/port %u "
		"/server/host %255s",
		&port, hostname);

	if (count != 2) {
		fprintf(stderr, "Failed to extract server configuration\n");
		fprintf(stderr, "Expected /server/port and /server/host in YAML\n");
		goto cleanup;
	}

	printf("Current configuration: %s:%u\n", hostname, port);

	// Modify document using path-based insertion
	if (fy_document_insert_at(fyd, "/server", FY_NT,
	    fy_node_buildf(fyd, "timeout: 30"))) {
		fprintf(stderr, "Failed to insert timeout setting\n");
		goto cleanup;
	}

	printf("Added timeout setting\n");

	// Emit as YAML
	printf("\nUpdated configuration:\n");
	if (fy_emit_document_to_fp(fyd, FYECF_SORT_KEYS, stdout)) {
		fprintf(stderr, "Failed to emit document\n");
		goto cleanup;
	}

	ret = EXIT_SUCCESS;

cleanup:
	fy_document_destroy(fyd);
	return ret;
}