Handle unsupported extension field types more gracefully.
[apps/agl-service-can-low-level.git] / tests / test_decode1.c
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
4  * to stdout as text.
5  *
6  * Run e.g. ./test_encode1 | ./test_decode1
7  */
8
9 #include <stdio.h>
10 #include <pb_decode.h>
11 #include "person.pb.h"
12
13 /* This function is called once from main(), it handles
14    the decoding and printing. */
15 bool print_person(pb_istream_t *stream)
16 {
17     int i;
18     Person person;
19     
20     if (!pb_decode(stream, Person_fields, &person))
21         return false;
22     
23     /* Now the decoding is done, rest is just to print stuff out. */
24
25     printf("name: \"%s\"\n", person.name);
26     printf("id: %ld\n", (long)person.id);
27     
28     if (person.has_email)
29         printf("email: \"%s\"\n", person.email);
30     
31     for (i = 0; i < person.phone_count; i++)
32     {
33         Person_PhoneNumber *phone = &person.phone[i];
34         printf("phone {\n");
35         printf("  number: \"%s\"\n", phone->number);
36         
37         if (phone->has_type)
38         {
39             switch (phone->type)
40             {
41                 case Person_PhoneType_WORK:
42                     printf("  type: WORK\n");
43                     break;
44                 
45                 case Person_PhoneType_HOME:
46                     printf("  type: HOME\n");
47                     break;
48                 
49                 case Person_PhoneType_MOBILE:
50                     printf("  type: MOBILE\n");
51                     break;
52             }
53         }
54         printf("}\n");
55     }
56     
57     return true;
58 }
59
60 int main()
61 {
62     /* Read the data into buffer */
63     uint8_t buffer[512];
64     size_t count = fread(buffer, 1, sizeof(buffer), stdin);
65     
66     if (!feof(stdin))
67     {
68         printf("Message does not fit in buffer\n");
69         return 1;
70     }
71     
72     /* Construct a pb_istream_t for reading from the buffer */
73     pb_istream_t stream = pb_istream_from_buffer(buffer, count);
74     
75     /* Decode and print out the stuff */
76     if (!print_person(&stream))
77     {
78         printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
79         return 1;
80     } else {
81         return 0;
82     }
83 }