1 /* Same as test_decode1 but reads from stdin directly.
8 /* This function is called once from main(), it handles
9 the decoding and printing.
10 Ugly copy-paste from test_decode1.c. */
11 bool print_person(pb_istream_t *stream)
16 if (!pb_decode(stream, Person_fields, &person))
19 /* Now the decoding is done, rest is just to print stuff out. */
21 printf("name: \"%s\"\n", person.name);
22 printf("id: %ld\n", (long)person.id);
25 printf("email: \"%s\"\n", person.email);
27 for (i = 0; i < person.phone_count; i++)
29 Person_PhoneNumber *phone = &person.phone[i];
31 printf(" number: \"%s\"\n", phone->number);
37 case Person_PhoneType_WORK:
38 printf(" type: WORK\n");
41 case Person_PhoneType_HOME:
42 printf(" type: HOME\n");
45 case Person_PhoneType_MOBILE:
46 printf(" type: MOBILE\n");
56 /* This binds the pb_istream_t to stdin */
57 bool callback(pb_istream_t *stream, uint8_t *buf, size_t count)
59 FILE *file = (FILE*)stream->state;
62 status = (fread(buf, 1, count, file) == count);
65 stream->bytes_left = 0;
72 /* Maximum size is specified to prevent infinite length messages from
73 * hanging this in the fuzz test.
75 pb_istream_t stream = {&callback, stdin, 10000};
76 if (!print_person(&stream))
78 printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));