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"
13 /* This function is called once from main(), it handles
14 the decoding and printing. */
15 bool print_person(pb_istream_t *stream)
20 if (!pb_decode(stream, Person_fields, &person))
23 /* Now the decoding is done, rest is just to print stuff out. */
25 printf("name: \"%s\"\n", person.name);
26 printf("id: %ld\n", (long)person.id);
29 printf("email: \"%s\"\n", person.email);
31 for (i = 0; i < person.phone_count; i++)
33 Person_PhoneNumber *phone = &person.phone[i];
35 printf(" number: \"%s\"\n", phone->number);
41 case Person_PhoneType_WORK:
42 printf(" type: WORK\n");
45 case Person_PhoneType_HOME:
46 printf(" type: HOME\n");
49 case Person_PhoneType_MOBILE:
50 printf(" type: MOBILE\n");
62 /* Read the data into buffer */
64 size_t count = fread(buffer, 1, sizeof(buffer), stdin);
66 /* Construct a pb_istream_t for reading from the buffer */
67 pb_istream_t stream = pb_istream_from_buffer(buffer, count);
69 /* Decode and print out the stuff */
70 if (!print_person(&stream))
72 printf("Parsing failed.\n");