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

		MCBinaryFileIO.h
		Copyright: (c) 2001 Eovia Corp. All rights reserved.

		Author:	Jeremy
		Date:	4/9/2004

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

#ifndef __MCBinaryFileIO__
#define __MCBinaryFileIO__

#if CP_PRAGMA_ONCE
#pragma once
#endif

#include "MCCountedPtr.h"
#include "MCStream.h"

/// \addtogroup MCCore
/// \{

///	TMCBinaryFileIO is Used for reading from or writing to binary files.
/// It converts from big to little endian and the reverse, as appropriate.
///
class TMCBinaryFileIO : public TMCfstream
{
public:
	enum EByteOrder
	{
		kBigEndian,
		kLittleEndian
	};

	enum EFileAccess
	{
		kReadFile,
		kWriteFile
	};
public:
	TMCBinaryFileIO(IMCFile* file,IMCFile::EFilePermission fileRead,EByteOrder fileByteOrder);

	void 		GetFileName		(TMCString& fileName) { fFile->GetFileName(fileName); };
	void		GetFullPathName	(TMCString& fileName) { fFile->GetFileFullPathName(fileName); };
	int32		Tell();
	void		Seek(int32 offset, int8 origin);

protected:
	void ReorderIfNeeded(void* thing, int8 size);
	boolean SwapBytes();
	EByteOrder		fByteOrder;
	IMCFile* fFile;
};

/// Reads binary data from a file
///
class TMCBinaryFileReader : public TMCBinaryFileIO
{
public:
	TMCBinaryFileReader(IMCFile* file, EByteOrder fileByteOrder);

	int8   ReadChar();
	void Readint8(int8& value);
	void Readuint8(uint8& value);
	void Readint16(int16& value);
	void Readuint16(uint16& value);
	void Readint32(int32& value);
	void Readuint32(uint32& value);
	void Readreal32(real32& value);
    void Readint24(int32& value);
    // Read a 24 integer directly into the buffer.
    void Readint24Buffer(int8* outData);
private:


};

/// Writes binary data to a file
///
class TMCBinaryFileWriter : public TMCBinaryFileIO
{
public:
	TMCBinaryFileWriter(IMCFile* file, EByteOrder fileByteOrder);

	void WriteChar(int8 value);
	void Writeint8(int8 value);
	void Writeuint8(uint8 value);
	void Writeint16(int16 value);
	void Writeuint16(uint16 value);
	void Writeint32(int32 value);
	void Writeuint32(uint32 value);
	void Writereal32(real32 value);
	void Writereal64(real64 value);
	void SetMacOSInfo(uint32 creator, uint32 type);

private:

};

/// \}

#endif
