/*
* Copyright (C) 1995-1997 Christopher D. Granz
*
* This header may not be removed.
*
* Refer to the file "License" included in this package for further
* information and before using any of the following.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <errno.h>
#include "abic.h"
/*
* Globals
*/
char * pCOFilename;
long lCurrentLine;
/*
* Functions
*/
FILE *open_file( char *pFilename, char *pFlags )
{
FILE *pFile;
if ( ( pFile = fopen( pFilename, pFlags ) ) == NULL )
fatal( "%s: %s.", pFilename, strerror( errno ) );
pCOFilename = str_dup( pFilename );
lCurrentLine = 1;
return ( pFile );
}
void close_file( FILE *pFile )
{
if ( fclose( pFile ) != 0 )
error( "%s: %s.", pCOFilename, strerror( errno ) );
str_free( pCOFilename );
}
/*
* Get one letter from a file.
*/
char fget_letter( FILE *pFile )
{
char c = '\0';
do
{
if ( feof( pFile ) != 0 )
break;
if ( ( c = getc( pFile ) ) == '\n' )
lCurrentLine++;
}
while ( isspace( c ) != 0 );
return ( c );
}
char *fget_word( FILE *pFile )
{
static char cOutput[MAX_INPUT];
char c = '\0';
int i;
do
{
if ( ( c = getc( pFile ) ) == EOF )
break;
if ( c == '\n' )
lCurrentLine++;
}
while ( isspace( c ) != 0 );
for ( i = 0; isspace( c ) == 0; i++ )
{
if ( i >= ( MAX_INPUT - 1 ) )
{
error( "Word too long." );
return ( EMPTY_STRING );
}
cOutput[i] = c;
if ( ( c = getc( pFile ) ) == EOF )
break;
if ( c == '\n' )
lCurrentLine++;
}
cOutput[i] = '\0';
return ( cOutput );
}
char *fget_string( FILE *pFile )
{
static char cOutput[MAX_STRING];
char c[2] = { '\0', '\0' };
int i;
do
{
if ( ( c[0] = getc( pFile ) ) == EOF )
{
error( "Unexpected EOF." );
return ( EMPTY_STRING );
}
if ( c[0] == '\n' )
lCurrentLine++;
}
while ( isspace( c[0] ) );
if ( c[0] != '"' )
{
error( "Symbol `\"' not found." );
return ( EMPTY_STRING );
}
c[0] = getc( pFile );
if ( c[0] == '\n' )
lCurrentLine++;
if ( c[0] == '"' )
return ( EMPTY_STRING );
for ( i = 0; ; i++ )
{
if ( i >= ( MAX_STRING - 1 ) )
{
error( "String too long." );
return ( EMPTY_STRING );
}
if ( feof( pFile ) != 0 )
{
error( "Unexpected EOF." );
return ( EMPTY_STRING );
}
if ( c[0] == '\\' )
{
switch ( ( c[0] = getc( pFile ) ) )
{
case EOF :
error( "Unexpected EOF." );
return ( EMPTY_STRING );
case '\\': cOutput[i] = '\\'; break;
case '"' : cOutput[i] = '"'; break;
case 'n' : cOutput[i] = '\n'; break;
case 'r' : cOutput[i] = '\r'; break;
case 't' : cOutput[i] = '\t'; break;
case 'a' : cOutput[i] = '\a'; break;
default :
error( "Unknown escape '\\%s'.", c );
break;
}
}
else if ( c[0] != '\n' && c[0] != '\r' && c[0] != '\t' )
cOutput[i] = c[0];
else
i--;
c[0] = getc( pFile );
if ( c[0] == '\n' )
lCurrentLine++;
if ( c[0] == '"' )
break;
}
cOutput[i + 1] = '\0';
return ( cOutput );
}
/*
* End of fileio.c
*/