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

Revision 180, 3.9 KB checked in by xi, 8 years ago (diff)

Working on the decoding code.

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    /* Allocate the parser structure. */
50
51    parser = yaml_malloc(sizeof(yaml_parser_t));
52    if (!parser) return NULL;
53
54    memset(parser, 0, sizeof(yaml_parser_t));
55
56    /* Allocate the raw buffer. */
57
58    parser->raw_buffer = yaml_malloc(YAML_RAW_BUFFER_SIZE);
59    if (!parser->raw_buffer) {
60        yaml_free(parser);
61        return NULL;
62    }
63    parser->raw_pointer = parser->raw_buffer;
64    parser->raw_unread = 0;
65
66    /* Allocate the character buffer. */
67
68    parser->buffer = yaml_malloc(YAML_BUFFER_SIZE);
69    if (!parser->buffer) {
70        yaml_free(parser->raw_buffer);
71        yaml_free(parser);
72        return NULL;
73    }
74    parser->buffer_end = parser->buffer;
75    parser->pointer = parser->buffer;
76    parser->unread = 0;
77
78    return parser;
79}
80
81/*
82 * Destroy a parser object.
83 */
84
85void
86yaml_parser_delete(yaml_parser_t *parser)
87{
88    assert(parser); /* Non-NULL parser object expected. */
89
90    yaml_free(parser->buffer);
91    yaml_free(parser->raw_buffer);
92
93    memset(parser, 0, sizeof(yaml_parser_t));
94
95    yaml_free(parser);
96}
97
98/*
99 * String read handler.
100 */
101
102static int
103yaml_string_read_handler(void *data, unsigned char *buffer, size_t size,
104        size_t *size_read)
105{
106    yaml_string_input_t *input = data;
107
108    if (input->current == input->end) {
109        *size_read = 0;
110        return 1;
111    }
112
113    if (size > (input->end - input->current)) {
114        size = input->end - input->current;
115    }
116
117    memcpy(buffer, input->current, size);
118    input->current += size;
119    *size_read = size;
120    return 1;
121}
122
123/*
124 * File read handler.
125 */
126
127static int
128yaml_file_read_handler(void *data, unsigned char *buffer, size_t size,
129        size_t *size_read)
130{
131    *size_read = fread(buffer, 1, size, (FILE *)data);
132    return !ferror((FILE *)data);
133}
134
135/*
136 * Set a string input.
137 */
138
139void
140yaml_parser_set_input_string(yaml_parser_t *parser,
141        unsigned char *input, size_t size)
142{
143    assert(parser); /* Non-NULL parser object expected. */
144    assert(!parser->read_handler);  /* You can set the source only once. */
145    assert(input);  /* Non-NULL input string expected. */
146
147    parser->string_input.start = input;
148    parser->string_input.current = input;
149    parser->string_input.end = input+size;
150
151    parser->read_handler = yaml_string_read_handler;
152    parser->read_handler_data = &parser->string_input;
153}
154
155/*
156 * Set a file input.
157 */
158
159void
160yaml_parser_set_input_file(yaml_parser_t *parser, FILE *file)
161{
162    assert(parser); /* Non-NULL parser object expected. */
163    assert(!parser->read_handler);  /* You can set the source only once. */
164    assert(file);   /* Non-NULL file object expected. */
165
166    parser->read_handler = yaml_file_read_handler;
167    parser->read_handler_data = file;
168}
169
170/*
171 * Set a generic input.
172 */
173
174void
175yaml_parser_set_input(yaml_parser_t *parser,
176        yaml_read_handler_t *handler, void *data)
177{
178    assert(parser); /* Non-NULL parser object expected. */
179    assert(!parser->read_handler);  /* You can set the source only once. */
180    assert(handler);    /* Non-NULL read handler expected. */
181
182    parser->read_handler = handler;
183    parser->read_handler_data = data;
184}
185
186/*
187 * Set the source encoding.
188 */
189
190void
191yaml_parser_set_encoding(yaml_parser_t *parser, yaml_encoding_t encoding)
192{
193    assert(parser); /* Non-NULL parser object expected. */
194    assert(!parser->encoding); /* Encoding is already set or detected. */
195
196    parser->encoding = encoding;
197}
198
Note: See TracBrowser for help on using the repository browser.