MOS Source Code
Loading...
Searching...
No Matches
decode_any.c
Go to the documentation of this file.
1#include <assert.h>
2#include <pb_decode.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 uint8_t buffer[256];
13 pb_istream_t stream;
14 size_t count;
15 bool status;
16
17 /* Read the data into buffer */
19 count = fread(buffer, 1, sizeof(buffer), stdin);
20 stream = pb_istream_from_buffer(buffer, count);
21
22 /* Decode the base message */
23 if (!pb_decode(&stream, BaseMessage_fields, &msg))
24 {
25 printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
26 return 1;
27 }
28
29 assert(msg.start == 1234);
30 assert(msg.end == 5678);
31
32 /* Decode the Any message if we know the type */
33 if (strcmp(msg.details.type_url, "type.googleapis.com/google.protobuf.Duration") == 0)
34 {
35 google_protobuf_Duration duration = google_protobuf_Duration_init_zero;
36 stream = pb_istream_from_buffer(msg.details.value.bytes, msg.details.value.size);
37 status = pb_decode(&stream, google_protobuf_Duration_fields, &duration);
38 assert(status);
39 assert(duration.seconds == 99999);
40 assert(duration.nanos == 100);
41 return 0;
42 }
43 else
44 {
45 fprintf(stderr, "Unknown Any type\n");
46 return 2;
47 }
48}
49
int main()
Definition decode_any.c:9
MOSAPI s32 strcmp(const char *str1, const char *str2)
Definition mos_string.c:24
size_t fread(void *__restrict ptr, size_t size, size_t nmemb, FILE *__restrict stream)
#define stderr
Definition mos_stdio.h:32
#define stdin
Definition mos_stdio.h:30
#define PB_GET_ERROR(stream)
Definition pb.h:891
bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
Definition pb_decode.c:1182
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
Definition pb_decode.c:143
unsigned char uint8_t
Definition pb_syshdr.h:20
#define SET_BINARY_MODE(file)
static char buffer[2048]
Definition test_printf.c:7