a2c7f4281a48bdd3fa4b6f79d0fe706d0ea0b323
[apps/agl-service-can-low-level.git] / tests / test_decode1.c
1 #include <stdio.h>
2 #include <pb_decode.h>
3 #include "person.h"
4
5 /* This test has only one source file anyway.. */
6 #include "person.c"
7
8 bool print_person(pb_istream_t *stream)
9 {
10     int i;
11     Person person;
12     
13     if (!pb_decode(stream, Person_fields, &person))
14         return false;
15     
16     printf("Person: name '%s' id '%d' email '%s'\n", person.name, person.id, person.email);
17     
18     for (i = 0; i < person.phone_count; i++)
19     {
20         Person_PhoneNumber *phone = &person.phone[i];
21         printf("PhoneNumber: number '%s' type '%d'\n", phone->number, phone->type);
22     }
23     
24     return true;
25 }
26
27 bool callback(pb_istream_t *stream, uint8_t *buf, size_t count)
28 {
29     FILE *file = (FILE*)stream->state;
30     bool status;
31     
32     if (buf == NULL)
33     {
34         while (count-- && fgetc(file) != EOF);
35         return count == 0;
36     }
37     
38     status = (fread(buf, 1, count, file) == count);
39     
40     if (feof(file))
41         stream->bytes_left = 0;
42     
43     return status;
44 }
45
46 int main()
47 {
48     pb_istream_t stream = {&callback, stdin, SIZE_MAX};
49     if (!print_person(&stream))
50         printf("Parsing failed.\n");
51     
52     return 0;
53 }