summaryrefslogtreecommitdiffstats
path: root/tests/test_decode1.c
blob: cc4688dcfb5d8aa96d4a3650d823825ec446e0ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <pb_decode.h>
#include "person.h"

/* This test has only one source file anyway.. */
#include "person.c"

bool print_person(pb_istream_t *stream)
{
    int i;
    Person person;
    
    if (!pb_decode(stream, Person_fields, &person))
        return false;
    
    printf("Person: name '%s' id '%d' email '%s'\n", person.name, person.id, person.email);
    
    for (i = 0; i < person.phone_count; i++)
    {
        Person_PhoneNumber *phone = &person.phone[i];
        printf("PhoneNumber: number '%s' type '%d'\n", phone->number, phone->type);
    }
    
    return true;
}

int main()
{
    uint8_t buffer[512];
    size_t size = fread(buffer, 1, 512, stdin);
    
    pb_istream_t stream = pb_istream_from_buffer(buffer, size);
    if (!print_person(&stream))
        printf("Parsing failed.\n");
    
    return 0;
}