/*
	Lan Wars.
	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.
*/

//=====================================================================================================================
/// @brief		Simple class that buffers an entire file into memory and allows binary data to be read 
///				from that file. Useful for speeding up small io operations on files.
//=====================================================================================================================

#include "Precompiled.h"
#include "Files\DiskFile.h"

using namespace Game;

//=====================================================================================================================
/// @brief		Opens the given disk file and buffers it into memory
/// @param		file_name - Name of the file to try and open
//=====================================================================================================================

void DiskFile::Open( std::string file_name )
{
    // Close the current file first:

    Close();

    // Don't bother if there is no file name:

    if ( file_name.size() == 0 ) return;

	// Try and open the file:

	HANDLE file_handle = CreateFileA
	(	
		file_name.data()			,		// file name
		FILE_READ_DATA				,		// access level: want to read
		FILE_SHARE_READ				,		// allow other processes to read only, but not write or delete
		0							,		// security attributes: don't care
		OPEN_EXISTING				,		// don't create if it doesn't exist
		FILE_FLAG_SEQUENTIAL_SCAN	,		// sequential access hint
		0									// template file ? whats this.. 
	);

	// See if the file handle was valid:

	if ( file_handle != INVALID_HANDLE_VALUE )
	{
		// Get the size of the file

		unsigned file_size = GetFileSize(file_handle,0);

		// Make sure the size is ok:

		if ( (DWORD) file_handle != INVALID_FILE_SIZE )
		{
			// Declare the array to hold the file data:
			
			m_data = new unsigned char[file_size];

			// Make sure the array is valid:

			if ( m_data != 0 )
			{
				// Store the number of bytes read here:

				unsigned bytes_read = 0;

				// Read the entire file:

				bool success = ReadFile
				(
					file_handle				,	// file to read
					m_data					,	// data to read
					file_size				,	// size of file
					(LPDWORD) &bytes_read	,	// number of bytes read
					0							// overlapped io struct: not needed
					
				) != 0;

				// See if we were successful:

				if ( success && bytes_read == file_size )
				{
					// Success: setup file pointers

					m_pointer		= m_data;
					m_end_pointer	= m_data + file_size;
					m_read_error	= false;
				}
				else
				{
					// Not successful: clear data array

					delete[] m_data; m_data = 0;
				}

			}	// end if array declared ok

		}
		
		// Close the file after we are done:

		CloseHandle(file_handle);

	}	// end if file opened ok
	
}


