1 /* A very simple decoding test case, using person.proto.
2 * Produces output compatible with protoc --decode.
3 * Reads the encoded data from stdin and prints the values
6 * Run e.g. ./test_encode1 | ./test_decode1
10 #include <pb_decode.h>
11 #include "person.pb.h"
12 #include "test_helpers.h"
14 /* This function is called once from main(), it handles
15 the decoding and printing. */
16 bool print_person(pb_istream_t *stream)
19 Person person = Person_init_zero;
21 if (!pb_decode(stream, Person_fields, &person))
24 /* Now the decoding is done, rest is just to print stuff out. */
26 printf("name: \"%s\"\n", person.name);
27 printf("id: %ld\n", (long)person.id);
30 printf("email: \"%s\"\n", person.email);
32 for (i = 0; i < person.phone_count; i++)
34 Person_PhoneNumber *phone = &person.phone[i];
36 printf(" number: \"%s\"\n", phone->number);
42 case Person_PhoneType_WORK:
43 printf(" type: WORK\n");
46 case Person_PhoneType_HOME:
47 printf(" type: HOME\n");
50 case Person_PhoneType_MOBILE:
51 printf(" type: MOBILE\n");
63 uint8_t buffer[Person_size];
67 /* Read the data into buffer */
68 SET_BINARY_MODE(stdin);
69 count = fread(buffer, 1, sizeof(buffer), stdin);
73 printf("Message does not fit in buffer\n");
77 /* Construct a pb_istream_t for reading from the buffer */
78 stream = pb_istream_from_buffer(buffer, count);
80 /* Decode and print out the stuff */
81 if (!print_person(&stream))
83 printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));