MOS Source Code
Loading...
Searching...
No Matches
encode_callbacks.c
Go to the documentation of this file.
1/* Encoding testcase for callback fields */
2
3#include <stdio.h>
4#include <string.h>
5#include <pb_encode.h>
6#include "callbacks.pb.h"
7#include "test_helpers.h"
8
9bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
10{
11 char *str = "Hello world!";
12
13 if (!pb_encode_tag_for_field(stream, field))
14 return false;
15
16 return pb_encode_string(stream, (uint8_t*)str, strlen(str));
17}
18
19bool encode_int32(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
20{
21 if (!pb_encode_tag_for_field(stream, field))
22 return false;
23
24 return pb_encode_varint(stream, 42);
25}
26
27bool encode_fixed32(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
28{
29 uint32_t value = 42;
30
31 if (!pb_encode_tag_for_field(stream, field))
32 return false;
33
34 return pb_encode_fixed32(stream, &value);
35}
36
37bool encode_fixed64(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
38{
39 uint64_t value = 42;
40
41 if (!pb_encode_tag_for_field(stream, field))
42 return false;
43
44 return pb_encode_fixed64(stream, &value);
45}
46
47bool encode_repeatedstring(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
48{
49 char *str[4] = {"Hello world!", "", "Test", "Test2"};
50 int i;
51
52 for (i = 0; i < 4; i++)
53 {
54 if (!pb_encode_tag_for_field(stream, field))
55 return false;
56
57 if (!pb_encode_string(stream, (uint8_t*)str[i], strlen(str[i])))
58 return false;
59 }
60 return true;
61}
62
63int main()
64{
65 uint8_t buffer[1024];
66 pb_ostream_t stream;
67 TestMessage testmessage = {{{NULL}}};
68
69 stream = pb_ostream_from_buffer(buffer, 1024);
70
71 testmessage.stringvalue.funcs.encode = &encode_string;
72 testmessage.int32value.funcs.encode = &encode_int32;
73 testmessage.fixed32value.funcs.encode = &encode_fixed32;
74 testmessage.fixed64value.funcs.encode = &encode_fixed64;
75
76 testmessage.has_submsg = true;
77 testmessage.submsg.stringvalue.funcs.encode = &encode_string;
78 testmessage.submsg.int32value.funcs.encode = &encode_int32;
79 testmessage.submsg.fixed32value.funcs.encode = &encode_fixed32;
80 testmessage.submsg.fixed64value.funcs.encode = &encode_fixed64;
81
82 testmessage.repeatedstring.funcs.encode = &encode_repeatedstring;
83
84 if (!pb_encode(&stream, TestMessage_fields, &testmessage))
85 return 1;
86
88 if (fwrite(buffer, stream.bytes_written, 1, stdout) != 1)
89 return 2;
90
91 return 0;
92}
bool encode_int32(pb_ostream_t *stream, const pb_field_t *field, void *const *arg)
bool encode_fixed64(pb_ostream_t *stream, const pb_field_t *field, void *const *arg)
bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void *const *arg)
bool encode_fixed32(pb_ostream_t *stream, const pb_field_t *field, void *const *arg)
bool encode_repeatedstring(pb_ostream_t *stream, const pb_field_t *field, void *const *arg)
int main()
#define stdout
Definition mos_stdio.h:31
size_t fwrite(const void *__restrict ptr, size_t size, size_t nmemb, FILE *__restrict stream)
pb_field_iter_t pb_field_t
Definition pb.h:359
bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_iter_t *field)
Definition pb_encode.c:681
bool pb_encode_varint(pb_ostream_t *stream, uint64_t value)
Definition pb_encode.c:607
pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
Definition pb_encode.c:63
bool pb_encode_fixed64(pb_ostream_t *stream, const void *value)
Definition pb_encode.c:654
bool pb_encode_fixed32(pb_ostream_t *stream, const void *value)
Definition pb_encode.c:637
bool pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
Definition pb_encode.c:512
bool pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size)
Definition pb_encode.c:716
#define NULL
Definition pb_syshdr.h:46
unsigned int uint32_t
Definition pb_syshdr.h:24
static size_t strlen(const char *s)
Definition pb_syshdr.h:80
unsigned long long uint64_t
Definition pb_syshdr.h:26
unsigned char uint8_t
Definition pb_syshdr.h:20
#define SET_BINARY_MODE(file)
static char buffer[2048]
Definition test_printf.c:7