summaryrefslogtreecommitdiffstats
path: root/tests/test_decode1.c
blob: a507b0dc4f35d2ac5269730ea1355ebf359d3701 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include "pb_decode.h"

/* Structures for "Person" message */

typedef enum {
    Person_PhoneType_MOBILE = 0,
    Person_PhoneType_HOME = 1,
    Person_PhoneType_WORK = 2
} Person_PhoneType;

typedef struct {
    char number[40];
    bool has_type;
    Person_PhoneType type;
} Person_PhoneNumber;

typedef struct {
    char name[40];
    int32_t id;
    bool has_email;
    char email[40];
    size_t phone_size;
    Person_PhoneNumber phone[5];
} Person;

/* Field descriptions */
#define membersize(st, m) (sizeof ((st*)0)->m)

const Person_PhoneType Person_PhoneType_type_default = Person_PhoneType_HOME;

const pb_field_t Person_PhoneNumber_fields[] = {
    {1, PB_HTYPE_REQUIRED | PB_LTYPE_STRING,
        offsetof(Person_PhoneNumber, number), 0,
        membersize(Person_PhoneNumber, number), 0, 0},
        
    {2, PB_HTYPE_OPTIONAL | PB_LTYPE_VARINT,
        offsetof(Person_PhoneNumber, type) - offsetof(Person_PhoneNumber, number),
        (int)offsetof(Person_PhoneNumber, has_type) - (int)offsetof(Person_PhoneNumber, type),
        membersize(Person_PhoneNumber, type), 0,
        &Person_PhoneType_type_default},
    
    PB_LAST_FIELD
};

const pb_field_t Person_fields[] = {
    {1, PB_HTYPE_REQUIRED | PB_LTYPE_STRING,
        offsetof(Person, name), 0,
        membersize(Person, name), 0, 0},
    
    {2, PB_HTYPE_REQUIRED | PB_LTYPE_VARINT,
        offsetof(Person, id) - offsetof(Person, name), 0,
        membersize(Person, id), 0, 0},
    
    {3, PB_HTYPE_OPTIONAL | PB_LTYPE_STRING,
        offsetof(Person, email) - offsetof(Person, id),
        (int)offsetof(Person, has_email) - (int)offsetof(Person, email),
        membersize(Person, email), 0, 0},
    
    {4, PB_HTYPE_ARRAY | PB_LTYPE_SUBMESSAGE,
        offsetof(Person, phone) - offsetof(Person, email),
        (int)offsetof(Person, phone_size) - (int)offsetof(Person, phone),
        membersize(Person, phone[0]),
        membersize(Person, phone) / membersize(Person, phone[0]),
        Person_PhoneNumber_fields},
    
    PB_LAST_FIELD
};

/* And now, the actual test program */

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_size; 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;
}