/***********************************(GPL)********************************
*   wxHexEditor is a hex edit tool for editing massive files in Linux   *
*   This file is File Abraction Layer for wxHexEditor                   *
*   Copyright (C) 2010  Erdem U. Altinyurt                              *
*                                                                       *
*   This program is free software; you can redistribute it and/or       *
*   modify it under the terms of the GNU General Public License         *
*   as published by the Free Software Foundation; either version 2      *
*   of the License.                                                     *
*                                                                       *
*   This program is distributed in the hope that it will be useful,     *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of      *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
*   GNU General Public License for more details.                        *
*                                                                       *
*   You should have received a copy of the GNU General Public License   *
*   along with this program;                                            *
*   if not, write to the Free Software Foundation, Inc.,                *
*   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA        *
*                                                                       *
*               home  : www.wxhexeditor.org                             *
*               email : spamjunkeater@gmail.com                         *
*************************************************************************/

///This file also available under propertieary license.
///Contact admin for licensing it.

// Journaling File Structure :)
#ifndef FAL_H
#define FAL_H
#include <iostream>	//for std::cout...
#include <wx/wx.h>
#include <wx/file.h>
#include <wx/filename.h>
#include <wx/msgdlg.h>
#include <wx/dynarray.h>
#include <stdint.h>

#ifdef __WXMAC__
	#include <sys/disk.h>
#endif

#ifdef WX_GCH
	#include <wx_pch.h>
#else
	#include <wx/wx.h>
#endif

#ifdef __WXMSW__
	#include <windows.h>
	#include <winioctl.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>
#include <unistd.h>

#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>

#ifdef __WXGTK__
	#include <sys/ioctl.h>
	//#include <dev/disk.h>
	#include <linux/fs.h>
#endif

int FDtoBlockSize( int FD );
uint64_t FDtoBlockCount( int FD );

class DiffNode{
	public:
		bool flag_undo;			//For undo
		bool flag_commit;			//For undo
		bool flag_inject;			//this is for file size extension. If this flag is true, that means a new data inserted to file
		uint64_t start_offset;	//start byte of node
		int64_t size;				//size of node
		unsigned char *old_data;			//old data buffer
		unsigned char *new_data;			//new data buffer
		inline uint64_t end_offset( void ){ return start_offset + abs(size); }
//	DiffNode *prev, *next;
	DiffNode( uint64_t start, int64_t size_, bool inject){
		start_offset = start;
		flag_inject = inject;
		size = size_;
		flag_commit = flag_undo = false;
		old_data = new_data = NULL;
		}
	~DiffNode( void ){
		delete [] new_data;
		delete [] old_data;
		}
	};

WX_DECLARE_OBJARRAY(DiffNode *, ArrayOfNode);

class FAL : private wxFile{
	public:
	enum FileAccessMode { ReadOnly, ReadWrite, DirectWrite, AccessInvalid };
	    FAL(wxFileName& myfilename, FileAccessMode FAM = ReadOnly, unsigned ForceBlockRW=0);
		~FAL();
//		friend class FindDialog;
		wxString oldOwner;

		bool SetAccessMode( FileAccessMode fam );
		int GetAccessMode( void );
		wxString GetAccessModeString( void );
		wxString FAMtoString( FileAccessMode& FAM );
		wxFileName GetFileName( void );

		bool IsChanged( void );	//returns if file is dirty or not
		bool Apply( void );		//flush changes to file
		int64_t Undo( void );	//undo last action
		int64_t Redo( void );	//redo last undo
		void ShowDebugState( void );
		wxFileOffset Length( void );

		wxFileOffset Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart);
		bool Close(){ return wxFile::Close();};
		bool IsOpened(){ return wxFile::IsOpened(); };
		int  fd() const { return wxFile::fd(); };
		long Read( char* buffer, int size );
		long Read( unsigned char* buffer, int size );
		void SetXORKey( wxMemoryBuffer );
		wxMemoryBuffer GetXORKey( void );
		void ApplyXOR( unsigned char* buffer, unsigned size, uint64_t from );

		bool Add( uint64_t start_byte, const char* data, int64_t size, bool injection=false ); //adds new node
		bool IsAvailable_Undo( void );
		bool IsAvailable_Redo( void );
		bool IsInjected( void );
		const DiffNode* GetFirstUndoNode( void );
		int GetBlockSize( void );

	protected:
		long ReadR( unsigned char* buffer, unsigned size, uint64_t location, ArrayOfNode *Patches, int PatchIndice );

		void RemoveTail( DiffNode *remove_node );	//remove further tails.
		DiffNode* NewNode( uint64_t start_byte, const char* data, int64_t size, bool extension = false );
		long DeletionPatcher( uint64_t location, unsigned char* data, int size, ArrayOfNode *Patches, int PatchIndice);
		long InjectionPatcher( uint64_t location, unsigned char* data, int size, ArrayOfNode *Patches, int PatchIndice);
		void ModificationPatcher( uint64_t location, unsigned char* data, int size, DiffNode* Patch);

	private:
		int BlockRWSize;
		uint64_t BlockRWCount;
		FileAccessMode file_access_mode;
		ArrayOfNode DiffArray;
		ArrayOfNode TempDiffArray;
		wxFileName the_file;
		uint64_t put_ptr,get_ptr;
		wxMemoryBuffer XORview;
//		DiffNode *head,*tail;	//linked list holds modification record

};

#endif // FAL_H
