MOS Source Code
Loading...
Searching...
No Matches
encode_buffer.c
Go to the documentation of this file.
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
11int 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 */
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}
int main()
#define stdout
Definition mos_stdio.h:31
#define stderr
Definition mos_stdio.h:32
size_t fwrite(const void *__restrict ptr, size_t size, size_t nmemb, FILE *__restrict stream)
#define PB_GET_ERROR(stream)
Definition pb.h:891
pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
Definition pb_encode.c:63
bool pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
Definition pb_encode.c:512
unsigned char uint8_t
Definition pb_syshdr.h:20
#define SET_BINARY_MODE(file)
static char buffer[2048]
Definition test_printf.c:7