/*
	Trampoline Sim.
	Copyright (c) 2009, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''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 DARRAGH COY 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.
*/

#ifndef BUFFERED_FILE_H
#define BUFFERED_FILE_H

//=====================================================================================================================
//=====================================================================================================================

namespace Game
{
	//=================================================================================================================
	/// @brief		Serves as a base class for all classes that buffer a 
	///				collection of data (file, embedded resource etc.) into 
	///				memory and allow that data to be read.
	//=================================================================================================================

	class BufferedFile
	{
		public:

			//=========================================================================================================
			// Enums
			//=========================================================================================================

				/// Seek modes: specifies which way to position the read marker in a file

				enum SeekMode
				{
					/// seek from the current offset
				
					FROM_CUR,
					
					/// seek from the end of the file
					
					FROM_END,
					
					/// seek from the beginning of the file
					
					FROM_BEG      
				};

			//=========================================================================================================
			// Functions
			//=========================================================================================================
			
				/// Constructor

				BufferedFile();
				
				/// Destructor
				
				virtual ~BufferedFile();	

				/// Copy constructor

				BufferedFile( const BufferedFile & other );

				/// Assignment operator

				BufferedFile & operator = ( const BufferedFile & other );

				/// Opens a given collection of data of the given name: this might be a disk file or something else depending on the implementation
				
				virtual void Open( std::string name ) = 0;
				
				/// Close the current file
				
				void Close();
				
				/// Read a given number of bytes into the destination
				
				void Read( void * dest , unsigned bytes );    
				
				/// Go to a particular position in the file
				
				void Seek( int offset , SeekMode s );     

				/// Return pointer to file data at current seek position

				void * GetDataAhead() const;  
				
				/// Tell what position in the file we are reading from
				
				unsigned GetPosition() const;
				
				/// Returns the size of the file
				
				unsigned Size()	const;
			
				/// Tells if a file is open
				  
				bool IsOpen() const;  
				
				/// Tells if the file is good for reading: returns false if not
				
				bool IsGood() const;  
				
				/// Tells if the end of the file has been reached:
				
				bool IsAtEof() const;
				
				/// Tells if there was an error reading from the file:
					
				bool HasError() const;  
				
				/// Tells if the specified number of bytes can be read without reaching the end of the file
				
				bool EnsureBytes( unsigned bytes ) const;

		protected:

			//=========================================================================================================
			// Variables
			//=========================================================================================================

				/// File data:

				unsigned char * m_data;

				/// Current position in the file:

				unsigned char * m_pointer;

				/// End of file pointer:

				unsigned char * m_end_pointer;

				/// Error flag: set when a read error occurs

				bool m_read_error;
	};
	
};

#endif // end of: ifndef BUFFERED_FILE_H
