2011-09-12 18:53:33 +00:00
|
|
|
/* A very simple decoding test case, using person.proto.
|
|
|
|
|
* Produces output compatible with protoc --decode.
|
2011-09-18 16:04:45 +00:00
|
|
|
* Reads the encoded data from stdin and prints the values
|
|
|
|
|
* to stdout as text.
|
|
|
|
|
*
|
|
|
|
|
* Run e.g. ./test_encode1 | ./test_decode1
|
2011-09-12 18:53:33 +00:00
|
|
|
*/
|
|
|
|
|
|
2011-07-25 20:42:48 +00:00
|
|
|
#include <stdio.h>
|
2011-07-31 12:55:09 +00:00
|
|
|
#include <pb_decode.h>
|
2011-08-24 12:13:24 +00:00
|
|
|
#include "person.pb.h"
|
2013-09-10 17:44:32 +03:00
|
|
|
#include "test_helpers.h"
|
2011-07-25 20:42:48 +00:00
|
|
|
|
2011-09-18 16:04:45 +00:00
|
|
|
/* This function is called once from main(), it handles
|
|
|
|
|
the decoding and printing. */
|
2011-07-25 20:42:48 +00:00
|
|
|
bool print_person(pb_istream_t *stream)
|
|
|
|
|
{
|
2011-07-27 19:22:11 +00:00
|
|
|
int i;
|
2014-08-04 18:40:40 +03:00
|
|
|
Person person = Person_init_zero;
|
2011-07-25 20:42:48 +00:00
|
|
|
|
2011-07-27 19:22:11 +00:00
|
|
|
if (!pb_decode(stream, Person_fields, &person))
|
2011-07-25 20:42:48 +00:00
|
|
|
return false;
|
|
|
|
|
|
2011-09-18 16:04:45 +00:00
|
|
|
/* Now the decoding is done, rest is just to print stuff out. */
|
|
|
|
|
|
2011-08-17 19:03:06 +00:00
|
|
|
printf("name: \"%s\"\n", person.name);
|
2011-11-30 15:01:59 +00:00
|
|
|
printf("id: %ld\n", (long)person.id);
|
2011-08-17 19:03:06 +00:00
|
|
|
|
|
|
|
|
if (person.has_email)
|
|
|
|
|
printf("email: \"%s\"\n", person.email);
|
2011-07-27 19:22:11 +00:00
|
|
|
|
2011-07-31 12:55:09 +00:00
|
|
|
for (i = 0; i < person.phone_count; i++)
|
2011-07-27 19:22:11 +00:00
|
|
|
{
|
|
|
|
|
Person_PhoneNumber *phone = &person.phone[i];
|
2011-08-17 19:03:06 +00:00
|
|
|
printf("phone {\n");
|
|
|
|
|
printf(" number: \"%s\"\n", phone->number);
|
|
|
|
|
|
2011-12-30 08:43:50 +00:00
|
|
|
if (phone->has_type)
|
2011-08-17 19:03:06 +00:00
|
|
|
{
|
2011-12-30 08:43:50 +00:00
|
|
|
switch (phone->type)
|
|
|
|
|
{
|
|
|
|
|
case Person_PhoneType_WORK:
|
|
|
|
|
printf(" type: WORK\n");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Person_PhoneType_HOME:
|
|
|
|
|
printf(" type: HOME\n");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Person_PhoneType_MOBILE:
|
|
|
|
|
printf(" type: MOBILE\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2011-08-17 19:03:06 +00:00
|
|
|
}
|
|
|
|
|
printf("}\n");
|
2011-07-27 19:22:11 +00:00
|
|
|
}
|
|
|
|
|
|
2011-07-25 20:42:48 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
2013-09-26 10:23:37 +03:00
|
|
|
uint8_t buffer[Person_size];
|
2013-09-10 17:44:32 +03:00
|
|
|
pb_istream_t stream;
|
|
|
|
|
size_t count;
|
|
|
|
|
|
|
|
|
|
/* Read the data into buffer */
|
|
|
|
|
SET_BINARY_MODE(stdin);
|
|
|
|
|
count = fread(buffer, 1, sizeof(buffer), stdin);
|
2011-12-30 08:57:27 +00:00
|
|
|
|
2012-09-18 16:48:43 +03:00
|
|
|
if (!feof(stdin))
|
|
|
|
|
{
|
|
|
|
|
printf("Message does not fit in buffer\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-30 08:57:27 +00:00
|
|
|
/* Construct a pb_istream_t for reading from the buffer */
|
2013-09-10 17:44:32 +03:00
|
|
|
stream = pb_istream_from_buffer(buffer, count);
|
2011-12-30 08:57:27 +00:00
|
|
|
|
|
|
|
|
/* Decode and print out the stuff */
|
2011-07-25 20:42:48 +00:00
|
|
|
if (!print_person(&stream))
|
2011-09-18 16:04:45 +00:00
|
|
|
{
|
2012-08-24 21:22:20 +03:00
|
|
|
printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
|
2011-09-18 16:04:45 +00:00
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2011-07-25 20:42:48 +00:00
|
|
|
}
|