/* inputfile.c */

/****************************************************************************
 *
 *  Copyright (C) 2000-2001 Eli-Jean R. Leyssens, aka Pervect of Topix
 *
 *  This file is part of the CodePressor package.
 *
 *  CodePressor is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  CodePressor is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with CodePressor; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 *
 *  Eli-Jean R. Leyssens can be reached via email at eli@dnd.utwente.nl
 *  snail: E-J.R. Leyssens, Schivelbeinerstr. 5, 10439 Berlin, Germany
 *
 ***************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <oslib/fileswitch.h>
#include <oslib/osfile.h>

#include "asm.h"
#include "inputfile.h"

bits	     InputLoadAddress;
TASMNumber   tInputLoadAddress;
bits	     InputExecAddress;
int          InputAlignedSize;
int	     InputSize;
bits	     InputFileType;
byte         * pbInputFile = NULL;

bool ReadInputFile( char * szPath) {
    bool fRetval = TRUE;

    fileswitch_object_type object_type;

    //
    // Release any input file still resident in memory
    //
    if ( pbInputFile != NULL) {
       free( pbInputFile);
       pbInputFile = NULL;
    }

    object_type = osfile_read_stamped_no_path( szPath, &InputLoadAddress,
    		  &InputExecAddress, &InputSize, NULL, &InputFileType);
    switch ( object_type) {
      case fileswitch_NOT_FOUND:
      	fprintf( stderr, "File not found.\n");
      	fRetval = FALSE;
      	break;
      case fileswitch_IS_FILE:
      	break;
      default:
        fprintf( stderr, "Not a file.\n");
      	fRetval = TRUE;
      	break;
    }
    if ( fRetval) {
       if ( InputSize == 0) {
          fprintf( stderr, "File is empty.\n");
          fRetval = FALSE;
       }
    }
    if ( fRetval) {
      switch ( InputFileType) {
        case 0xffffffff:
          break;
        case 0xff8:
          InputLoadAddress = 0x8000;
          InputExecAddress = 0x8000;
          break;
        default:
          fprintf( stderr, "Can't handle filetype %8x\n", InputFileType);
          fRetval = FALSE;
      }
    }

    if ( fRetval) {
       NumberToTASMNumber( InputLoadAddress, &tInputLoadAddress);
    }

    if ( fRetval) {
        //
        // Information about input file gathered and checked
        //
        // Now load the input file into memory
        //

        InputAlignedSize = 4*( (InputSize + 3) >> 2);
        pbInputFile = (byte *) malloc( InputAlignedSize);
        if ( pbInputFile != NULL) {
            pbInputFile[ InputAlignedSize - 4] = 0;
            pbInputFile[ InputAlignedSize - 3] = 0;
            pbInputFile[ InputAlignedSize - 2] = 0;
            pbInputFile[ InputAlignedSize - 1] = 0;
            osfile_load_stamped_no_path( szPath, pbInputFile, NULL, NULL,
                                          NULL, NULL);
        } else {
            // Not enough memory to load file
            fprintf( stderr, "Failed to claim memory for input file.\n");
            fRetval = FALSE;
        }
    }

    return fRetval;
}
