C:
Working with YAML

How to:

Working with YAML in C requires a library, as the standard C library does not provide direct support for YAML parsing or serialization. One of the most popular YAML libraries for C is libyaml, which offers both low-level and high-level interfaces for parsing and emitting YAML. Below is an example of how to parse a simple YAML file using libyaml:

First, you need to install the libyaml library. If you’re on a Unix-like system, you can usually install it via your package manager. For example, on Ubuntu:

sudo apt-get install libyaml-dev

Next, consider a simple YAML file named config.yaml:

name: John Doe
age: 29
married: false

Here’s a basic example of how to parse this YAML file in C:

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

void process_yaml_file(const char *filename) {
    FILE *fh = fopen(filename, "rb");
    yaml_parser_t parser;
    yaml_event_t event;

    if (!yaml_parser_initialize(&parser))
        fputs("Failed to initialize YAML parser!\n", stderr);

    if (fh == NULL)
        fputs("Failed to open file!\n", stderr);

    yaml_parser_set_input_file(&parser, fh);

    while (1) {
        if (!yaml_parser_parse(&parser, &event))
            break;

        if (event.type == YAML_SCALAR_EVENT) {
            printf("Value: %s\n", event.data.scalar.value);
        }

        if (event.type == YAML_STREAM_END_EVENT)
            break;

        yaml_event_delete(&event);
    }

    yaml_parser_delete(&parser);
    fclose(fh);
}

int main() {
    process_yaml_file("config.yaml");
    return 0;
}

This simple program opens a YAML file, initializes the YAML parser, and reads the file, printing the scalar values (in this example, the fields of our simple YAML). Note that error checking is minimal in this simple example and should be more robust in production code.

Running the program with our config.yaml will output:

Value: John Doe
Value: 29
Value: false

Deep Dive

YAML was first released in 2001 and designed to be more readable and user-friendly than other data serialization formats like XML or JSON, borrowing from several languages like C, Perl, and Python for its design philosophy. Despite its advantages in readability and ease of human modification, YAML can be complex to parse programmatically due to its reliance on indentation and its extensive feature set, including references and custom types.

While libyaml provides robust, low-level access to parsing and emitting YAML in C, it can be cumbersome for simple tasks due to its verbose API. For these reasons, some programmers prefer to use higher-level libraries or even other data serialization formats like JSON when working in C, especially when performant parsing with minimal code overhead is a priority. However, YAML remains a popular choice for configuration files and situations where human readability is paramount. Alternatives like TinyYAML or embedding a high-level interpreter (e.g., embedding Python or Lua) could provide more convenience for specific applications, balancing between ease of use and performance needs.