Changeset 179
- Timestamp:
- 05/27/06 13:19:07 (2 years ago)
- Files:
-
- libyaml/trunk/include/yaml/yaml.h (modified) (10 diffs)
- libyaml/trunk/src/Makefile.am (modified) (1 diff)
- libyaml/trunk/src/api.c (modified) (3 diffs)
- libyaml/trunk/src/reader.c (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
libyaml/trunk/include/yaml/yaml.h
r178 r179 17 17 18 18 #include <stdlib.h> 19 #include <stdio.h> 20 #include <string.h> 19 21 20 22 /** … … 63 65 } yaml_encoding_t; 64 66 65 /** @} */ 66 67 /* 68 67 /** Many bad things could happen with the parser and emitter. */ 69 68 typedef enum { 70 69 YAML_NO_ERROR, … … 79 78 YAML_EMITTER_ERROR 80 79 } yaml_error_type_t; 80 81 /** @} */ 82 83 /* 81 84 82 85 typedef enum { … … 250 253 * buffer. The number of written bytes should be set to the @a length variable. 251 254 * 252 * @param[in] ext A pointer to an application data specified by253 * @c yaml_parser_set_read_handler.254 * @param[out] buffer The buffer to write the data from the source.255 * @param[in] size The size of the buffer.256 * @param[out] lengthThe actual number of bytes read from the source.255 * @param[in] ext A pointer to an application data specified by 256 * @c yaml_parser_set_read_handler. 257 * @param[out] buffer The buffer to write the data from the source. 258 * @param[in] size The size of the buffer. 259 * @param[out] size_read The actual number of bytes read from the source. 257 260 * 258 261 * @returns On success, the handler should return @c 1. If the handler failed, … … 260 263 * @a length to @c 0 and return @c 1. 261 264 */ 262 typedef int yaml_read_handler_t(void *ext, yaml_char_t *buffer, size_t size, 263 size_t *length); 264 265 typedef int yaml_read_handler_t(void *ext, unsigned char *buffer, size_t size, 266 size_t *size_read); 265 267 266 268 /** … … 272 274 273 275 typedef struct { 276 277 /** 278 * @name Error handling 279 * @{ 280 */ 281 282 error_type_t error; 283 284 /** 285 * @} 286 */ 274 287 275 288 /** … … 279 292 280 293 /** Read handler */ 281 yaml_read_handler_t *read er;294 yaml_read_handler_t *read_handler; 282 295 283 296 /** A pointer for passing to the read handler. */ 284 void *read er_ext;297 void *read_handler_data; 285 298 286 299 /** EOF flag */ … … 290 303 yaml_char_t *buffer; 291 304 305 /** The size of the buffer (in bytes). */ 306 size_t buffer_size; 307 292 308 /** The pointer to the current character in the working buffer. */ 293 yaml_char_t *pointer; 309 yaml_char_t *buffer_pointer; 310 311 /** The number of unread characters in the buffer (in characters). */ 312 size_t buffer_length; 294 313 295 314 /** The remaining undecoded characters. */ 296 315 unsigned char *raw_buffer; 297 316 298 /** The size of the raw buffer . */317 /** The size of the raw buffer (in bytes). */ 299 318 size_t raw_buffer_size; 319 320 /** Is the application responsible for freeing the raw buffer? */ 321 int raw_buffer_foreign; 300 322 301 323 /** The input encoding. */ 302 324 yaml_encoding_t encoding; 325 326 /** The offset of the current position (in bytes). */ 327 size_t offset; 328 329 /** The index of the current position (in characters). */ 330 size_t index; 331 332 /** The line of the current position (starting from @c 0). */ 333 size_t line; 334 335 /** The column of the current position (starting from @c 0). */ 336 size_t column; 303 337 304 338 /** … … 329 363 yaml_parser_delete(yaml_parser_t *parser); 330 364 365 /** 366 * Set a string input. 367 * 368 * Note that the @a input pointer must be valid while the @a parser object 369 * exists. The application is responsible for destroing @a input after 370 * destroying the @a parser. 371 * 372 * @param[in] parser A parser object. 373 * @param[in] input A source data. 374 * @param[in] length The length of the source data in bytes. 375 */ 376 377 void 378 yaml_parser_set_input_string(yaml_parser_t *parser, 379 unsigned char *input, size_t size); 380 381 382 /** 383 * Set a file input. 384 * 385 * @a file should be a file object open for reading. The application is 386 * responsible for closing the @a file. 387 * 388 * @param[in] parser A parser object. 389 * @param[in] file An open file. 390 */ 391 392 void 393 yaml_parser_set_input_file(yaml_parser_t *parser, FILE *file); 394 395 /** 396 * Set a generic input handler. 397 * 398 * @param[in] parser A parser object. 399 * @param[in] handler A read handler. 400 * @param[in] data Any application data for passing to the read handler. 401 */ 402 403 void 404 yaml_parser_set_input(yaml_parser_t *parser, 405 yaml_read_handler_t *handler, void *data); 406 407 /** 408 * Set the source encoding. 409 * 410 * @param[in] encoding The source encoding. 411 */ 412 413 void 414 yaml_parser_set_encoding(yaml_parser_t *parser, yaml_encoding_t encoding); 415 331 416 /** @} */ 332 417 … … 335 420 } yaml_emitter_t; 336 421 */ 422 423 /** 424 * @defgroup internal Internal Definitions 425 * @{ 426 */ 427 428 /** 429 * Allocate a dynamic memory block. 430 * 431 * @param[in] size Size of a memory block, \c 0 is valid. 432 * 433 * @returns @c yaml_malloc returns a pointer to a newly allocated memory block, 434 * or @c NULL if it failed. 435 */ 436 437 void * 438 yaml_malloc(size_t size); 439 440 /** 441 * Reallocate a dynamic memory block. 442 * 443 * @param[in] ptr A pointer to an existing memory block, \c NULL is 444 * valid. 445 * @param[in] size A size of a new block, \c 0 is valid. 446 * 447 * @returns @c yaml_realloc returns a pointer to a reallocated memory block, 448 * or @c NULL if it failed. 449 */ 450 451 void * 452 yaml_realloc(void *ptr, size_t size); 453 454 /** 455 * Free a dynamic memory block. 456 * 457 * @param[in] ptr A pointer to an existing memory block, \c NULL is 458 * valid. 459 */ 460 461 void 462 yaml_free(void *ptr); 463 464 /** @} */ 465 337 466 338 467 #ifdef __cplusplus libyaml/trunk/src/Makefile.am
r178 r179 1 1 AM_CPPFLAGS = -I$(top_srcdir)/include 2 2 lib_LTLIBRARIES = libyaml.la 3 libyaml_la_SOURCES = version.c api.c 3 libyaml_la_SOURCES = version.c api.c reader.c 4 4 libyaml_la_LDFLAGS = -release $(YAML_LT_RELEASE) -version-info $(YAML_LT_CURRENT):$(YAML_LT_REVISION):$(YAML_LT_AGE) libyaml/trunk/src/api.c
r178 r179 6 6 #include <yaml/yaml.h> 7 7 8 #include <assert.h> 9 8 10 /* 9 * Create a new parser. 11 * Allocate a dynamic memory block. 12 */ 13 14 void * 15 yaml_malloc(size_t size) 16 { 17 return malloc(size ? size : 1); 18 } 19 20 /* 21 * Reallocate a dynamic memory block. 22 */ 23 24 void * 25 yaml_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 34 void 35 yaml_free(void *ptr) 36 { 37 if (ptr) free(ptr); 38 } 39 40 /* 41 * Create a new parser object. 10 42 */ 11 43 … … 15 47 yaml_parser_t *parser; 16 48 17 parser = malloc(sizeof(yaml_parser_t));49 parser = yaml_malloc(sizeof(yaml_parser_t)); 18 50 if (!parser) return NULL; 19 51 … … 30 62 yaml_parser_delete(yaml_parser_t *parser) 31 63 { 32 free(parser); 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); 33 73 } 34 74 75 /* 76 * String read handler (always returns error). 77 */ 78 79 static int 80 yaml_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 91 static int 92 yaml_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 103 void 104 yaml_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 124 void 125 yaml_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 139 void 140 yaml_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 155 void 156 yaml_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
