Last active
July 10, 2016 17:20
-
-
Save moyix/93cd687fde9fb965cfb7d508118d27c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#pragma pack(1) | |
#define MAGIC 0x4c415641 | |
enum { | |
TYPEA = 1, | |
TYPEB = 2 | |
}; | |
typedef struct { | |
uint32_t magic; // Magic value | |
uint32_t reserved; // Reserved for future use | |
uint16_t num_recs; // How many entries? | |
uint16_t flags; // None used yet | |
uint32_t timestamp; // Unix Time | |
} file_header; | |
typedef struct { | |
char bar[16]; | |
uint32_t type; | |
union { | |
float fdata; | |
uint32_t intdata; | |
} data; | |
} file_entry; | |
void parse_header(FILE *f, file_header *hdr) { | |
if (1 != fread(hdr, sizeof(file_header), 1, f)) | |
exit(1); | |
if (hdr->magic != MAGIC) | |
exit(1); | |
} | |
file_entry * parse_record(FILE *f) { | |
file_entry *ret = (file_entry *) malloc(sizeof(file_entry)); | |
if (1 != fread(ret, sizeof(file_entry), 1, f)) | |
exit(1); | |
return ret; | |
} | |
void consume_record(file_entry *ent) { | |
printf("Entry: bar = %s, ", ent->bar); | |
if (ent->type == TYPEA) { | |
printf("fdata = %f\n", ent->data.fdata); | |
} | |
else if (ent->type == TYPEB) { | |
printf("intdata = %u\n", ent->data.intdata); | |
} | |
else { | |
printf("Unknown type %x\n", ent->type); | |
exit(1); | |
} | |
free(ent); | |
} | |
int main(int argc, char **argv) { | |
FILE *f = fopen(argv[1], "rb"); | |
file_header head; | |
parse_header(f, &head); | |
printf("File timestamp: %u\n", head.timestamp); | |
unsigned i; | |
for (i = 0; i < head.num_recs; i++) { | |
file_entry *ent = parse_record(f); | |
consume_record(ent); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment