Handle unsupported extension field types more gracefully.
[apps/agl-service-can-low-level.git] / tests / basic_stream / encode_stream.c
1 /* Same as test_encode1.c, except writes directly to stdout.
2  */
3
4 #include <stdio.h>
5 #include <pb_encode.h>
6 #include "person.pb.h"
7
8 /* This binds the pb_ostream_t into the stdout stream */
9 bool streamcallback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
10 {
11     FILE *file = (FILE*) stream->state;
12     return fwrite(buf, 1, count, file) == count;
13 }
14
15 int main()
16 {
17     /* Initialize the structure with constants */
18     Person person = {"Test Person 99", 99, true, "test@person.com",
19         3, {{"555-12345678", true, Person_PhoneType_MOBILE},
20             {"99-2342", false, 0},
21             {"1234-5678", true, Person_PhoneType_WORK},
22         }};
23     
24     /* Prepare the stream, output goes directly to stdout */
25     pb_ostream_t stream = {&streamcallback, stdout, SIZE_MAX, 0};
26     
27     /* Now encode it and check if we succeeded. */
28     if (pb_encode(&stream, Person_fields, &person))
29     {
30         return 0; /* Success */
31     }
32     else
33     {
34         fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
35         return 1; /* Failure */
36     }
37 }