1 /* Encode a message using map field */ 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <pb_encode.h> 6 #include "map.pb.h" 7 #include "test_helpers.h" 8 9 int main(int argc, char **argv) 10 { 11 uint8_t buffer[MyMessage_size]; 12 MyMessage msg = MyMessage_init_zero; 13 pb_ostream_t stream; 14 15 /* Fill in the map entries */ 16 msg.numbers_count = 3; 17 strncpy(msg.numbers[0].key, "one", sizeof(msg.numbers[0].key)); 18 strncpy(msg.numbers[1].key, "two", sizeof(msg.numbers[1].key)); 19 strncpy(msg.numbers[2].key, "seven", sizeof(msg.numbers[2].key)); 20 msg.numbers[0].value = 1; 21 msg.numbers[1].value = 2; 22 msg.numbers[2].value = 7; 23 24 stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); 25 26 if (pb_encode(&stream, MyMessage_fields, &msg)) 27 { 28 SET_BINARY_MODE(stdout); 29 fwrite(buffer, 1, stream.bytes_written, stdout); 30 return 0; 31 } 32 else 33 { 34 fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream)); 35 return 1; 36 } 37 } 38