MOS Source Code
Loading...
Searching...
No Matches
encode_stream.c
Go to the documentation of this file.
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#include "test_helpers.h"
8
9/* This binds the pb_ostream_t into the stdout stream */
10bool streamcallback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
11{
12 FILE *file = (FILE*) stream->state;
13 return fwrite(buf, 1, count, file) == count;
14}
15
16int main()
17{
18 /* Initialize the structure with constants */
19 Person person = {"Test Person 99", 99, true, "test@person.com",
20 3, {{"555-12345678", true, Person_PhoneType_MOBILE},
21 {"99-2342", false, 0},
22 {"1234-5678", true, Person_PhoneType_WORK},
23 }};
24
25 /* Prepare the stream, output goes directly to stdout */
26 pb_ostream_t stream = {&streamcallback, NULL, SIZE_MAX, 0};
27 stream.state = stdout;
29
30 /* Now encode it and check if we succeeded. */
31 if (pb_encode(&stream, Person_fields, &person))
32 {
33 return 0; /* Success */
34 }
35 else
36 {
37 fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
38 return 1; /* Failure */
39 }
40}
bool streamcallback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
int main()
#define stdout
Definition mos_stdio.h:31
struct _FILE FILE
Definition mos_stdio.h:24
#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
bool pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
Definition pb_encode.c:512
#define NULL
Definition pb_syshdr.h:46
unsigned char uint8_t
Definition pb_syshdr.h:20
#define SET_BINARY_MODE(file)