Merge branch 'master' into dynamic_alloc_dev
Conflicts: tests/Makefile tests/test_encode1.c
This commit is contained in:
@@ -1,33 +1,36 @@
|
||||
/* A very simple encoding test case using person.proto.
|
||||
* Just puts constant data in the fields and writes the
|
||||
* data to stdout.
|
||||
* Just puts constant data in the fields and encodes into
|
||||
* buffer, which is then written to stdout.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <pb_encode.h>
|
||||
#include "person.pb.h"
|
||||
|
||||
/* This binds the pb_ostream_t into the stdout stream */
|
||||
bool streamcallback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
|
||||
{
|
||||
FILE *file = (FILE*) stream->state;
|
||||
return fwrite(buf, 1, count, file) == count;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
/* Initialize the structure with constants */
|
||||
Person person = {{0}, "Test Person 99", 99, "test@person.com",
|
||||
1, {{{0}, "555-12345678", Person_PhoneType_MOBILE}}};
|
||||
Person_set(person, email);
|
||||
Person_PhoneNumber_set(person.phone[0], type);
|
||||
Person person = {{1 << Person_email_index},
|
||||
"Test Person 99", 99, "test@person.com",
|
||||
3, {{{1 << Person_PhoneNumber_type_index},
|
||||
"555-12345678", Person_PhoneType_MOBILE},
|
||||
{{1 << Person_PhoneNumber_type_index},
|
||||
"99-2342", 0},
|
||||
{{1 << Person_PhoneNumber_type_index},
|
||||
"1234-5678", Person_PhoneType_WORK},
|
||||
}};
|
||||
|
||||
/* Prepare the stream, output goes directly to stdout */
|
||||
pb_ostream_t stream = {&streamcallback, stdout, SIZE_MAX, 0};
|
||||
uint8_t buffer[512];
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
||||
|
||||
/* Now encode it and check if we succeeded. */
|
||||
if (pb_encode(&stream, Person_msg, &person))
|
||||
{
|
||||
fwrite(buffer, 1, stream.bytes_written, stdout);
|
||||
return 0; /* Success */
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1; /* Failure */
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user