/*
**	$Id: IOFile.cpp 1.5 1995/10/13 11:43:06 olsen Exp olsen $
**
**	:ts=4
*/

/*
 * Copyright © 1995 by Olaf Barthel, All Rights Reserved
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
 * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software has not been validated by the ``Bundesamt fuer Zulassungen in
 * der Telekommunikation'' of the ``Deutsche Bundepost Telekom'' and thus
 * must not be used for accessing the BTX-Network of the Telekom in Germany.
 *
 * Diese Software hat keine Zulassung durch das Bundesamt fuer Zulassungen in
 * der Telekommunikation der Deutschen Bundespost Telekom und darf daher nicht
 * am Netz der Deutschen Bundespost Telekom in Deutschland betrieben werden.
 */

/****************************************************************************/

#include <exec/memory.h>
#include <dos/dosextens.h>
#include <dos/stdio.h>

#include <clib/exec_protos.h>
#include <clib/dos_protos.h>

#ifdef __SASC
#include <pragmas/exec_pragmas.h>
#include <pragmas/dos_pragmas.h>

extern struct ExecBase *SysBase;
extern struct DosLibrary *DOSBase;
#endif	// __SASC

/****************************************************************************/

#ifndef _IOFILE_HPP
#include "IOFile.hpp"
#endif

/****************************************************************************/

/* this macro lets us long-align structures on the stack */
#define D_S(type,name) char a_##name[sizeof(type)+3]; \
                       type *name = (type *)((LONG)(a_##name+3) & ~3);

IOFile::IOFile()
{
	FileHandle = NULL;
}

IOFile::~IOFile()
{
	Close();
}

LONG IOFile::Open(CONST STRPTR Channel,ULONG Unit,ULONG Baud,BOOL RTS_CTS)
{
	LONG Error;

	Close();

	if(FileHandle = ::Open((char *)Channel,MODE_OLDFILE))
	{
		BPTR FileLock = Lock((char *)Channel,SHARED_LOCK);

		if(FileLock)
		{
			D_S(struct FileInfoBlock,FileInfo);

			if(Examine(FileLock,FileInfo))
			{
				Size = FileInfo->fib_Size;
				BytesRead = 0;

				UnLock(FileLock);

				SetVBuf(FileHandle,NULL,BUF_FULL,4096);

				return(0);
			}
			else
				Error = IoErr();

			UnLock(FileLock);
		}
		else
			Error = IoErr();

		Close();
	}
	else
		Error = IoErr();

	return(Error);
}

VOID IOFile::Close(VOID)
{
   if(FileHandle)
   {
	   ::Close(FileHandle);
	   FileHandle = NULL;
   }
}

LONG IOFile::GetChar(LONG Timeout)
{
	if(FileHandle)
	{
		LONG Result = FGetC(FileHandle);

		if(Result < 0)
			return(CHANNELERR_EOF);
		else
		{
			BytesRead++;

			return(Result);
		}
	}
	else
		return(CHANNELERR_EOF);
}

LONG IOFile::Waiting(VOID)
{
	if(FileHandle)
		return(Size - BytesRead);
	else
		return(0);
}
