MOS Source Code
Loading...
Searching...
No Matches
encode_map.c
Go to the documentation of this file.
1/* Encode a message using map field */
2
3#include <stdio.h>
4#include <stdlib.h>
5#include <pb_encode.h>
6#include "map.pb.h"
7#include "test_helpers.h"
8
9int main(int argc, char **argv)
10{
11 uint8_t buffer[MyMessage_size];
12 MyMessage msg = MyMessage_init_zero;
13 pb_ostream_t stream;
14
15 /* Fill in the map entries */
16 msg.numbers_count = 3;
17 strncpy(msg.numbers[0].key, "one", sizeof(msg.numbers[0].key));
18 strncpy(msg.numbers[1].key, "two", sizeof(msg.numbers[1].key));
19 strncpy(msg.numbers[2].key, "seven", sizeof(msg.numbers[2].key));
20 msg.numbers[0].value = 1;
21 msg.numbers[1].value = 2;
22 msg.numbers[2].value = 7;
23
24 stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
25
26 if (pb_encode(&stream, MyMessage_fields, &msg))
27 {
29 fwrite(buffer, 1, stream.bytes_written, stdout);
30 return 0;
31 }
32 else
33 {
34 fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
35 return 1;
36 }
37}
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)
const char ** argv
Definition kmain.c:44
size_t argc
Definition kmain.c:43
#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
int main()
Definition simple.cpp:6
#define SET_BINARY_MODE(file)
static char buffer[2048]
Definition test_printf.c:7