1 /* This program reads a message from stdin, detects its type and decodes it.
9 #include "unionproto.pb.h"
11 /* This function reads manually the first tag from the stream and finds the
12 * corresponding message type. It doesn't yet decode the actual message.
14 * Returns a pointer to the MsgType_fields array, as an identifier for the
15 * message type. Returns null if the tag is of unknown type or an error occurs.
17 const pb_field_t* decode_unionmessage_type(pb_istream_t *stream)
19 pb_wire_type_t wire_type;
23 while (pb_decode_tag(stream, &wire_type, &tag, &eof))
25 if (wire_type == PB_WT_STRING)
27 const pb_field_t *field;
28 for (field = UnionMessage_fields; field->tag != 0; field++)
30 if (field->tag == tag && (field->type & PB_LTYPE_SUBMESSAGE))
32 /* Found our field. */
38 /* Wasn't our field.. */
39 pb_skip_field(stream, wire_type);
45 bool decode_unionmessage_contents(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
47 pb_istream_t substream;
49 if (!pb_make_string_substream(stream, &substream))
52 status = pb_decode(&substream, fields, dest_struct);
53 pb_close_string_substream(stream, &substream);
59 /* Read the data into buffer */
61 size_t count = fread(buffer, 1, sizeof(buffer), stdin);
62 pb_istream_t stream = pb_istream_from_buffer(buffer, count);
64 const pb_field_t *type = decode_unionmessage_type(&stream);
67 if (type == MsgType1_fields)
70 status = decode_unionmessage_contents(&stream, MsgType1_fields, &msg);
71 printf("Got MsgType1: %d\n", msg.value);
73 else if (type == MsgType2_fields)
76 status = decode_unionmessage_contents(&stream, MsgType2_fields, &msg);
77 printf("Got MsgType2: %s\n", msg.value ? "true" : "false");
79 else if (type == MsgType3_fields)
82 status = decode_unionmessage_contents(&stream, MsgType3_fields, &msg);
83 printf("Got MsgType3: %d %d\n", msg.value1, msg.value2);
88 printf("Decode failed: %s\n", PB_GET_ERROR(&stream));