LCOV - code coverage report
Current view: top level - basic_buffer - decode_buffer.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 27 35 77.1 %
Date: 2023-02-14 20:10:26 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* A very simple decoding test case, using person.proto.
       2             :  * Produces output compatible with protoc --decode.
       3             :  * Reads the encoded data from stdin and prints the values
       4             :  * to stdout as text.
       5             :  *
       6             :  * Run e.g. ./test_encode1 | ./test_decode1
       7             :  */
       8             : 
       9             : #include <stdio.h>
      10             : #include <pb_decode.h>
      11             : #include "person.pb.h"
      12             : #include "test_helpers.h"
      13             : 
      14             : /* This function is called once from main(), it handles
      15             :    the decoding and printing. */
      16           2 : bool print_person(pb_istream_t *stream)
      17             : {
      18             :     int i;
      19           2 :     Person person = Person_init_zero;
      20             :     
      21           2 :     if (!pb_decode(stream, Person_fields, &person))
      22           0 :         return false;
      23             :     
      24             :     /* Now the decoding is done, rest is just to print stuff out. */
      25             : 
      26           2 :     printf("name: \"%s\"\n", person.name);
      27           2 :     printf("id: %ld\n", (long)person.id);
      28             :     
      29           2 :     if (person.has_email)
      30           2 :         printf("email: \"%s\"\n", person.email);
      31             :     
      32           8 :     for (i = 0; i < person.phone_count; i++)
      33             :     {
      34           6 :         Person_PhoneNumber *phone = &person.phone[i];
      35           6 :         printf("phone {\n");
      36           6 :         printf("  number: \"%s\"\n", phone->number);
      37             :         
      38           6 :         if (phone->has_type)
      39             :         {
      40           4 :             switch (phone->type)
      41             :             {
      42           2 :                 case Person_PhoneType_WORK:
      43           2 :                     printf("  type: WORK\n");
      44           2 :                     break;
      45             :                 
      46           0 :                 case Person_PhoneType_HOME:
      47           0 :                     printf("  type: HOME\n");
      48           0 :                     break;
      49             :                 
      50           2 :                 case Person_PhoneType_MOBILE:
      51           2 :                     printf("  type: MOBILE\n");
      52           2 :                     break;
      53             :             }
      54             :         }
      55           6 :         printf("}\n");
      56             :     }
      57             :     
      58           2 :     return true;
      59             : }
      60             : 
      61           2 : int main()
      62             : {
      63             :     uint8_t buffer[Person_size];
      64             :     pb_istream_t stream;
      65             :     size_t count;
      66             :     
      67             :     /* Read the data into buffer */
      68             :     SET_BINARY_MODE(stdin);
      69           2 :     count = fread(buffer, 1, sizeof(buffer), stdin);
      70             :     
      71           2 :     if (!feof(stdin))
      72             :     {
      73           0 :         printf("Message does not fit in buffer\n");
      74           0 :         return 1;
      75             :     }
      76             :     
      77             :     /* Construct a pb_istream_t for reading from the buffer */
      78           2 :     stream = pb_istream_from_buffer(buffer, count);
      79             :     
      80             :     /* Decode and print out the stuff */
      81           2 :     if (!print_person(&stream))
      82             :     {
      83           0 :         printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
      84           0 :         return 1;
      85             :     } else {
      86           2 :         return 0;
      87             :     }
      88             : }

Generated by: LCOV version 1.14