LCOV - code coverage report
Current view: top level - basic_stream - decode_stream.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 33 39 84.6 %
Date: 2023-02-14 20:10:26 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* Same as test_decode1 but reads from stdin directly.
       2             :  */
       3             : 
       4             : #include <stdio.h>
       5             : #include <pb_decode.h>
       6             : #include "person.pb.h"
       7             : #include "test_helpers.h"
       8             : 
       9             : /* This function is called once from main(), it handles
      10             :    the decoding and printing.
      11             :    Ugly copy-paste from test_decode1.c. */
      12           2 : bool print_person(pb_istream_t *stream)
      13             : {
      14             :     int i;
      15           2 :     Person person = Person_init_zero;
      16             :     
      17           2 :     if (!pb_decode(stream, Person_fields, &person))
      18           0 :         return false;
      19             :     
      20             :     /* Now the decoding is done, rest is just to print stuff out. */
      21             : 
      22           2 :     printf("name: \"%s\"\n", person.name);
      23           2 :     printf("id: %ld\n", (long)person.id);
      24             :     
      25           2 :     if (person.has_email)
      26           2 :         printf("email: \"%s\"\n", person.email);
      27             :     
      28           8 :     for (i = 0; i < person.phone_count; i++)
      29             :     {
      30           6 :         Person_PhoneNumber *phone = &person.phone[i];
      31           6 :         printf("phone {\n");
      32           6 :         printf("  number: \"%s\"\n", phone->number);
      33             :         
      34           6 :         if (phone->has_type)
      35             :         {
      36           4 :             switch (phone->type)
      37             :             {
      38           2 :                 case Person_PhoneType_WORK:
      39           2 :                     printf("  type: WORK\n");
      40           2 :                     break;
      41             :                 
      42           0 :                 case Person_PhoneType_HOME:
      43           0 :                     printf("  type: HOME\n");
      44           0 :                     break;
      45             :                 
      46           2 :                 case Person_PhoneType_MOBILE:
      47           2 :                     printf("  type: MOBILE\n");
      48           2 :                     break;
      49             :             }
      50             :         }
      51           6 :         printf("}\n");
      52             :     }
      53             :     
      54           2 :     return true;
      55             : }
      56             : 
      57             : /* This binds the pb_istream_t to stdin */
      58          67 : bool callback(pb_istream_t *stream, uint8_t *buf, size_t count)
      59             : {
      60          67 :     FILE *file = (FILE*)stream->state;
      61          67 :     size_t len = fread(buf, 1, count, file);
      62             :     
      63          67 :     if (len == count)
      64             :     {
      65          65 :         return true;
      66             :     }
      67             :     else
      68             :     {
      69           2 :         stream->bytes_left = 0;
      70           2 :         return false;
      71             :     }
      72             : }
      73             : 
      74           2 : int main()
      75             : {
      76           2 :     pb_istream_t stream = {&callback, NULL, SIZE_MAX};
      77           2 :     stream.state = stdin;
      78             :     SET_BINARY_MODE(stdin);
      79             : 
      80           2 :     if (!print_person(&stream))
      81             :     {
      82           0 :         printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
      83           0 :         return 1;
      84             :     } else {
      85           2 :         return 0;
      86             :     }
      87             : }

Generated by: LCOV version 1.14