1 /* Same as test_decode1 but reads from stdin directly.
7 #include "test_helpers.h"
9 /* This function is called once from main(), it handles
10 the decoding and printing.
11 Ugly copy-paste from test_decode1.c. */
12 bool print_person(pb_istream_t *stream)
15 Person person = Person_init_zero;
17 if (!pb_decode(stream, Person_fields, &person))
20 /* Now the decoding is done, rest is just to print stuff out. */
22 printf("name: \"%s\"\n", person.name);
23 printf("id: %ld\n", (long)person.id);
26 printf("email: \"%s\"\n", person.email);
28 for (i = 0; i < person.phone_count; i++)
30 Person_PhoneNumber *phone = &person.phone[i];
32 printf(" number: \"%s\"\n", phone->number);
38 case Person_PhoneType_WORK:
39 printf(" type: WORK\n");
42 case Person_PhoneType_HOME:
43 printf(" type: HOME\n");
46 case Person_PhoneType_MOBILE:
47 printf(" type: MOBILE\n");
57 /* This binds the pb_istream_t to stdin */
58 bool callback(pb_istream_t *stream, uint8_t *buf, size_t count)
60 FILE *file = (FILE*)stream->state;
63 status = (fread(buf, 1, count, file) == count);
66 stream->bytes_left = 0;
73 pb_istream_t stream = {&callback, NULL, SIZE_MAX};
75 SET_BINARY_MODE(stdin);
77 if (!print_person(&stream))
79 printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));