Line data Source code
1 : #include <assert.h> 2 : #include <pb_encode.h> 3 : #include <string.h> 4 : #include <stdio.h> 5 : #include "test_helpers.h" 6 : #include "anytest.pb.h" 7 : #include "google/protobuf/duration.pb.h" 8 : 9 1 : int main() 10 : { 11 1 : BaseMessage msg = BaseMessage_init_zero; 12 1 : google_protobuf_Duration duration = google_protobuf_Duration_init_zero; 13 : uint8_t buffer[256]; 14 : pb_ostream_t stream; 15 : bool status; 16 : 17 1 : msg.start = 1234; 18 1 : msg.end = 5678; 19 : 20 : /* Fill in the Any message header */ 21 1 : msg.has_details = true; 22 1 : strncpy(msg.details.type_url, "type.googleapis.com/google.protobuf.Duration", sizeof(msg.details.type_url)); 23 : 24 : /* Encode a Duration message inside the Any message. */ 25 1 : duration.seconds = 99999; 26 1 : duration.nanos = 100; 27 1 : stream = pb_ostream_from_buffer(msg.details.value.bytes, sizeof(msg.details.value.bytes)); 28 1 : status = pb_encode(&stream, google_protobuf_Duration_fields, &duration); 29 1 : assert(status); 30 1 : msg.details.value.size = stream.bytes_written; 31 : 32 : /* Then encode the outer message */ 33 1 : stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); 34 1 : if (pb_encode(&stream, BaseMessage_fields, &msg)) 35 : { 36 : /* Write the result data to stdout */ 37 : SET_BINARY_MODE(stdout); 38 1 : fwrite(buffer, 1, stream.bytes_written, stdout); 39 1 : return 0; 40 : } 41 : else 42 : { 43 0 : fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream)); 44 0 : return 1; 45 : } 46 : }