From 07ebb0347fc9ff410b0b93c92f6ece52568d6991 Mon Sep 17 00:00:00 2001 From: xi Date: Mon, 11 Dec 2006 19:33:21 +0000 Subject: Add `const` qualifier for `yaml_parser_set_input_string` parameter `input`. git-svn-id: http://svn.pyyaml.org/libyaml/trunk@237 18f92427-320e-0410-9341-c67f048884a3 --- include/yaml.h | 310 +++++++++++++++++++++++++++++++++++++++++++++++++++-- src/api.c | 183 ++++++++++++++++++++++++++++++- src/yaml_private.h | 36 +++++++ 3 files changed, 522 insertions(+), 7 deletions(-) diff --git a/include/yaml.h b/include/yaml.h index 8aec0be..afb62f0 100644 --- a/include/yaml.h +++ b/include/yaml.h @@ -406,9 +406,9 @@ typedef struct { } data; - /** The beginning of the token. */ + /** The beginning of the event. */ yaml_mark_t start_mark; - /** The end of the token. */ + /** The end of the event. */ yaml_mark_t end_mark; } yaml_event_t; @@ -586,6 +586,304 @@ yaml_mapping_end_event_initialize(yaml_event_t *event); YAML_DECLARE(void) yaml_event_delete(yaml_event_t *event); +/** + * @defgroup nodes Nodes + * @{ + */ + +#define YAML_NULL_TAG "tag:yaml.org,2002:null" +#define YAML_BOOL_TAG "tag:yaml.org,2002:bool" +#define YAML_STR_TAG "tag:yaml.org,2002:str" +#define YAML_INT_TAG "tag:yaml.org,2002:int" +#define YAML_FLOAT_TAG "tag:yaml.org,2002:float" +#define YAML_TIMESTAMP_TAG "tag:yaml.org,2002:timestamp" + +#define YAML_SEQ_TAG "tag:yaml.org,2002:seq" +#define YAML_MAP_TAG "tag:yaml.org,2002:map" + +#define YAML_DEFAULT_SCALAR_TAG YAML_STR_TAG +#define YAML_DEFAULT_SEQUENCE_TAG YAML_SEQ_TAG +#define YAML_DEFAULT_MAPPING_STYLE YAML_MAP_TAG + +/** Node types. */ +typedef enum { + YAML_NO_NODE, + + YAML_SCALAR_NODE, + YAML_SEQUENCE_NODE, + YAML_MAPPING_NODE +} yaml_node_type_t; + +#if 0 + +typedef struct _yaml_node_t yaml_node_item_t; + +typedef struct { + yaml_node_item_t key; + yaml_node_item_t value; +} yaml_node_pair_t; + +/** The node structure. */ +typedef struct _yaml_node_t { + + /** The node type. */ + yaml_node_type_t type; + + /* The reference counter. */ + int references; + + /** The node data. */ + union { + + /** The scalar parameters (for @c YAML_SCALAR_NODE). */ + struct { + /** The tag. */ + yaml_char_t *tag; + /** The scalar value. */ + yaml_char_t *value; + /** The length of the scalar value. */ + size_t length; + /** The scalar style. */ + yaml_scalar_style_t style; + } scalar; + + /** The sequence parameters (for @c YAML_SEQUENCE_NODE). */ + struct { + /** The tag. */ + yaml_char_t *tag; + /** The stack of sequence items. */ + struct { + /** The beginning of the stack. */ + struct yaml_node_item_t *start; + /** The end of the stack. */ + struct yaml_node_item_t *end; + /** The top of the stack. */ + struct yaml_node_item_t *top; + } items; + /** The sequence style. */ + yaml_sequence_style_t style; + } sequence; + + /** The mapping parameters (for @c YAML_MAPPING_NODE). */ + struct { + /** The tag. */ + yaml_char_t *tag; + /** The stack of mapping pairs. */ + struct { + /** The beginning of the stack. */ + struct yaml_node_pair_t *start; + /** The end of the stack. */ + struct yaml_node_pair_t *end; + /** The top of the stack. */ + struct yaml_node_pair_t *top; + } pairs; + /** The mapping style. */ + yaml_mapping_style_t style; + } mapping; + + } data; + + /** The beginning of the node. */ + yaml_mark_t start_mark; + /** The end of the node. */ + yaml_mark_t end_mark; + +} yaml_node_t; + +/** + * Create a SCALAR node. + * + * The @a style argument may be ignored by the emitter. + * + * @param[out] node An empty node object. + * @param[in] tag The scalar tag. + * @param[in] value The scalar value. + * @param[in] length The length of the scalar value. + * @param[in] style The scalar style. + * + * @returns @c 1 if the function succeeded, @c 0 on error. + */ + +YAML_DECLARE(int) +yaml_scalar_node_initialize(yaml_node_t *node, + yaml_char_t *tag, yaml_char_t *value, int length, + yaml_scalar_style_t style); + +/** + * Create a SEQUENCE node. + * + * The @a style argument may be ignored by the emitter. + * + * @param[out] node An empty node object. + * @param[in] tag The sequence tag. + * @param[in] style The sequence style. + * + * @returns @c 1 if the function succeeded, @c 0 on error. + */ + +YAML_DECLARE(int) +yaml_sequence_node_initialize(yaml_node_t *node, + yaml_char_t *tag, yaml_sequence_style_t style); + +/** + * Add an item to a SEQUENCE node + * + * @param[out] node A sequence node. + * @param[in] item An item node. +* + * @returns @c 1 if the function succeeded, @c 0 on error. + */ + +YAML_DECLARE(int) +yaml_sequence_node_add_item(yaml_node_t *node, yaml_node_t *item) + +/** + * Create a SCALAR node and add it to a SEQUENCE node. + * + * @param[out] node A sequence node. + * @param[in] tag The scalar tag. + * @param[in] value The scalar value. + * @param[in] length The length of the scalar value. + * @param[in] style The scalar style. + * + * @returns @c 1 if the function succeeded, @c 0 on error. + */ + +YAML_DECLARE(int) +yaml_sequence_node_add_scalar_item(yaml_node_t *node, + yaml_char_t *tag, yaml_char_t *value, int length, + yaml_scalar_style_t style); + +/** + * Get the number of subnodes of a SEQUENCE node. + * + * @param[in] node A sequence node. + * + * @returns the number of subnodes. + */ + +YAML_DECLARE(size_t) +yaml_sequence_node_get_length(yaml_node_t *node); + +/** + * Get a subnode of a SEQUENCE node. + * + * @param[in] node A sequence node. + * @param[in] index The index of a subnode. + * @param[out] item A subnode. + */ + +YAML_DECLARE(void) +yaml_sequence_node_get_item(yaml_node_t *node, size_t index, + yaml_node_t *item); + +/** + * Create a MAPPING node. + * + * The @a style argument may be ignored by the emitter. + * + * @param[out] node An empty node object. + * @param[in] tag The mapping tag. + * @param[in] style The mapping style. + * + * @returns @c 1 if the function succeeded, @c 0 on error. + */ + +YAML_DECLARE(int) +yaml_mapping_node_initialize(yaml_node_t *node, + yaml_char_t *tag, yaml_mapping_style_t style); + +/** + * Add a key/value pair of nodes to a MAPPING node. + * + * @param[out] node A mapping node. + * @param[in] key A key node. + * @param[in] value A value node. + * + * @returns @c 1 if the function succeeded, @c 0 on error. + */ + +YAML_DECLARE(int) +yaml_mapping_node_add_pair(yaml_node_t *node, + yaml_node_t *key, yaml_node_t *value) + +/** + * Create a scalar key and add the key/value pair to a MAPPING node. + * + * @param[out] node A mapping node. + * @param[in] key_tag The key node tag. + * @param[in] key_value The key node value. + * @param[in] key_length The length of the key node value. + * @param[in] key_style The key node style. + * @param[in] value A value node. + * + * @returns @c 1 if the function succeeded, @c 0 on error. + */ + +YAML_DECLARE(int) +yaml_sequence_node_add_scalar_key_pair(yaml_node_t *node, + yaml_char_t *key_tag, yaml_char_t *key_value, int key_length, + yaml_scalar_style_t key_style, + yaml_node_t *value); + +/** + * Create a scalar key/value nodes and add the pair to a MAPPING node. + * + * @param[out] node A mapping node. + * @param[in] key_tag The key node tag. + * @param[in] key_value The key node value. + * @param[in] key_length The length of the key node value. + * @param[in] key_style The key node style. + * @param[in] value_tag The value node tag. + * @param[in] value_value The value node value. + * @param[in] value_length The length of the value node value. + * @param[in] value_style The value node style. + * + * @returns @c 1 if the function succeeded, @c 0 on error. + */ + +YAML_DECLARE(int) +yaml_sequence_node_add_scalar_pair(yaml_node_t *node, + yaml_char_t *key_tag, yaml_char_t *key_value, int key_length, + yaml_scalar_style_t key_style, + yaml_char_t *value_tag, yaml_char_t *value_value, int value_length, + yaml_scalar_style_t value_style); + +/** + * Get the number of subnode pairs of a MAPPING node. + * + * @param[in] node A mapping node. + * + * @returns the number of pairs. + */ + +YAML_DECLARE(size_t) +yaml_mapping_node_get_length(yaml_node_t *node); + +/** + * Get a subnode of a SEQUENCE node. + * + * @param[in] node A sequence node. + * @param[in] index The index of a subnode. + * @param[out] key The key subnode. + * @param[out] value The value subnode. + */ + +YAML_DECLARE(void) +yaml_mapping_node_get_pair(yaml_node_t *node, size_t index, + yaml_node_t *key, yaml_node_t *value); + +/** + * Delete a node and its subnodes. + * + * @param[out] node A node object. + */ + +YAML_DECLARE(void) +yaml_node_delete(yaml_node_t *node); + +#endif + /** @} */ /** @@ -711,11 +1009,11 @@ typedef struct { /** String input data. */ struct { /** The string start pointer. */ - unsigned char *start; + const unsigned char *start; /** The string end pointer. */ - unsigned char *end; + const unsigned char *end; /** The string current position. */ - unsigned char *current; + const unsigned char *current; } string; /** File input data. */ @@ -908,7 +1206,7 @@ yaml_parser_delete(yaml_parser_t *parser); YAML_DECLARE(void) yaml_parser_set_input_string(yaml_parser_t *parser, - unsigned char *input, size_t size); + const unsigned char *input, size_t size); /** * Set a file input. diff --git a/src/api.c b/src/api.c index 83ca720..a2d5543 100644 --- a/src/api.c +++ b/src/api.c @@ -279,7 +279,7 @@ yaml_file_read_handler(void *data, unsigned char *buffer, size_t size, YAML_DECLARE(void) yaml_parser_set_input_string(yaml_parser_t *parser, - unsigned char *input, size_t size) + const unsigned char *input, size_t size) { assert(parser); /* Non-NULL parser object expected. */ assert(!parser->read_handler); /* You can set the source only once. */ @@ -1019,3 +1019,184 @@ yaml_event_delete(yaml_event_t *event) memset(event, 0, sizeof(yaml_event_t)); } +#if 0 + +/* + * Create a SCALAR node. + */ + +YAML_DECLARE(int) +yaml_scalar_node_initialize(yaml_node_t *node, + yaml_char_t *tag, yaml_char_t *value, int length, + yaml_scalar_style_t style) +{ + yaml_mark_t mark = { 0, 0, 0 }; + yaml_char_t *tag_copy = NULL; + yaml_char_t *value_copy = NULL; + + assert(node); /* Non-NULL node object is expected. */ + assert(value); /* Non-NULL anchor is expected. */ + + if (!tag) { + tag = 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); + + return 1; + +error: + yaml_free(tag_copy); + yaml_free(value_copy); + + return 0; +} + +/* + * Create a SEQUENCE node. + */ + +YAML_DECLARE(int) +yaml_sequence_node_initialize(yaml_node_t *node, + yaml_char_t *tag, yaml_sequence_style_t style) +{ + struct { + yaml_error_type_t error; + } context; + yaml_mark_t mark = { 0, 0, 0 }; + yaml_char_t *tag_copy = NULL; + struct { + yaml_node_item_t *start; + yaml_node_item_t *end; + yaml_node_item_t *top; + } items = { NULL, NULL, NULL }; + + assert(node); /* Non-NULL node object is expected. */ + + if (!tag) { + tag = YAML_DEFAULT_SEQUENCE_TAG; + } + + if (tag) { + if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; + tag_copy = yaml_strdup(tag); + if (!tag_copy) goto error; + } + + if (!STACK_INIT(context, items, INITIAL_STACK_SIZE)) goto error; + + SEQUENCE_NODE_INIT(*node, tag_copy, items.start, item.end, style, + mark, mark); + + return 1; + +error: + yaml_free(tag_copy); + STACK_DEL(context, items); + + return 0; +} + +/* + * Create a MAPPING node. + */ + +YAML_DECLARE(int) +yaml_mapping_node_initialize(yaml_node_t *node, + yaml_char_t *tag, yaml_mapping_style_t style) +{ + struct { + yaml_error_type_t error; + } context; + yaml_mark_t mark = { 0, 0, 0 }; + yaml_char_t *tag_copy = NULL; + struct { + yaml_node_pair_t *start; + yaml_node_pair_t *end; + yaml_node_pair_t *top; + } pairs = { NULL, NULL, NULL }; + + assert(node); /* Non-NULL node object is expected. */ + + if (!tag) { + tag = YAML_DEFAULT_MAPPING_TAG; + } + + if (tag) { + if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; + tag_copy = yaml_strdup(tag); + if (!tag_copy) goto error; + } + + if (!STACK_INIT(context, pairs, INITIAL_STACK_SIZE)) goto error; + + MAPPING_NODE_INIT(*node, tag_copy, pairs.start, pairs.end, style, + mark, mark); + + return 1; + +error: + yaml_free(tag_copy); + STACK_DEL(context, pairs); + + return 0; +} + +/* + * Delete a node and its subnodes. + */ + +YAML_DECLARE(void) +yaml_node_delete(yaml_node_t *node) +{ + struct { + yaml_error_type_t error; + } context; + struct { + yaml_node_item_t *start; + yaml_node_item_t *end; + yaml_node_item_t *head; + yaml_node_item_t *tail; + } queue = { NULL, NULL, NULL, NULL }; + + assert(node); /* Non-NULL node object is expected. */ + + if (node->type == YAML_SCALAR_NODE) { + yaml_free(node->data.scalar.tag); + yaml_free(node->data.scalar.value); + memset(node, 0, sizeof(yaml_node_t)); + return; + } + + if (!QUEUE_INIT(context, queue, INITIAL_QUEUE_SIZE)) goto error; + if (!ENQUEUE(context, queue, node)) goto error; + + while (!QUEUE_EMPTY(context, queue)) { + yaml_node_t node = DEQUEUE(context, queue); + if (node.type == YAML_SCALAR_NODE) { + if (!node->reference) + } + if (node->type == YAML_SEQUENCE_NODE) { + while (!STACK_EMPTY(context, node->data.sequence.items)) { + yaml_node_t *item = + } + } + } +} + +#endif + diff --git a/src/yaml_private.h b/src/yaml_private.h index 08a0232..3378735 100644 --- a/src/yaml_private.h +++ b/src/yaml_private.h @@ -579,3 +579,39 @@ yaml_queue_extend(void **start, void **head, void **tail, void **end); #define MAPPING_END_EVENT_INIT(event,start_mark,end_mark) \ (EVENT_INIT((event),YAML_MAPPING_END_EVENT,(start_mark),(end_mark))) +/* + * Node initializers. + */ + +#define NODE_INIT(node,node_type,node_start_mark,node_end_mark) \ + (memset(&(node), 0, sizeof(yaml_node_t)), \ + (node).type = (node_type), \ + (node).start_mark = (node_start_mark), \ + (node).end_mark = (node_end_mark)) + +#define SCALAR_NODE_INIT(node,node_tag,node_value,node_length, \ + node_style,start_mark,end_mark) \ + (EVENT_INIT((node),YAML_SCALAR_NODE,(start_mark),(end_mark)), \ + (node).data.scalar.tag = (node_tag), \ + (node).data.scalar.value = (node_value), \ + (node).data.scalar.length = (node_length), \ + (node).data.scalar.style = (node_style)) + +#define SEQUENCE_NODE_INIT(node,node_tag,node_items_start,node_items_end, \ + node_style,start_mark,end_mark) \ + (NODE_INIT((node),YAML_SEQUENCE_NODE,(start_mark),(end_mark)), \ + (node).data.sequence.tag = (node_tag), \ + (node).data.sequence.items.start = (node_items_start), \ + (node).data.sequence.items.end = (node_items_end), \ + (node).data.sequence.items.top = (node_items_start), \ + (node).data.sequence.style = (node_style)) + +#define MAPPING_NODE_INIT(node,node_tag,node_pairs_start,node_pairs_end, \ + node_style,start_mark,end_mark) \ + (NODE_INIT((node),YAML_MAPPING_NODE,(start_mark),(end_mark)), \ + (node).data.mapping.tag = (node_tag), \ + (node).data.mapping.pairs.start = (node_pairs_start), \ + (node).data.mapping.pairs.end = (node_pairs_end), \ + (node).data.mapping.pairs.top = (node_pairs_start), \ + (node).data.mapping.style = (node_style)) + -- cgit v1.2.1