Changeset 183 for libyaml/trunk/src/reader.c
- Timestamp:
- 06/02/06 09:03:14 (7 years ago)
- File:
-
- 1 edited
-
libyaml/trunk/src/reader.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libyaml/trunk/src/reader.c
r182 r183 12 12 */ 13 13 14 int14 static int 15 15 yaml_parser_set_reader_error(yaml_parser_t *parser, const char *problem, 16 16 size_t offset, int value) … … 24 24 } 25 25 26 /* 27 * Update the raw buffer. 28 */ 29 30 static int 31 yaml_parser_update_raw_buffer(yaml_parser_t *parser) 32 { 33 size_t size_read = 0; 34 35 /* Return if the raw buffer is full. */ 36 37 if (parser->raw_unread == YAML_RAW_BUFFER_SIZE) return 1; 38 39 /* Return on EOF. */ 40 41 if (parser->eof) return 1; 42 43 /* Move the remaining bytes in the raw buffer to the beginning. */ 44 45 if (parser->raw_unread && parser->raw_buffer < parser->raw_pointer) { 46 memmove(parser->raw_buffer, parser->raw_pointer, parser->raw_unread); 47 } 48 parser->raw_pointer = parser->raw_buffer; 49 50 /* Call the read handler to fill the buffer. */ 51 52 if (!parser->read_handler(parser->read_handler_data, 53 parser->raw_buffer + parser->raw_unread, 54 YAML_RAW_BUFFER_SIZE - parser->raw_unread, 55 &size_read)) { 56 return yaml_parser_set_reader_error(parser, "Input error", 57 parser->offset, -1); 58 } 59 parser->raw_unread += size_read; 60 if (!size_read) { 61 parser->eof = 1; 62 } 63 64 return 1; 65 } 66 67 /* 68 * Determine the input stream encoding by checking the BOM symbol. If no BOM is 69 * found, the UTF-8 encoding is assumed. Return 1 on success, 0 on failure. 70 */ 71 72 #define BOM_UTF8 "\xef\xbb\xbf" 73 #define BOM_UTF16LE "\xff\xfe" 74 #define BOM_UTF16BE "\xfe\xff" 75 76 static int 77 yaml_parser_determine_encoding(yaml_parser_t *parser) 78 { 79 /* Ensure that we had enough bytes in the raw buffer. */ 80 81 while (!parser->eof && parser->raw_unread < 3) { 82 if (!yaml_parser_update_raw_buffer(parser)) { 83 return 0; 84 } 85 } 86 87 /* Determine the encoding. */ 88 89 if (parser->raw_unread >= 2 90 && !memcmp(parser->raw_pointer, BOM_UTF16LE, 2)) { 91 parser->encoding = YAML_UTF16LE_ENCODING; 92 parser->raw_pointer += 2; 93 parser->raw_unread -= 2; 94 parser->offset += 2; 95 } 96 else if (parser->raw_unread >= 2 97 && !memcmp(parser->raw_pointer, BOM_UTF16BE, 2)) { 98 parser->encoding = YAML_UTF16BE_ENCODING; 99 parser->raw_pointer += 2; 100 parser->raw_unread -= 2; 101 parser->offset += 2; 102 } 103 else if (parser->raw_unread >= 3 104 && !memcmp(parser->raw_pointer, BOM_UTF8, 3)) { 105 parser->encoding = YAML_UTF8_ENCODING; 106 parser->raw_pointer += 3; 107 parser->raw_unread -= 3; 108 parser->offset += 3; 109 } 110 else { 111 parser->encoding = YAML_UTF8_ENCODING; 112 } 113 114 return 1; 115 } 26 116 27 117 /* … … 32 122 */ 33 123 34 int 124 YAML_DECLARE(int) 35 125 yaml_parser_update_buffer(yaml_parser_t *parser, size_t length) 36 126 { … … 346 436 } 347 437 348 /*349 * Determine the input stream encoding by checking the BOM symbol. If no BOM is350 * found, the UTF-8 encoding is assumed. Return 1 on success, 0 on failure.351 */352 353 #define BOM_UTF8 "\xef\xbb\xbf"354 #define BOM_UTF16LE "\xff\xfe"355 #define BOM_UTF16BE "\xfe\xff"356 357 int358 yaml_parser_determine_encoding(yaml_parser_t *parser)359 {360 /* Ensure that we had enough bytes in the raw buffer. */361 362 while (!parser->eof && parser->raw_unread < 3) {363 if (!yaml_parser_update_raw_buffer(parser)) {364 return 0;365 }366 }367 368 /* Determine the encoding. */369 370 if (parser->raw_unread >= 2371 && !memcmp(parser->raw_pointer, BOM_UTF16LE, 2)) {372 parser->encoding = YAML_UTF16LE_ENCODING;373 parser->raw_pointer += 2;374 parser->raw_unread -= 2;375 parser->offset += 2;376 }377 else if (parser->raw_unread >= 2378 && !memcmp(parser->raw_pointer, BOM_UTF16BE, 2)) {379 parser->encoding = YAML_UTF16BE_ENCODING;380 parser->raw_pointer += 2;381 parser->raw_unread -= 2;382 parser->offset += 2;383 }384 else if (parser->raw_unread >= 3385 && !memcmp(parser->raw_pointer, BOM_UTF8, 3)) {386 parser->encoding = YAML_UTF8_ENCODING;387 parser->raw_pointer += 3;388 parser->raw_unread -= 3;389 parser->offset += 3;390 }391 else {392 parser->encoding = YAML_UTF8_ENCODING;393 }394 395 return 1;396 }397 398 /*399 * Update the raw buffer.400 */401 402 int403 yaml_parser_update_raw_buffer(yaml_parser_t *parser)404 {405 size_t size_read = 0;406 407 /* Return if the raw buffer is full. */408 409 if (parser->raw_unread == YAML_RAW_BUFFER_SIZE) return 1;410 411 /* Return on EOF. */412 413 if (parser->eof) return 1;414 415 /* Move the remaining bytes in the raw buffer to the beginning. */416 417 if (parser->raw_unread && parser->raw_buffer < parser->raw_pointer) {418 memmove(parser->raw_buffer, parser->raw_pointer, parser->raw_unread);419 }420 parser->raw_pointer = parser->raw_buffer;421 422 /* Call the read handler to fill the buffer. */423 424 if (!parser->read_handler(parser->read_handler_data,425 parser->raw_buffer + parser->raw_unread,426 YAML_RAW_BUFFER_SIZE - parser->raw_unread,427 &size_read)) {428 return yaml_parser_set_reader_error(parser, "Input error",429 parser->offset, -1);430 }431 parser->raw_unread += size_read;432 if (!size_read) {433 parser->eof = 1;434 }435 436 return 1;437 }438
Note: See TracChangeset
for help on using the changeset viewer.
