/
Sapphire/bin/
Sapphire/db/
Sapphire/db/OLC_rooms/
Sapphire/db/abi/
Sapphire/db/em_src/
Sapphire/db/helps/
Sapphire/db/helps/emman/ifunc/
Sapphire/db/npcs/Tatt/
Sapphire/db/objects/Tatt/
Sapphire/db/q_data/
Sapphire/db/rooms/Tatt/
Sapphire/doc/
Sapphire/doc/em/
Sapphire/etc/
Sapphire/src/abic/
Sapphire/src/areacon/
Sapphire/src/client/
Sapphire/src/embc/
Sapphire/src/emi/
Sapphire/src/emi/test/
Sapphire/src/include/
Sapphire/src/sapphire/em/
Sapphire/src/tcon/
/*
 * 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 <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

#include "abic.h"


/*
 * Globals
 */
char                                                          cNullChar;


/*
 * Functions
 */
int main( int iArgC, char *pArgV[] )
{
    FILE *pFile;
    FILE *pFileOut;
    char cBuf[1028];
    char *pBuf;
    char c[2];
    int i;

    if ( iArgC > 1 )
    {
        strncpy( cBuf, pArgV[1], 1024 );
        cBuf[1024] = '\0';
        pFile      = open_file( cBuf, "r" );

        if ( iArgC > 2 )
            strncpy( cBuf, pArgV[2], 1024 );
        else
        {
            pBuf   = strrchr( cBuf, '.' );

            if ( !strcmp( pBuf, ".abi" ) || !strcmp( pBuf, ".ABI" ) )
                strcpy( pBuf, ".bin" );
            else
                strcat( pBuf, ".bin" );
        }
    }
    else
    {
        pFile      = stdin;
        strcpy( cBuf, "_a.bin" );
    }

    if ( ( pFileOut = fopen( cBuf, "wb" ) ) == NULL )
    {
        fprintf( stderr, "\n%s: %s.\n", pArgV[0], strerror( errno ) );

        if ( pFile != stdin )
            close_file( pFile );

        return ( 0 );
    }

    if ( pFile != stdin )
        fprintf( stderr, "%s: %s -> %s\n", pArgV[0], pArgV[1], cBuf );

    c[0]       = fget_letter( pFile );

    while ( feof( pFile ) == 0 )
    {
        switch ( c[0] )
        {
          case ';':
              while ( c[0] != '\n' && c[0] != EOF )
                  c[0]     = getc( pFile );

              if ( c[0] == '\n' )
                  lCurrentLine++;

              break;

          default :
              ungetc( c[0], pFile );
              pBuf         = fget_word( pFile );

              if ( strcmp( pBuf, "int1" ) == 0 )
              {
                  char cV  = (char) atoi( fget_word( pFile ) );

                  i        = fwrite( &cV, sizeof( char ), 1, pFileOut );

                  if ( i < 0 )
                      error( "%s: %s.", cBuf, strerror( errno ) );
              }
              else if ( strcmp( pBuf, "int2" ) == 0 )
              {
                  i2 iV    = (i2) atoi( fget_word( pFile ) );

                  i        = fwrite( &iV, sizeof( i2 ), 1, pFileOut );

                  if ( i < 0 )
                      error( "%s: %s.", cBuf, strerror( errno ) );
              }
              else if ( strcmp( pBuf, "int4" ) == 0 )
              {
                  i4 iV    = (i4) atol( fget_word( pFile ) );

                  i        = fwrite( &iV, sizeof( i4 ), 1, pFileOut );

                  if ( i < 0 )
                      error( "%s: %s.", cBuf, strerror( errno ) );
              }
              else if ( strcmp( pBuf, "float4" ) == 0 )
              {
                  f4 fV    = (f4) atof( fget_word( pFile ) );

                  i        = fwrite( &fV, sizeof( f4 ), 1, pFileOut );

                  if ( i < 0 )
                      error( "%s: %s.", cBuf, strerror( errno ) );
              }
              else if ( strcmp( pBuf, "float8" ) == 0 )
              {
                  f8 fV    = (f8) strtod( fget_word( pFile ),
                                    (char **) NULL );

                  i        = fwrite( &fV, sizeof( f8 ), 1, pFileOut );

                  if ( i < 0 )
                      error( "%s: %s.", cBuf, strerror( errno ) );
              }
              else if ( strcmp( pBuf, "string" ) == 0 )
              {
                  pBuf     = fget_string( pFile );
                  i        = fwrite( pBuf, 1, strlen( pBuf ), pFileOut );

                  if ( i < 0 )
                      error( "%s: %s.", cBuf, strerror( errno ) );
              }
              else
                  error( "Unknown literal type `%s'.", pBuf );

              break;
        }

        c[0]               = fget_letter( pFile );
    }

    fclose( pFileOut );

    if ( pFile != stdin )
        close_file( pFile );

    return ( 0 );
}


/*
 * End of main.c
 */