1 /* Decoding testcase for callback fields.
2 * Run e.g. ./test_encode_callbacks | ./test_decode_callbacks
7 #include "callbacks.pb.h"
9 bool print_string(pb_istream_t *stream, const pb_field_t *field, void *arg)
13 /* We could read block-by-block to avoid the large buffer... */
14 if (stream->bytes_left > sizeof(buffer))
17 if (!pb_read(stream, buffer, stream->bytes_left))
20 /* Print the string, in format comparable with protoc --decode. */
21 printf("%s: \"%s\"\n", (char*)arg, buffer);
28 size_t length = fread(buffer, 1, 1024, stdin);
29 pb_istream_t stream = pb_istream_from_buffer(buffer, length);
31 /* Note: empty initializer list initializes the struct with all-0.
32 * This is recommended so that unused callbacks are set to NULL instead
33 * of crashing at runtime.
35 TestMessage testmessage = {};
37 testmessage.stringvalue.funcs.decode = &print_string;
38 testmessage.stringvalue.arg = "stringvalue";
40 if (!pb_decode(&stream, TestMessage_fields, &testmessage))