The datafiles only contain ascii data, here is the code I have to read in each item from a data file:
Code:
void *read_next_item( FILE *fp, int *status )
{
char *word;
void *value = NULL;
int letter, errstatus;
do // while( !value )
{
letter = fread_letter( fp );
if( letter == EOF )
{
*status = -1;
return status;
}
while( letter == '#' )
{
fread_to_eol( fp );
letter = fread_letter( fp );
if( letter == EOF )
{
*status = -1;
return status;
}
}
ungetc( letter, fp );
word = fread_word( fp, &errstatus );
if( errstatus )
{
*status = -1;
return NULL;
}
if( !str_cmp( word, "eof" ) || !str_cmp( word, "end" )
|| !str_cmp( word, "eof;" ) || !str_cmp( word, "end;" ) )
{
*status = -1;
return status;
}
else if( !str_cmp( word, "-Version" ) )
{
errstatus = (int)!fread_number_ii( fp, &file_version, NULL );
if( errstatus )
return NULL;
}
else if( !str_cmp( word, "-Date" ) )
{
file_date = fread_time( fp, &errstatus );
if( errstatus )
return NULL;
}
else
{
*status = get_top_entry( word );
if( global_data_table[*status].name == NULL )
{
bug( "Read unknown section header: %s", word );
return NULL;
}
value = load_complex_block( fp, global_data_table + *status );
if( !value )
{
bug( "read_next_item: error reading item" );
return NULL;
}
}
if( ( letter = fread_letter( fp ) != ';' ) )
{
if( letter == EOF )
bug( "read_next_item: EOF reached early." );
else
bug( "read_next_item: missing ';' character." );
return NULL;
}
}
while( !value );
return value;
}
The fread_word function follows:
Code:
char *fread_word( FILE *fp, int *status )
{
static char word[MAX_INPUT_LENGTH];
char *pword;
char cEnd;
*status = 0;
do
{
if( feof( fp ) )
{
bug( "fread_word: EOF encountered on read.\n\r" );
if( fBootDb )
exit( 1 );
return &str_empty[0];
}
cEnd = getc( fp );
}
while( isspace( cEnd ) );
if( cEnd == '\'' || cEnd == '"' )
{
pword = word;
}
else
{
word[0] = cEnd;
pword = word + 1;
cEnd = ' ';
}
for( ; pword < word + MAX_INPUT_LENGTH; pword++ )
{
int c = getc( fp );
if( c == EOF )
{
bug( "fread_word: returned EOF." );
*pword = '\0';
return word;
}
*pword = c;
if( cEnd == ' ' ? isspace( *pword ) : *pword == cEnd )
{
if( cEnd == ' ' )
ungetc( *pword, fp );
*pword = '\0';
return word;
}
}
bug( "Fread_word: word too long." );
*status = 1;
return NULL;
}
And also fread_letter:
Code:
// Read a letter from a file.
int fread_letter( FILE *fp )
{
int c;
do
{
if( feof( fp ) )
{
bug( "fread_letter: EOF encountered on read.\n\r" );
if( fBootDb )
exit( 1 );
return '#';
}
c = getc( fp );
}
while( isspace( c ) );
return c;
}
There is quite a lot of other additional code for reading other data types as well but I have checked these and I believe they all check for EOF characters.