I Wrote this along time ago for a mate who wanted a lap time recorder for a website to publish the best times
/*
* Let's assume that you only want the information printed, that you don't need
* to have it all in memory at once. The program then takes a different turn...
*/
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
/* also ident in <windows.h> too */
typedef struct _FILETIME
{
unsigned int dwLowDateTime;
unsigned int dwHighDateTime;
} FILETIME;
struct TrackRecordHeader
{
unsigned int version; /* version == 0x0000000a */
unsigned int trackRecordCount; /* Number of track records stored in input */
};
void foo(float value, FILE *file)
{
int input, hours, minutes, seconds, milli;
input = value;
hours = input / 3600;
input -= hours * 3600;
minutes = input / 60;
input -= minutes * 60;
seconds = input;
value -= (int)value; /* get rid of integer portion */
milli = value * 1000;
fprintf(file, "time = %02d:%02d.%03d\n", minutes, seconds, milli);
}
int main(int argc, char *argv[])
{
if ( argc > 2 )
{
const char *infilename = argv[1];
const char *outfilename = argv[2];
FILE *input = fopen(infilename, "rb"); /* binary input */
FILE *output = fopen(outfilename, "w"); /* text output */
if ( input )
{
printf("Opened \"%s\".\n", infilename);
if ( output )
{
printf("Opened \"%s\".\n", outfilename);
}
else
{
perror(outfilename);
}
}
else
{
perror(infilename);
}
size_t count;
union /* one of the things that we will be reading */
{
struct TrackRecordHeader header;
char text[128];
wchar_t wtext[128 / sizeof(wchar_t)];
FILETIME dateTime;
float fvalue;
} data;
unsigned int x, length; /* helpers for the things we are reading */
// printf("Opened \"%s\" for input.\n", infilename);
// printf("Outputting to file \"%s\".\n", outfilename);
/*
* Now, let's get to it. Read the header first.
*/
count = fread(&data.header, sizeof data.header, 1, input);
if ( count != 1 )
{
puts("problem with fread of header");
return 0;
}
// printf("version = %u\n", data.header.version);
// printf("records = %u\n", data.header.trackRecordCount);
/*
* We need to loop about the number of records...
*/
length = data.header.trackRecordCount;
for ( x = 0; x < length; ++x )
{
unsigned int y, len; /* text length */
/*
* First part of record: length of the playerName to follow.
*/
count = fread(&len, sizeof len, 1, input);
if ( count != 1 )
{
puts("problem with fread of playerNameLength");
return 0;
}
// printf("playerNameLength = %u\n", len);
/*
* Next item: the wchar text of playerName.
*/
count = fread(data.wtext, len, sizeof *data.wtext, input);
if ( count != sizeof *data.wtext )
{
puts("problem with fread of playerName");
return 0;
}
/*
* The strings in the input, wide or not, are not null terminated.
* Let's just print it the hard way.
*/
fputs("[lap time]\n", output);
// wprintf("playerName = " L"%s\n", data.wtext);
fputs("playerName = ", output);
fputws(data.wtext, output);
fputs("\n", output);
/* fputs("playerName = ", stdout);
fputs("playerName = ", output);
for ( y = 0; y < len; ++y )
{
(void)putw(data.wtext[y], stdout);
(void)putw(data.wtext[y], output);
}
puts("\n"); */
/*
* Next item: the length of the carName to follow.
*/
count = fread(&len, sizeof len, 1, input);
if ( count != 1 )
{
puts("problem with fread of carNameLength");
return 0;
}
// printf("carNameLength = %u\n", len);
/*
* Next item: the char text of carName.
*/
count = fread(data.text, len, sizeof *data.text, input);
if ( count != sizeof *data.text )
{
puts("problem with fread of playerName");
return 0;
}
/*
* The strings in the input, wide or not, are not null terminated.
* Let's just print it the hard way.
*/
// fputs("carName = ", stdout);
fputs("carName = ", output);
for ( y = 0; y < len; ++y )
{
// putc(data.text[y], stdout);
putc(data.text[y], output);
}
// puts("\n");
fputs("\n", output);
/*
* Next item: the time/date stamp.
*/
count = fread(&data.dateTime, sizeof data.dateTime, 1, input);
if ( count != 1 )
{
puts("problem with fread of dateTime");
return 0;
}
/*
* Do something with dateTime if you want.
* I'll just throw it away and continue on.
*/
/*
* Next item: the time.
*/
count = fread(&data.fvalue, sizeof data.fvalue, 1, input);
if ( count != 1 )
{
puts("problem with fread of dateTime");
return 0;
}
// printf("Lap time = %f\n", data.fvalue);
// foo(data.fvalue, stdout);
foo(data.fvalue, output);
// puts("\n");
fputs("\n", output);
}
/*
* Now it's time for the information at the end of the input.
* Apparently, we've got a number of checkpoint times. Find out how many.
*/
count = fread(&length, sizeof length, 1, input);
if ( count != 1 )
{
puts("problem with fread of numberOfCheckpointTimes");
return 0;
}
// printf("numberOfCheckpointTimes = %u\n", length);
/*
* Show each of the checkpoint times.
*/
for ( x = 0; x < length; ++x )
{
/*
* Next item: the checkpointTimes.
*/
count = fread(&data.fvalue, sizeof data.fvalue, 1, input);
if ( count != 1 )
{
puts("problem with fread of checkpointTimes");
return 0;
}
// printf("checkpointTimes = %f\n", data.fvalue);
// foo(data.fvalue, output);
// foo(data.fvalue, stdout);
}
/*
* Clean up and leave.
*/
fclose(input);
fclose(output);
}
return 0;
}