LCOV - code coverage report
Current view: top level - t - c-parse.c (source / functions) Hit Total Coverage
Test: PB-C-TEXT Code Coverage Lines: 24 41 58.5 %
Date: 2014-09-08 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #include <sys/types.h>
       2             : #include <sys/stat.h>
       3             : #include <unistd.h>
       4             : #include <stdio.h>
       5             : #include <string.h>
       6             : #include <stdlib.h>
       7             : #include <protobuf-c/protobuf-c.h>
       8             : #include "protobuf-c-text/protobuf-c-text.h"
       9             : #include "addressbook.pb-c.h"
      10             : #include "broken-alloc.h"
      11             : 
      12             : #define CHUNK 1024
      13             : 
      14             : int
      15         167 : main(int argc, char *argv[])
      16             : {
      17             :   ProtobufCTextError tf_res;
      18             :   Tutorial__AddressBook *ab;
      19             :   size_t len;
      20             :   uint8_t *buf;
      21             :   FILE *out;
      22             :   Tutorial__Short *shortmsg;
      23             : 
      24         167 :   ab = (Tutorial__AddressBook *)protobuf_c_text_from_file(
      25             :       &tutorial__address_book__descriptor,
      26             :       stdin, &tf_res, &broken_allocator);
      27         167 :   if (tf_res.error_txt) {
      28         161 :     printf("ERROR on import:\n%s", tf_res.error_txt);
      29         161 :     free(tf_res.error_txt);
      30         161 :     exit(1);
      31             :   }
      32           6 :   if (!tf_res.complete) {
      33           0 :     printf("ERROR on import: Message not complete.\n");
      34           0 :     if (ab) {
      35           0 :       tutorial__address_book__free_unpacked(ab, &broken_allocator);
      36             :     }
      37           0 :     exit(1);
      38             :   }
      39           6 :   if (!ab) {
      40           4 :     printf("ERROR malloc failures.\n");
      41           4 :     exit(1);
      42             :   }
      43             : 
      44           2 :   len = tutorial__address_book__get_packed_size(ab);
      45           2 :   buf = malloc(len);
      46           2 :   tutorial__address_book__pack(ab, buf);
      47           2 :   if (argv[1]) {
      48           2 :     out = fopen(argv[1], "w");
      49           2 :     if (len != fwrite(buf, 1, len, out)) {
      50           0 :       printf("ERROR: Protobuf output file truncated.");
      51           0 :       exit(1);
      52             :     }
      53           2 :     fclose(out);
      54             :   } else {
      55           0 :     printf("ERROR: Name for protobuf output file not provided.");
      56           0 :     exit(1);
      57             :   }
      58             : 
      59           2 :   tutorial__address_book__free_unpacked(ab, &broken_allocator);
      60             : 
      61           2 :   shortmsg = (Tutorial__Short *)protobuf_c_text_from_string(
      62             :       &tutorial__short__descriptor,
      63             :       "id: 42\ntruer: 7\nfalser: \"\t\"\n", &tf_res, NULL);
      64           2 :   if (tf_res.error_txt) {
      65           0 :     printf("ERROR on import:\n%s", tf_res.error_txt);
      66           0 :     free(tf_res.error_txt);
      67           0 :     exit(1);
      68             :   }
      69           2 :   if (!tf_res.complete) {
      70           0 :     printf("ERROR on import: Message not complete.\n");
      71           0 :     if (shortmsg) {
      72           0 :       tutorial__short__free_unpacked(shortmsg, &broken_allocator);
      73             :     }
      74           0 :     exit(1);
      75             :   }
      76           2 :   if (!shortmsg) {
      77           0 :     printf("ERROR malloc failures.\n");
      78           0 :     exit(1);
      79             :   }
      80           2 :   tutorial__short__free_unpacked(shortmsg, NULL);
      81             : 
      82           2 :   exit(0);
      83             : }

Generated by: LCOV version 1.10