/******************************************************************************
*	File		-	binary_file_buffer.h
*	Author		-	Joey Pollack
*	Date		-	2020/01/24 (y/m/d)
*	Mod Date	-	2020/01/24 (y/m/d)
*	Description	-	Reads an entire binary file into memory and allows for
*					extracting the data in chunks. Much faster than reading
*					large files chunk by chunk.
******************************************************************************/

#ifndef BINARY_FILE_BUFFER_H_
#define BINARY_FILE_BUFFER_H_

#include <string>

namespace lunarium
{
	class BinaryFileBuffer
	{
	public:
		BinaryFileBuffer();
		~BinaryFileBuffer();

		/// Create a new file buffer of the given starting size
		/// This will clear out any existing buffer
		bool NewFile(int reserve_size);
		bool WriteFile(const char* filename, bool include_extra_bytes = false);
		bool LoadFile(const char* filename);
		void Unload();
		bool IsLoaded() const;
		const std::string& LoadedFileName() const;
		int GetFileSize() const;
		const unsigned char* GetBuffer() const;

		bool Read(char* buffer, int numBytes);
		int ReadInt();
		short ReadShort();
		char ReadByte();
		unsigned int ReadUInt();
		unsigned short ReadUShort();
		unsigned char ReadUByte();

		bool Write(char* buffer, int num_bytes);
		void WriteInt(int value);
		void WriteShort(short value);
		void WriteByte(char value);
		void WriteUInt(unsigned int value);
		void WriteUShort(unsigned short value);
		void WriteUByte(unsigned char value);

		void SeekTo(int pos);

	private:

		bool mbIsLoaded;
		std::string mFileName;
		int mFileSize;
		unsigned char* mpBuffer;
		int mBufferPos;

	};
}

#endif	// BINARY_FILE_BUFFER_H_