MOS Source Code
Loading...
Searching...
No Matches
encode_any.c
Go to the documentation of this file.
1#include <assert.h>
2#include <pb_encode.h>
3#include <string.h>
4#include <stdio.h>
5#include "test_helpers.h"
6#include "anytest.pb.h"
7#include "google/protobuf/duration.pb.h"
8
9int main()
10{
11 BaseMessage msg = BaseMessage_init_zero;
12 google_protobuf_Duration duration = google_protobuf_Duration_init_zero;
13 uint8_t buffer[256];
14 pb_ostream_t stream;
15 bool status;
16
17 msg.start = 1234;
18 msg.end = 5678;
19
20 /* Fill in the Any message header */
21 msg.has_details = true;
22 strncpy(msg.details.type_url, "type.googleapis.com/google.protobuf.Duration", sizeof(msg.details.type_url));
23
24 /* Encode a Duration message inside the Any message. */
25 duration.seconds = 99999;
26 duration.nanos = 100;
27 stream = pb_ostream_from_buffer(msg.details.value.bytes, sizeof(msg.details.value.bytes));
28 status = pb_encode(&stream, google_protobuf_Duration_fields, &duration);
29 assert(status);
30 msg.details.value.size = stream.bytes_written;
31
32 /* Then encode the outer message */
33 stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
34 if (pb_encode(&stream, BaseMessage_fields, &msg))
35 {
36 /* Write the result data to stdout */
38 fwrite(buffer, 1, stream.bytes_written, stdout);
39 return 0;
40 }
41 else
42 {
43 fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
44 return 1;
45 }
46}
int main()
Definition encode_any.c:9
MOSAPI char * strncpy(char *__restrict dest, const char *__restrict src, size_t n)
Definition mos_string.c:202
#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