| 1 | /* Test program for libyaml 0.1.2, ticket #123 |
|---|
| 2 | ** |
|---|
| 3 | ** Run with "./bug123" to trigger the bug (the reader returns all in one all). |
|---|
| 4 | ** Run with "./bug123 x" to make it split the data into two reads. |
|---|
| 5 | ** |
|---|
| 6 | ** Per-Erik Martin (pem@foxt.com) 2009-04-24 |
|---|
| 7 | ** |
|---|
| 8 | */ |
|---|
| 9 | |
|---|
| 10 | #include <stdlib.h> |
|---|
| 11 | #include <stdio.h> |
|---|
| 12 | #include <yaml.h> |
|---|
| 13 | |
|---|
| 14 | #define START "---\r\n" |
|---|
| 15 | #define REST \ |
|---|
| 16 | "!!Abcd\r\nefghijkl: mnopqrst\r\nuvwxyzabcd: {efgh: 'ijklmnopq'}\r\n...\r\n" |
|---|
| 17 | #define ALL (START REST) |
|---|
| 18 | #define START_LEN (sizeof(START)-1) |
|---|
| 19 | #define REST_LEN (sizeof(REST)-1) |
|---|
| 20 | #define ALL_LEN (sizeof(ALL)-1) |
|---|
| 21 | |
|---|
| 22 | static int |
|---|
| 23 | the_reader(void *data, unsigned char *buffer, size_t size, |
|---|
| 24 | size_t *size_read) |
|---|
| 25 | { |
|---|
| 26 | static int count = 0; |
|---|
| 27 | int split = *((int *)data); |
|---|
| 28 | |
|---|
| 29 | if (split) |
|---|
| 30 | { /* Split into two reads */ |
|---|
| 31 | switch (count++) |
|---|
| 32 | { |
|---|
| 33 | case 0: |
|---|
| 34 | printf("Split call 1 (good)\n"); |
|---|
| 35 | strncpy((char *)buffer, START, size); |
|---|
| 36 | *size_read = (START_LEN > size ? size : START_LEN); |
|---|
| 37 | break; |
|---|
| 38 | case 1: |
|---|
| 39 | printf("Split call 2 (good)\n"); |
|---|
| 40 | strncpy((char *)buffer, REST, size); |
|---|
| 41 | *size_read = (REST_LEN > size ? size : REST_LEN); |
|---|
| 42 | break; |
|---|
| 43 | default: |
|---|
| 44 | /* Shouldn't happen */ |
|---|
| 45 | printf("Split call %d (bad)\n", count); |
|---|
| 46 | return 0; |
|---|
| 47 | } |
|---|
| 48 | } |
|---|
| 49 | else |
|---|
| 50 | { |
|---|
| 51 | if (count++) |
|---|
| 52 | { |
|---|
| 53 | /* Shouldn't happen, but it does */ |
|---|
| 54 | printf("No-split call %d (bad)\n", count); |
|---|
| 55 | return 0; |
|---|
| 56 | } |
|---|
| 57 | /* First call returns the entire document */ |
|---|
| 58 | printf("No-split call 1 (good)\n"); |
|---|
| 59 | strncpy((char *)buffer, ALL, size); |
|---|
| 60 | *size_read = (ALL_LEN > size ? size : ALL_LEN); |
|---|
| 61 | } |
|---|
| 62 | return 1; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | int |
|---|
| 66 | main(int argc, char **argv) |
|---|
| 67 | { |
|---|
| 68 | yaml_document_t document; |
|---|
| 69 | yaml_parser_t parser; |
|---|
| 70 | int split = (argc > 1); /* Any argument will make it split the reads */ |
|---|
| 71 | |
|---|
| 72 | if (! yaml_parser_initialize(&parser)) |
|---|
| 73 | { |
|---|
| 74 | fprintf(stderr, "yaml_parser_initialize() failed\n"); |
|---|
| 75 | exit(1); |
|---|
| 76 | } |
|---|
| 77 | yaml_parser_set_input(&parser, the_reader, &split); |
|---|
| 78 | if (! yaml_parser_load(&parser, &document)) |
|---|
| 79 | { |
|---|
| 80 | fprintf(stderr, "yaml_parser_load() failed\n"); |
|---|
| 81 | exit(1); |
|---|
| 82 | } |
|---|
| 83 | printf("Got it\n"); |
|---|
| 84 | yaml_document_delete(&document); |
|---|
| 85 | yaml_parser_delete(&parser); |
|---|
| 86 | exit(0); |
|---|
| 87 | } |
|---|