wiki:LibSyck

Syck C API

Stolen from syck/README.ext, syck/lib/syck.h, syck/lib/syck.c, and syck/lib/emitter.c.

SyckParser?

#include <syck.h>

SyckParser *p = syck_new_parser(); /* Creates a new Syck parser. */

void *extension;
p.bonus = extension; /* Sets the pointer for extension's use. */

syck_parser_implicit_typing(p, 1); /* Toggles implicit typing of builtin YAML types. Default is on. */
syck_parser_taguri_expansion(p, 1); /* Toggles expansion of types in full taguri. Default is on (recommended). */

SYMID node_handler(SyckParser *p, SyckNode *n);
syck_parser_handler(p, node_handler); /*  Assign a callback function as a node handler. */

void error_handler(SyckParser *p, char *str);
syck_parser_error_handler(p, error_handler); /* Assign a callback function as an error handler. */

SyckNode *bad_anchor_handler(SyckParser *p, char *anchor);
syck_parser_bad_anchor_handler(p, bad_anchor_handler); /* Assign a callback function as a bad anchor handler. */

syck_parser_str(p, ptr, len, NULL); /* Assigns a string as the IO source with the default handler. */
/* or */
long io_file_read(char *buf, SyckIoFile *str, long max_size, long skip);
syck_parser_file(p, file, io_file_read); /* Assigns a file as the IO source with a callback function. */

SYMID document = syck_parse(p); /* Parses a single document from the YAML stream. */
/* or */
while ((document = syck_parse(p))); /* Parses all documents from the YAML stream. */

syck_free_parser(p); /* Frees the parser. */

SyckIoFileRead?

/*
 * Default FILE IO function
 */
long
syck_io_file_read( char *buf, SyckIoFile *file, long max_size, long skip )
{
    long len = 0;

    ASSERT( file != NULL );

    max_size -= skip;
    len = fread( buf + skip, sizeof( char ), max_size, file->ptr );
    len += skip;
    buf[len] = '\0';

    return len;
}

SyckNodeHandler?

SyckNode *n;
/* ... */

char *type_id = n.type_id;
char *anchor = n.anchor;

switch (n->kind) {

    case syck_str_kind: /* Loads a scalar node. */
        char *ptr = n->data.str->ptr;
        long len = n->data.str->ptr;
        /* ... */
        break;

    case syck_seq_kind: /* Loads a sequence node. */
        for (long i = 0; i < syck_seq_count(n); i++) {
            SYMID val = sym_seq_read(n, i);
            /* ... */
        }
        break;

    case syck_map_kind: /* Loads a mapping node. */
        for (long i = 0; i < syck_map_count(n); i++) {
            SYMID key = sym_map_read(n, map_key, i);
            SYMID val = sym_map_read(n, map_value, i);
            /* ... */
        }
        break;
}

SyckEmitter?

#include <syck.h>

SyckEmitter *e = syck_new_emitter(); /* Creates a new Syck emitter. */

void *extension;
e.bonus = extension; /* Sets the pointer for extension's use. */

e->headless = 0; /* Headerless doc flag. */
e->use_header = 0; /* Force header? */
e->use_version = 0; /* Force version? */
e->sort_keys = 0; /* Sort hash keys -- not used. */
e->anchor_format = "id%03d"; /* Anchor format -- dangerous. */
e->explicit_typing = 0; /* Explicit typing on all collections? */
e->best_width = 80; /* Best width on folded scalars. */
e->style = scalar_none; /* Use literal or folded blocks on all text? -- scalar_fold or any other value. */
e->indent = 2; /* Default indentation. */

void output_handler(SyckEmitter *e, char *ptr, long len);
syck_output_handler(e, output_handler); /* Assign a callback as the output handler */

void emitter_handler(SyckEmitter *e, st_data_t node_id);
syck_emitter_handler(e, emitter_handler); /* Assign a callback as the emitter handler */

syck_emitter_ignore_id(e, sym_id); /* Not used. */

st_data_t node_id = 0;
syck_emitter_mark_node(e, node_id); /* Add a node to the symbol table. Allocate an anchor if it has repeated. */
/* ... */

syck_emit(e, root_node_id); /* Start emitting from the given node. */
syck_emitter_flush(e, 0); /* Flush the internal buffer. */

/* ... */

syck_free_emitter(e); /* Frees the emitter. */

SyckEmitterHandler?

void
emitter_handler(SyckEmitter *e, st_data_t node_id)
{

    /* ... */
    syck_emit_scalar(e, tag, style, indent, width, chomp, str, len); /* Emit scalar. */

    /* ... */
    syck_emit_seq(e, tag, style); /* Emit sequence. */
    for (/* ... */) {
        syck_emit_item(e, item_id);
    }
    syck_emit_end(e);

    /* ... */
    syck_emit_map(e, tag, style); /* Emit mapping. */
    for (/* ... */) {
        syck_emit_item(e, key_id);
        syck_emit_item(e, value_id);
    }
    syck_emit_end(e);
}