//  define the default base dir...
#define AF_DEFAULT_DIR  "C:\\NEWTEK\\"
#include "CoHack.h"

//#define DATAFILE	"c:\\trash\\demo\\data.lzw"
#define DATAFILE	"data.lzw"
#define PAKMODE		1
class fileOperations
{
public:
	char name[512];		// this should be enough...
	long fileSize;

	fileOperations()
	{
		fileSize = -1;
	}

	fileOperations(char *filename)
	{
		strcpy(name,filename);
		fileSize = -1;
		//  get Here the code to modify paths and filename.... :) class rules
		mustBeFound();
	}

	fileOperations(char *filename, bool & valid)
	{
		strcpy(name,filename);
		fileSize = -1;
		//  get Here the code to modify paths and filename.... :) class rules
		if( !couldBeFound() ) valid = false;
		else valid = true;
	}

	bool checkExtension(char *ext)
	{
		long nChar = 0;
		while (ext[nChar]!=0) nChar++;
		long a=0;
		while(name[a]!=0) a++;
		a -= nChar;
		if( name[a-1] != '.') return false;
		for(long i=0;i<nChar;i++)
		{
			if( name[a+i] != ext[i]) return false;
		}
		return true;
	}

	bool addBaseDir(char *hopA)
	{
		char temp[512];
		strcpy(temp,hopA);
		long a=0;
		while(temp[a] != 0) a++;
		long o=0;
		while(name[o] != 0) temp[o+a] = name[o++];
		temp[o+a] = 0;
		FILE *h = fopen(temp,"rb");
		if( h == NULL) return false;
		else
		{
			fclose(h);
			strcpy(name,temp);
			return true;
		}
	}

	bool checkExeDir()
	{
		MyString ms;
		long a = 0;
		while(name[a++] != 0);
		while(name[a] != '\\') a--;

		ms = exeDir;
		ms += name+a;
		FILE *h = fopen( ms.data, "rb");
		if( h == NULL ) return false;
		else
		{
			fclose(h);
			strcpy(name,ms.data);
			return true;
		}
	}

	bool couldBeFound()
	{
		if( !PAKMODE )
		{
			FILE *h = fopen(name,"rb");
			if( h == NULL)
			{
				if( !addBaseDir(AF_DEFAULT_DIR)) 
				{
					if ( !checkExeDir() ) return false;
				}
			}
			else fclose(h);
			return true;
		}
		else
		{

//		compact mode
			CoHack compak;
			long returnVal = compak.hackIt(DATAFILE, name, compak.CO_MODE_DECRUNCH | compak.CO_MODE_GETSIZE);
			// file not found
			if(returnVal == -4) return false;
			else return true;
		}

	}

	void mustBeFound()
	{
		if( !couldBeFound() ) quitMessage("fileOperations: file can not be found", name);
	}

	long getfilesize()
	{
		if( !PAKMODE )
		{

			FILE	*fHandle;
			long length;

			fHandle = fopen(name,"rb");
			if(fHandle == NULL) quitMessage("fileOperations::getfilesize : Can't open file", name);
			fseek(fHandle, 0L, SEEK_END);
			fileSize = length = ftell(fHandle);
			fclose(fHandle);
			return length;
		}
		else
		{
//		compact mode
			CoHack compak;
			if( fileSize == -1 )
				fileSize = compak.hackIt(DATAFILE, name, compak.CO_MODE_DECRUNCH | compak.CO_MODE_GETSIZE);
			return fileSize;	
		}
	}

	void *qloadfile()
	{
		if( !PAKMODE ) 
		{
			FILE    *fHandle;

			if(fileSize == -1) getfilesize();
			void *adr=malloc(fileSize);
			fHandle=fopen(name,"rb");
			if(fHandle == NULL) quitMessage("fileOperations::getfilesize:Can't open file", name);
			fread(adr,fileSize,1,fHandle);
			fclose	(fHandle);
			return adr;
		}
		else
		{

//		compact mode
			CoHack compak;
			if  (
			compak.hackIt(DATAFILE, name, compak.CO_MODE_DECRUNCH | compak.CO_MODE_FORMEM)
				) quitMessage("error","while decrunching file");
			if(fileSize == -1)	fileSize = compak.normalBuffer.size;
			return compak.normalBuffer.ptr;
		}
	}
};

