/*
 * 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 "areacon.h"
/*
 * Globals
 */
extern char                                                   cNullChar;
/*
 * Functions
 */
/*
 * Gets one letter from a file.
 */
char fget_letter( FILE *pFile )
{
    char c;
    do
    {
        if ( feof( pFile ) )
        {
            error( "Unexpected EOF." );
            return ( '\0' );
        }
        c = getc( pFile );
    }
    while ( isspace( c ) != 0 );
    return ( c );
}
/*
 * Gets one word (terminated by a space) from a file.
 */
char *fget_word( FILE *pFile )
{
    static char cOutput[MAX_STRING];
    char c        = '\0';
    int i;
    do
    {
        if ( feof( pFile ) != 0 )
            break;
        c          = getc( pFile );
    }
    while ( isspace( c ) != 0 );
    for ( i = 0; isspace( c ) == 0; i++ )
    {
        if ( i >= ( MAX_INPUT - 1 ) )
        {
            error( "Word too long." );
            return ( EMPTY_STRING );
        }
        if ( feof( pFile ) != 0 )
            break;
        cOutput[i] = c;
        c          = getc( pFile );
    }
    cOutput[i]     = '\0';
    return ( cOutput );
}
void fput_string( FILE *pFile, char *pFormat, ... )
{
    va_list pArgs;
    char cBuf[MAX_STRING];
    int i;
    va_start( pArgs, pFormat );
#ifdef _sysBSD
    vsnprintf( cBuf, MAX_STRING, pFormat, pArgs );
#else
    vsprintf( cBuf, pFormat, pArgs );
#endif
    putc( '\"', pFile );
    for ( i = 0; cBuf[i] != '\0'; i++ )
    {
        switch ( cBuf[i] )
        {
          case '\\': putc( '\\', pFile ); putc( '\\', pFile ); break;
          case '"' : putc( '\\', pFile ); putc( '"', pFile );  break;
          case '\n':
              putc( '\\', pFile );
              putc( 'n', pFile );
              if ( cBuf[i + 1] != '\r' )
                  putc( '\n', pFile );
              break;
          case '\r':
              putc( '\\', pFile );
              putc( 'r', pFile );
              if ( i > 0 && cBuf[i - 1] == '\n' )
                  putc( '\n', pFile );
              break;
          case '\t': putc( '\\', pFile ); putc( 't', pFile );  break;
          case '\a': putc( '\\', pFile ); putc( 'a', pFile );  break;
          default  : putc( cBuf[i], pFile );                   break;
        }
    }
    putc( '\"', pFile );
    va_end( pArgs );
}
char *fget_string( FILE *pFile )
{
    static char cOutput[MAX_STRING];
    char c[2]      = { '\0', '\0' };
    int i;
    do
    {
        if ( feof( pFile ) != 0 )
        {
            error( "Unexpected EOF." );
            return ( EMPTY_STRING );
        }
        c[0]       = getc( pFile );
    }
    while ( isspace( c[0] ) != 0 );
    if ( c[0] == '~' )
        return ( EMPTY_STRING );
    for ( i = 0; c[0] != '~'; i++ )
    {
        if ( i >= ( MAX_STRING - 1 ) )
        {
            error( "String too long." );
            return ( EMPTY_STRING );
        }
        if ( feof( pFile ) != 0 )
        {
            error( "Unexpected EOF." );
            return ( EMPTY_STRING );
        }
        cOutput[i] = c[0];
        switch ( c[0] )
        {
          case '\n': cOutput[++i] = '\r'; break;
          default  : break;
        }
        c[0]       = getc( pFile );
    }
    cOutput[i]     = '\0';
    return ( str_dup( cOutput ) );
}
/*
 * Get a string until the end of the line is reached.
 */
char *fget_string_eol( FILE *pFile )
{
    static char cOutput[MAX_STRING];
    char c[2]      = { '\0', '\0' };
    int i;
    do
    {
        if ( feof( pFile ) != 0 )
        {
            error( "Unexpected EOF." );
            return ( EMPTY_STRING );
        }
        c[0]       = getc( pFile );
    }
    while ( c[0] == ' ' );
    if ( c[0] == '\n' )
        return ( EMPTY_STRING );
    for ( i = 0; c[0] != '\n'; i++ )
    {
        if ( i >= ( MAX_STRING - 1 ) )
        {
            error( "String too long." );
            return ( EMPTY_STRING );
        }
        if ( feof( pFile ) != 0 )
            break;
        cOutput[i] = c[0];
        c[0]       = getc( pFile );
    }
    cOutput[i]     = '\0';
    do
    {
        if ( feof( pFile ) != 0 )
            break;
        c[0]       = getc( pFile );
    }
    while ( c[0] == '\n' || c[0] == '\r' );
    fseek( pFile, -1L, SEEK_CUR );
    return ( cOutput );
}
/*
 * Gets a number from a file.
 */
long fget_number( FILE *pFile )
{
    char cBuf[MAX_INPUT];
    char c;
    long lOutput;
    int i;
    bool b      = FALSE;
    do
    {
        if ( feof( pFile ) != 0 )
        {
            error( "Unexpected EOF." );
            return ( 0 );
        }
        c       = getc( pFile );
    }
    while ( isspace( c ) != 0 );
    if ( c == '+' )
        c       = getc( pFile );
    else if ( c == '-' )
    {
        b       = TRUE;
        c       = getc( pFile );
    }
    for ( i = 0; isdigit( c ); i++ )
    {
        if ( i >= ( MAX_INPUT - 1 ) )
        {
            error( "Word too long." );
            return ( 0 );
        }
        if ( feof( pFile ) != 0 )
        {
            error( "Unexpected EOF." );
            return ( 0 );
        }
        cBuf[i] = c;
        c       = getc( pFile );
    }
    cBuf[i]     = '\0';
    if ( cBuf[0] == '\0' )
    {
        error( "Number not found." );
        return ( 0 );
    }
    lOutput     = atol( cBuf );
    return ( ( b == TRUE ? -lOutput : lOutput ) );
}
/*
 * (Taken from db.c of ROM2.4)
 */
static long flag_convert( char cLetter )
{
    char c;
    long lBitSum     = 0;
    if ( 'A' <= cLetter && cLetter <= 'Z' ) 
    {
        lBitSum      = 1;
        for ( c = cLetter; c > 'A'; c-- )
            lBitSum *= 2;
    }
    else if ( 'a' <= cLetter && cLetter <= 'z' )
    {
        lBitSum      = 67108864; /* 2^26 */
        for ( c = cLetter; c > 'a'; c-- )
            lBitSum *= 2;
    }
    return ( lBitSum );
}
/*
 * (Taken from db.c of ROM2.4.)
 */
long fget_flags( FILE *pFile )
{
    int i;
    char c;
    bool b     = FALSE;
    do
    {
        c      = getc( pFile );
    }
    while ( isspace( c ) != 0 );
    if ( c == '-' )
    {
        b      = TRUE;
        c      = getc( pFile );
    }
    i          = 0;
    if ( isdigit( c ) == 0 )
    {
        while ( ( 'A' <= c && c <= 'Z' ) || ( 'a' <= c && c <= 'z' ) )
        {
            i += flag_convert( c );
            c  = getc( pFile );
        }
    }
    while ( isdigit( c ) != 0 )
    {
        i      = ( i * 10 + c - '0' );
        c      = getc( pFile );
    }
    if ( c == '|' )
        i     += fget_flags( pFile );
    else if ( c != ' ' )
        ungetc( c, pFile );
    if ( b == TRUE )
        return ( -1 * i );
    return ( i );
}
/*
 * End of fileio.c
 */