File: event-streaming.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 (117 lines) | stat: -rw-r--r-- 2,437 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
/*
 * event-streaming.c - Event-based streaming parser example
 *
 * Demonstrates:
 * - Event-based (streaming) YAML parsing
 * - Processing different event types
 * - Memory-efficient parsing of large files
 * - Getting values from scalar events
 *
 * 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_parser *fyp = NULL;
	struct fy_event *fye = NULL;
	const char *input_file = (argc > 1) ? argv[1] : "config.yaml";
	int indent = 0;
	int ret = EXIT_FAILURE;

	// Create parser
	fyp = fy_parser_create(NULL);
	if (!fyp) {
		fprintf(stderr, "Failed to create parser\n");
		return EXIT_FAILURE;
	}

	// Set input file
	if (fy_parser_set_input_file(fyp, input_file)) {
		fprintf(stderr, "Failed to set input file: %s\n", input_file);
		fprintf(stderr, "Note: Create config.yaml or pass file as argument\n");
		goto cleanup;
	}

	printf("Parsing events from: %s\n\n", input_file);

	// Process events
	while ((fye = fy_parser_parse(fyp)) != NULL) {
		enum fy_event_type type = fye->type;

		// Print indentation
		for (int i = 0; i < indent; i++)
			printf("  ");

		switch (type) {
		case FYET_STREAM_START:
			printf("STREAM-START\n");
			break;

		case FYET_STREAM_END:
			printf("STREAM-END\n");
			break;

		case FYET_DOCUMENT_START:
			printf("DOCUMENT-START\n");
			indent++;
			break;

		case FYET_DOCUMENT_END:
			indent--;
			printf("DOCUMENT-END\n");
			break;

		case FYET_MAPPING_START:
			printf("MAPPING-START\n");
			indent++;
			break;

		case FYET_MAPPING_END:
			indent--;
			printf("MAPPING-END\n");
			break;

		case FYET_SEQUENCE_START:
			printf("SEQUENCE-START\n");
			indent++;
			break;

		case FYET_SEQUENCE_END:
			indent--;
			printf("SEQUENCE-END\n");
			break;

		case FYET_SCALAR: {
			// Get the scalar value
			const char *value = fy_token_get_text0(fy_event_get_token(fye));
			printf("SCALAR: \"%s\"\n", value ? value : "(null)");
			break;
		}

		case FYET_ALIAS: {
			const char *anchor = fy_token_get_text0(fy_event_get_token(fye));
			printf("ALIAS: *%s\n", anchor ? anchor : "(null)");
			break;
		}

		default:
			printf("OTHER-EVENT (type=%d)\n", type);
			break;
		}

		fy_parser_event_free(fyp, fye);
	}

	printf("\nParsing completed successfully\n");
	ret = EXIT_SUCCESS;

cleanup:
	fy_parser_destroy(fyp);
	return ret;
}