source: libyaml/trunk/src/api.c @ 179

Revision 179, 3.1 KB checked in by xi, 8 years ago (diff)

Implementing Reader: first tries.

Line 
1
2#if HAVE_CONFIG_H
3#include <config.h>
4#endif
5
6#include <yaml/yaml.h>
7
8#include <assert.h>
9
10/*
11 * Allocate a dynamic memory block.
12 */
13
14void *
15yaml_malloc(size_t size)
16{
17    return malloc(size ? size : 1);
18}
19
20/*
21 * Reallocate a dynamic memory block.
22 */
23
24void *
25yaml_realloc(void *ptr, size_t size)
26{
27    return ptr ? realloc(ptr, size ? size : 1) : malloc(size ? size : 1);
28}
29
30/*
31 * Free a dynamic memory block.
32 */
33
34void
35yaml_free(void *ptr)
36{
37    if (ptr) free(ptr);
38}
39
40/*
41 * Create a new parser object.
42 */
43
44yaml_parser_t *
45yaml_parser_new(void)
46{
47    yaml_parser_t *parser;
48
49    parser = yaml_malloc(sizeof(yaml_parser_t));
50    if (!parser) return NULL;
51
52    memset(parser, 0, sizeof(yaml_parser_t));
53
54    return parser;
55}
56
57/*
58 * Destroy a parser object.
59 */
60
61void
62yaml_parser_delete(yaml_parser_t *parser)
63{
64    assert(parser); /* Non-NULL parser object expected. */
65
66    yaml_free(parser->buffer);
67    if (!parser->raw_buffer_foreign)
68        yaml_free(parser->raw_buffer);
69
70    memset(parser, 0, sizeof(yaml_parser_t));
71
72    yaml_free(parser);
73}
74
75/*
76 * String read handler (always returns error).
77 */
78
79static int
80yaml_string_read_handler(void *data, unsigned char *buffer, size_t size,
81        size_t *size_read)
82{
83    *size_read = 0;
84    return 1;
85}
86
87/*
88 * File read handler.
89 */
90
91static int
92yaml_file_read_handler(void *data, unsigned char *buffer, size_t size,
93        size_t *size_read)
94{
95    *size_read = fread(buffer, 1, size, (FILE *)ext);
96    return !ferror((FILE *)ext);
97}
98
99/*
100 * Set a string input.
101 */
102
103void
104yaml_parser_set_input_string(yaml_parser_t *parser,
105        unsigned char *input, size_t size)
106{
107    assert(parser); /* Non-NULL parser object expected. */
108    assert(!parser->reader); /* You can set the source only once. */
109    assert(input);  /* Non-NULL input string expected. */
110
111    parser->read_handler = yaml_string_read_handler;
112    parser->read_handler_data = NULL;
113
114    /* We use the input string as a raw (undecoded) buffer. */
115    parser->raw_buffer = input; 
116    parser->raw_buffer_size = size;
117    parser->raw_buffer_foreign = 1;
118}
119
120/*
121 * Set a file input.
122 */
123
124void
125yaml_parser_set_input_file(yaml_parser_t *parser, FILE *file)
126{
127    assert(parser); /* Non-NULL parser object expected. */
128    assert(!parser->reader); /* You can set the source only once. */
129    assert(file);   /* Non-NULL file object expected. */
130
131    parser->read_handler = yaml_file_read_handler;
132    parser->read_handler_data = file;
133}
134
135/*
136 * Set a generic input.
137 */
138
139void
140yaml_parser_set_input(yaml_parser_t *parser,
141        yaml_read_handler_t *handler, void *data)
142{
143    assert(parser); /* Non-NULL parser object expected. */
144    assert(!parser->reader); /* You can set the source only once. */
145    assert(handler);    /* Non-NULL read handler expected. */
146
147    parser->read_handler = handler;
148    parser->read_handler_data = data
149}
150
151/*
152 * Set the source encoding.
153 */
154
155void
156yaml_parser_set_encoding(yaml_parser_t *parser, yaml_encoding_t encoding)
157{
158    assert(parser); /* Non-NULL parser object expected. */
159    assert(!parser->encoding); /* Encoding is already set or detected. */
160
161    parser->encoding = encoding;
162}
163
Note: See TracBrowser for help on using the repository browser.