Function yaml_document_add_scalar

Synopsis

#include <include/yaml.h>

int yaml_document_add_scalar(yaml_document_t *document, const yaml_char_t *tag, const yaml_char_t *value, int length, yaml_scalar_style_t style)

Description

Create a SCALAR node and attach it to the document.

The style argument may be ignored by the emitter.

Parameters:

[ in,out ] document - A document object.

[ in ] tag - The scalar tag.

[ in ] value - The scalar value.

[ in ] length - The length of the scalar value.

[ in ] style - The scalar style.

Return
the node id or 0 on error.
Add a scalar node to a document.

Source

Lines 1194-1238 in src/api.c. Line 898 in include/yaml.h.

YAML_DECLARE(int)
yaml_document_add_scalar(yaml_document_t *document,
        const yaml_char_t *tag, const yaml_char_t *value, int length,
        yaml_scalar_style_t style)
{
    struct {
        yaml_error_type_t error;
    } context;
    yaml_mark_t mark = { 0, 0, 0 };
    yaml_char_t *tag_copy = NULL;
    yaml_char_t *value_copy = NULL;
    yaml_node_t node;

    assert(document);   /* Non-NULL document object is expected. */
    assert(value);      /* Non-NULL value is expected. */

    if (!tag) {
        tag = (yaml_char_t *)YAML_DEFAULT_SCALAR_TAG;
    }

    if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error;
    tag_copy = yaml_strdup(tag);
    if (!tag_copy) goto error;

    if (length < 0) {
        length = strlen((char *)value);
    }

    if (!yaml_check_utf8(value, length)) goto error;
    value_copy = YAML_MALLOC(length+1);
    if (!value_copy) goto error;
    memcpy(value_copy, value, length);
    value_copy[length] = '\0';

    SCALAR_NODE_INIT(node, tag_copy, value_copy, length, style, mark, mark);
    if (!PUSH(&context, document->nodes, node)) goto error;

    return document->nodes.top - document->nodes.start;

error:
    yaml_free(tag_copy);
    yaml_free(value_copy);

    return 0;
}





Add Discussion as Guest

Log in to DocsForge