/* outputfile.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 <oslib/osgbpb.h>
#include <oslib/osfind.h>
#include <oslib/osfile.h>

#include "outputfile.h"

char * szOutputPath;
bits        OutputLoadAddress;
bits        OutputExecAddress;

os_f fhOutput;
os_error * e;

void SetOutputFile( char * szPath) {
    szOutputPath = szPath;
}

bool OpenOutputFile( void) {
     bool fRetval = TRUE;

     if ( ( e = xosfind_openout( osfind_NO_PATH, szOutputPath, NULL, &fhOutput)) != NULL) {
         fprintf( stderr, "Failed to create output file.\n");
         fRetval = FALSE;
     }
     return fRetval;
}

bool WriteToOutputFile( byte * pbMemory, int Length) {
     bool fRetval = TRUE;

     if ( ( e = xosgbpb_write( fhOutput, pbMemory, Length, NULL)) != NULL) {
         fprintf( stderr, "Failed to write to output file.\n");
         fRetval = FALSE;
     }
     return fRetval;
}

bool CloseOutputFile( void) {
     bool fRetval = TRUE;

     if ( ( e = xosfind_close( fhOutput)) != NULL) {
         fprintf( stderr, "Failed to close output file.\n");
         fRetval = FALSE;
     }
     if ( fRetval) {
         // Set load/exec addresses
         osfile_write_load( szOutputPath, OutputLoadAddress);
         osfile_write_exec( szOutputPath, OutputExecAddress);
     }
     return fRetval;
}
