Files
nanopb/tests/test_encode1.c
Michael Poole 43b8e20744 Create a message descriptor type.
This replaces the sentinel at the end of the pb_field_t
array for each message type.

git-svn-id: https://svn.kapsi.fi/jpa/nanopb-dev@1007 e3a754e5-d11d-0410-8d38-ebb782a927b9
2012-01-05 22:01:17 +02:00

32 lines
935 B
C

/* A very simple encoding test case using person.proto.
* Just puts constant data in the fields and writes the
* data 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 = {"Test Person 99", 99, true, "test@person.com",
1, {{"555-12345678", true, Person_PhoneType_MOBILE}}};
/* Prepare the stream, output goes directly to stdout */
pb_ostream_t stream = {&streamcallback, stdout, SIZE_MAX, 0};
/* Now encode it and check if we succeeded. */
if (pb_encode(&stream, Person_msg, &person))
return 0; /* Success */
else
return 1; /* Failure */
}