/********************************************************************************************************************
Methods:
1. Append()		append the source file with data given
2. Copy()		copy the given source file to destination
3. Delete()		delete the given source file 
4. Move()		move the given source file to destination
5. Read()		read the given source file
6. Rename()		rename the given source file to destination
7. Write()		write the source file with data given

Properties:
1. PutAddNewLine()	if this property is set to TRUE, a new line character is appended to the text that is written
					to the file. If this attribute is set to FALSE, no new line character is appended to the text.
					valid values are
					TRUE or 1 (default)
					FALSE or 0
2. PutAttributes()	a comma-delimited list of file attributes to be set on the file being processed. valid values are
					Normal (default)
					ReadOnly
					Temporary
					Archive
					Hidden
					System
3. GetErrorReason() reports any errors that occur during the request
4. GetIsError()		returns 1 if any error occur during the request otherwise 0
********************************************************************************************************************/

/***********************************/
/*include all required header files*/
/***********************************/
#include <stdio.h>
#include <conio.h>

/************************************************************************/
/*import AdvDirectory COM library here to get its methods and properties*/
/************************************************************************/
#import "C:\AdvFile.dll" no_namespace

/*************************/
/*prototype for functions*/
/*************************/
void Append();//Append function
void Copy();//Copy function
void Delete();//Delete function
void Move();//Move function
void Read();//Read function
void Rename();//Rename function
void Write();//Write function

/**************/
/*main program*/
/**************/
void main(void)
{
	char Ch;
	do
	{
		printf("\r\nAppend	[A]");
		printf("\r\nCopy	[C]");
		printf("\r\nDelete	[D]");
		printf("\r\nMove	[M]");
		printf("\r\nRead	[R]");
		printf("\r\nRename	[N]");
		printf("\r\nWrite	[W]");
		printf("\r\nQuit	[Q]");
		printf("\r\n\r\nEnter Choice = ");
		scanf("%c", &Ch);
		getchar();
		Ch = toupper(Ch);
		switch (Ch)
		{
			case 'A':Append();break;//call Append function
			case 'C':Copy();break;//call Copy function
			case 'D':Delete();break;//call Delete function
			case 'M':Move();break;//call Move function
			case 'R':Read();break;//call Read function
			case 'N':Rename();break;//call Rename function
			case 'W':Write();break;//call Write function
			case 'Q':printf("\r\nGood Bye\r\n");break;
			default: printf("\r\nError: Please enter valid choice\r\n");
		}
	}
	while (Ch != 'Q');
}

/*******************************************************************************/
/*Append function: This function is used to append a given file with given data*/
/*******************************************************************************/
void Append()
{
	char m_Source[256], m_Data[256], m_Attributes[256];//define charecter buffer of 256 to read source file name, data and attributes
	long l_NewLine;//new line flag
	printf("\r\nEnter Source File Name = ");
	gets(m_Source);//read source file name
	printf("\r\nEnter File Data = ");
	gets(m_Data);//read file data
	printf("\r\nEnter New Line Flag [0/1] = ");
	scanf("%d", &l_NewLine);//read new line flag
	getchar();
	printf("\r\nEnter File Attributes = ");
	gets(m_Attributes);//read file attributes
	try//for COM library call, try...catch is used to handle exceptions
	{
		IFilesPtr obj;//take smart pointer object obj for COM library
		CoInitialize(NULL);//Initializes the COM library on the current thread
		obj = IFilesPtr("AdvFile.File");
		obj->PutAddNewLine(l_NewLine);//set add new line flag
		obj->PutAttributes(m_Attributes);//set attributes
		HRESULT hr = obj->Append(m_Source, m_Data);//call Append method of COM library
		if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
		{
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
			return;
		}
		//call GetIsError() property of COM library and check its value, if its value is 1, then
		//GetErrorReason() property of COM will contain error message, for which this error occured
		if (obj->GetIsError() == 1)
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
		else
			printf("\r\nSuccessfully Append\r\n");//display successfull message
	}
	catch (_com_error &error)//catch exception here
	{
		printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
	}
}

/*************************************************************************************/
/*Copy function: This function is used to copy file form given source to given target*/
/*************************************************************************************/
void Copy()
{
	char m_Source[256], m_Destination[256], m_Attributes[256];//define charecter buffer of 256 to read source file name, destination and attributes
	printf("\r\nEnter Source File Name = ");
	gets(m_Source);//read source file name
	printf("\r\nEnter Destination File Name = ");
	gets(m_Destination);//read destination file name
	printf("\r\nEnter File Attributes = ");
	gets(m_Attributes);//read file attributes
	try//for COM library call, try...catch is used to handle exceptions
	{
		IFilesPtr obj;//take smart pointer object obj for COM library
		CoInitialize(NULL);//Initializes the COM library on the current thread
		obj = IFilesPtr("AdvFile.File");
		obj->PutAttributes(m_Attributes);//set attributes
		HRESULT hr = obj->Copy(m_Source, m_Destination);//call Copy method of COM library
		if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
		{
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
			return;
		}
		//call GetIsError() property of COM library and check its value, if its value is 1, then
		//GetErrorReason() property of COM will contain error message, for which this error occured
		if (obj->GetIsError() == 1)
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
		else
			printf("\r\nSuccessfully Copied\r\n");//display successfull message
	}
	catch (_com_error &error)//catch exception here
	{
		printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
	}
}

/*************************************************************************/
/*Delete function: This function is used to delete file with given source*/
/*************************************************************************/
void Delete()
{
	char m_Source[256];//define charecter buffer of 256 to read source file name
	printf("\r\nEnter Source File Name = ");
	gets(m_Source);//read source file name
	try//for COM library call, try...catch is used to handle exceptions
	{
		IFilesPtr obj;//take smart pointer object obj for COM library
		CoInitialize(NULL);//Initializes the COM library on the current thread
		obj = IFilesPtr("AdvFile.File");
		HRESULT hr = obj->Delete(m_Source);//call Delete method of COM library
		if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
		{
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
			return;
		}
		//call GetIsError() property of COM library and check its value, if its value is 1, then
		//GetErrorReason() property of COM will contain error message, for which this error occured
		if (obj->GetIsError() == 1)
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
		else
			printf("\r\nSuccessfully Deleted\r\n");//display successfull message
	}
	catch (_com_error &error)//catch exception here
	{
		printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
	}
}

/*************************************************************************************/
/*Move function: This function is used to move file form given source to given target*/
/*************************************************************************************/
void Move()
{
	char m_Source[256], m_Destination[256], m_Attributes[256];//define charecter buffer of 256 to read source file name, destination and attributes
	printf("\r\nEnter Source File Name = ");
	gets(m_Source);//read source file name
	printf("\r\nEnter Destination File Name = ");
	gets(m_Destination);//read destination file name
	printf("\r\nEnter File Attributes = ");
	gets(m_Attributes);//read file attributes
	try//for COM library call, try...catch is used to handle exceptions
	{
		IFilesPtr obj;//take smart pointer object obj for COM library
		CoInitialize(NULL);//Initializes the COM library on the current thread
		obj = IFilesPtr("AdvFile.File");
		obj->PutAttributes(m_Attributes);//set attributes
		HRESULT hr = obj->Move(m_Source, m_Destination);//call Move method of COM library
		if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
		{
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
			return;
		}
		//call GetIsError() property of COM library and check its value, if its value is 1, then
		//GetErrorReason() property of COM will contain error message, for which this error occured
		if (obj->GetIsError() == 1)
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
		else
			printf("\r\nSuccessfully Moved\r\n");//display successfull message
	}
	catch (_com_error &error)//catch exception here
	{
		printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
	}
}

/*************************************************************************************/
/*Read function: This function is used to read file form given source to given target*/
/*************************************************************************************/
void Read()
{
	char m_Source[256];//define charecter buffer of 256 to read source file name
	printf("\r\nEnter Source File Name = ");
	gets(m_Source);//read source file name
	try//for COM library call, try...catch is used to handle exceptions
	{
		IFilesPtr obj;//take smart pointer object obj for COM library
		CoInitialize(NULL);//Initializes the COM library on the current thread
		obj = IFilesPtr("AdvFile.File");
		IDispatch *p = obj->Read(m_Source);//call Read method of COM library
		//call GetIsError() property of COM library and check its value, if its value is 1, then
		//GetErrorReason() property of COM will contain error message, for which this error occured
		if (obj->GetIsError() == 1)
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
		else
		{
			printf("\r\nSuccessfully Read\r\n");//display successfull message
			IReadPtr readobj;
			p->QueryInterface(IID_IDispatch,(void**)&readobj);//call query interface
			printf("\r\nData = %s\r\n", (char*)readobj->GetFileContent());
		}
	}
	catch (_com_error &error)//catch exception here
	{
		printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
	}
}

/*****************************************************************************************/
/*Rename function: This function is used to rename file with given source to given target*/
/*****************************************************************************************/
void Rename()
{
	char m_Source[256], m_Destination[256], m_Attributes[256];//define charecter buffer of 256 to read source file name, destination and attributes
	printf("\r\nEnter Source File Name = ");
	gets(m_Source);//read source file name
	printf("\r\nEnter Destination File Name = ");
	gets(m_Destination);//read destination file name
	printf("\r\nEnter File Attributes = ");
	gets(m_Attributes);//read file attributes
	try//for COM library call, try...catch is used to handle exceptions
	{
		IFilesPtr obj;//take smart pointer object obj for COM library
		CoInitialize(NULL);//Initializes the COM library on the current thread
		obj = IFilesPtr("AdvFile.File");
		obj->PutAttributes(m_Attributes);//set attributes
		HRESULT hr = obj->Rename(m_Source, m_Destination);//call Rename method of COM library
		if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
		{
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
			return;
		}
		//call GetIsError() property of COM library and check its value, if its value is 1, then
		//GetErrorReason() property of COM will contain error message, for which this error occured
		if (obj->GetIsError() == 1)
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
		else
			printf("\r\nSuccessfully Renamed\r\n");//display successfull message
	}
	catch (_com_error &error)//catch exception here
	{
		printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
	}
}

/*****************************************************************************/
/*Write function: This function is used to write a given file with given data*/
/*****************************************************************************/
void Write()
{
	char m_Source[256], m_Data[256], m_Attributes[256];//define charecter buffer of 256 to read source file name, data and attributes
	long l_NewLine;//new line flag
	printf("\r\nEnter Source File Name = ");
	gets(m_Source);//read source file name
	printf("\r\nEnter File Data = ");
	gets(m_Data);//read file data
	printf("\r\nEnter New Line Flag [0/1] = ");
	scanf("%d", &l_NewLine);//read new line flag
	getchar();
	printf("\r\nEnter File Attributes = ");
	gets(m_Attributes);//read file attributes
	try//for COM library call, try...catch is used to handle exceptions
	{
		IFilesPtr obj;//take smart pointer object obj for COM library
		CoInitialize(NULL);//Initializes the COM library on the current thread
		obj = IFilesPtr("AdvFile.File");
		obj->PutAddNewLine(l_NewLine);//set add new line flag
		obj->PutAttributes(m_Attributes);//set attributes
		HRESULT hr = obj->Write(m_Source, m_Data);//call Write method of COM library
		if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
		{
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
			return;
		}
		//call GetIsError() property of COM library and check its value, if its value is 1, then
		//GetErrorReason() property of COM will contain error message, for which this error occured
		if (obj->GetIsError() == 1)
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
		else
			printf("\r\nSuccessfully Write\r\n");//display successfull message
	}
	catch (_com_error &error)//catch exception here
	{
		printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
	}
}
