1 /* A very simple encoding test case using person.proto.
2 * Just puts constant data in the fields and encodes into
3 * buffer, which is then written to stdout.
4 */
5
6 #include <stdio.h>
7 #include <pb_encode.h>
8 #include "person.pb.h"
9 #include "test_helpers.h"
10
main()11 int main()
12 {
13 uint8_t buffer[Person_size];
14 pb_ostream_t stream;
15
16 /* Initialize the structure with constants */
17 Person person = {"Test Person 99", 99, true, "test@person.com",
18 3, {{"555-12345678", true, Person_PhoneType_MOBILE},
19 {"99-2342", false, 0},
20 {"1234-5678", true, Person_PhoneType_WORK},
21 }};
22
23 stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
24
25 /* Now encode it and check if we succeeded. */
26 if (pb_encode(&stream, Person_fields, &person))
27 {
28 /* Write the result data to stdout */
29 SET_BINARY_MODE(stdout);
30 fwrite(buffer, 1, stream.bytes_written, stdout);
31 return 0; /* Success */
32 }
33 else
34 {
35 fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
36 return 1; /* Failure */
37 }
38 }
39