Fix some compiler warnings in strict C89 mode
[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: %d\n", 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         switch (phone->type)
38         {
39             case Person_PhoneType_WORK:
40                 printf("  type: WORK\n");
41                 break;
42             
43             case Person_PhoneType_HOME:
44                 printf("  type: HOME\n");
45                 break;
46             
47             case Person_PhoneType_MOBILE:
48                 printf("  type: MOBILE\n");
49                 break;
50         }
51         printf("}\n");
52     }
53     
54     return true;
55 }
56
57 /* This binds the pb_istream_t to stdin */
58 bool callback(pb_istream_t *stream, uint8_t *buf, size_t count)
59 {
60     FILE *file = (FILE*)stream->state;
61     bool status;
62     
63     if (buf == NULL)
64     {
65        /* Skipping data */
66         while (count-- && fgetc(file) != EOF);
67         return count == 0;
68     }
69     
70     status = (fread(buf, 1, count, file) == count);
71     
72     if (feof(file))
73         stream->bytes_left = 0;
74     
75     return status;
76 }
77
78 int main()
79 {
80     /* Maximum size is specified to prevent infinite length messages from
81      * hanging this in the fuzz test.
82      */
83     pb_istream_t stream = {&callback, stdin, 10000};
84     if (!print_person(&stream))
85     {
86         printf("Parsing failed.\n");
87         return 1;
88     } else {
89         return 0;
90     }
91 }