LCOV - code coverage report
Current view: top level - afldemo - toy.c (source / functions) Hit Total Coverage
Test: toy.info Lines: 24 29 82.8 %
Date: 2019-09-16 18:20:30 Functions: 4 4 100.0 %

          Line data    Source code
       1             : #include <stdio.h>
       2             : #include <stdlib.h>
       3             : #include <stdint.h>
       4             : 
       5             : #pragma pack(1)
       6             : #define MAGIC 0x4c415641
       7             : 
       8             : enum {
       9             :     TYPEA = 1,
      10             :     TYPEB = 2
      11             : };  
      12             : 
      13             : typedef struct {
      14             :     uint32_t magic;     // Magic value
      15             :     uint32_t reserved;  // Reserved for future use
      16             :     uint16_t num_recs;  // How many entries?
      17             :     uint16_t flags;     // None used yet
      18             :     uint32_t timestamp; // Unix Time
      19             : } file_header;
      20             : 
      21             : typedef struct {
      22             :     char bar[16];
      23             :     uint32_t type;   
      24             :     union {
      25             :         float fdata; 
      26             :         uint32_t intdata;
      27             :     } data;
      28             : } file_entry;
      29             : 
      30           1 : void parse_header(FILE *f, file_header *hdr) {
      31           1 :     if (1 != fread(hdr, sizeof(file_header), 1, f))
      32           0 :         exit(1);
      33           1 :     if (hdr->magic != MAGIC)
      34           0 :         exit(1);
      35           1 : }
      36             : 
      37           3 : file_entry * parse_record(FILE *f) {
      38           3 :     file_entry *ret = (file_entry *) malloc(sizeof(file_entry));
      39           3 :     if (1 != fread(ret, sizeof(file_entry), 1, f))
      40           0 :         exit(1);
      41           3 :     return ret;
      42             : }
      43             : 
      44           3 : void consume_record(file_entry *ent) {
      45           3 :     printf("Entry: bar = %s, ", ent->bar);
      46           3 :     if (ent->type == TYPEA) {
      47           2 :         printf("fdata = %f\n", ent->data.fdata);
      48             :     }
      49           1 :     else if (ent->type == TYPEB) {
      50           1 :         printf("intdata = %u\n", ent->data.intdata);
      51             :     }
      52             :     else {
      53           0 :         printf("Unknown type %x\n", ent->type);
      54           0 :         exit(1);
      55             :     }
      56           3 :     free(ent);
      57           3 : }
      58             : 
      59           1 : int main(int argc, char **argv) {
      60           1 :     FILE *f = fopen(argv[1], "rb");
      61             :     file_header head;
      62             : 
      63           1 :     parse_header(f, &head);
      64           1 :     printf("File timestamp: %u\n", head.timestamp);
      65             : 
      66             :     unsigned i;
      67           4 :     for (i = 0; i < head.num_recs; i++) {
      68           3 :         file_entry *ent = parse_record(f);
      69           3 :         consume_record(ent);
      70             :     }
      71           1 :     return 0;
      72             : }

Generated by: LCOV version 1.12