
#include <iostream>
#include <fstream>
#include <direct.h>
using namespace std;

// *********Function Declarations Start***********
void loadFile();
void saveFile();
void removeFile();
void fileMenu();

void filePathToBuf();
void setDrive();
void setDir();
void makeDir();
void rmDir();
//*********Function Declarations End***********

const int p_size = 100;		// Global variable
char pathBuffer[p_size];	// Global variable

void makeDir()
{
	if (! _mkdir(pathBuffer))
	{
		cerr << "Unable to create directory" << endl;
	}
}

void rmDir()
{
	if (! _rmdir(pathBuffer))
	{
		cerr << "Unable to remove directory" << endl;
	}
}

// Function sets current drive START
void setDrive()
{
	int drive = 0;
	int count_ch = 0;

	if (pathBuffer[0] == 'C' || pathBuffer[0] == 'c')
		drive = 2;
	else if (pathBuffer[0] == 'D' || pathBuffer[0] == 'd')
		drive = 3;
	else if (pathBuffer[0] == 'D' || pathBuffer[0] == 'd')
		drive = 4;
	else { cerr << "Cant find currend hard-drive" << endl; }
	
	// eliminates the need to add a \ after the c: START
	for (int i = 0;pathBuffer[i] != '\0';i++)
	{
		count_ch++;
	}
	cout << "***************************" << endl;
	cout << "count_ch is: " << count_ch << endl;
	cout << "***************************" << endl;
	if (count_ch == 2)
		pathBuffer[2] = '\\';	// adds a \ to the 3:rd field in the array
	// eliminates the need to add a \ after the c: END

	if (! _chdrive( drive ))
	{ cerr << "Unable to set current drive to " << pathBuffer[0] << ':' << '\\' << endl; }
}
// Function sets current drive END

// Function sets file path START
void setDir()
{
	if ( _chdir(pathBuffer) == -1)
	{ cerr << "Unable to set current file path" << endl; }
}
// Function sets file path END

// Function puts the filr path in PathBuffer START
void filePathToBuf()
{
	cin.get();
	cout << flush;
	// The filepath is applied to pathBuffer[],by user input
	cin.getline(pathBuffer,(p_size-1),'\n');
	cout << flush;
}
// Function puts the filr path in PathBuffer END

// Function for loading a file START
void loadFile()
{
	const int s_length = 100;
	const int b_size = 10000;
	char fileName[s_length];
	char buffer[b_size];

	cout << "------------------------------------------" << endl;
	cout << "Type the path to the file you want to load" << endl;
	cout << "------------------------------------------" << endl << endl;
	cout << "Path:" << flush;

	filePathToBuf();	// applies the file path to the pathBuffer[]
	setDir();			// setting the filepath
	setDrive();			// setting current drive

	cout << "Print a name for the file to load" << endl;
	cout << "Filename:" << flush;
	
	cin.getline(fileName,(s_length-1),'\n');
	ifstream input(fileName,ios::in);

	if (!input.is_open())
	{ cerr << "Unable to open the file " << fileName << endl << endl; }
	else
	{

		while (!input.eof())
		{
			input.getline(buffer,(b_size - 1));
			cout << buffer << endl;
		}
	}
	input.close();
	
}
// Function for loading a file END

// Function for saving a file START
void saveFile()
{
	const int s_length = 100;
	const int t_size = 1020;
	char fileName[s_length];
	char textBuffer[t_size];
	// sets the value of where rowBreak will occure, in the file
	const int rowBreak[34] = {29,59,89,119,149,179,209,239,269,299,329,359,389,419,449,479,509,
							  539,569,599,629,659,689,719,749,779,809,839,869,899,929,959,989,1019};
	int i = 0,j = 0;

	cout << "------------------------------------------------------" << endl;
	cout << "Type the path to where you want the file to be created" << endl;
	cout << "------------------------------------------------------" << endl << endl;
	cout << "Path:" << flush;

	filePathToBuf();	// applies the file path to the pathBuffer[]
	setDrive();			// setting current drive
	setDir();
	makeDir();			// creating directory

	textBuffer[0] = '0';

	cout << "Print a name for the file to save" << endl;
	cout << "Filename:" << flush;
	cin.getline(fileName,(s_length - 1),'\n');

	cout << "*********************************************" << endl;
	cout << "Print some text for the file (Max 1000 chars)" << endl;
	cout << "*********************************************" << endl;
	cin.getline(textBuffer,(t_size - 1));

	ofstream output(fileName,ios::out);

	if (output.is_open())
	{
		for (i = 0;i < (t_size-1) && textBuffer[i] != '\0';i++)
		{
			output << textBuffer[i];
			if (i == (rowBreak[j]))
			{
				output << endl;
				j++;
			}
		}
	}
	output.close();
	system("cls");
	cout << "-----------------------------------------------------" << endl;
	cout << "The file: " << fileName << " was successfully created" << endl;
	cout << "-----------------------------------------------------" << endl << endl;
	cout << "The file is located in: " << pathBuffer << endl << endl;

}
// Function for saving a file END

// Function for removing a file START
void removeFile()
{
	const int s_length = 100;
	char fileName[100];
	
	cout << "--------------------------------------------" << endl;
	cout << "Type the path to the file you want to remove" << endl;
	cout << "--------------------------------------------" << endl << endl;
	cout << "Path:" << flush;

	filePathToBuf();	// applies the file path to the pathBuffer[]
	setDrive();	// setting current drive
	setDir();	// setting the filepath
	

	cout << "Print a name for the file to remove" << endl;
	cout << "Filename:" << flush;
	
	cin.getline(fileName,(s_length-1),'\n');

	if (remove(fileName) == -1)
	{ 
		cout << "---------------------------------------------------------------------------" << endl;
		cerr << "Unable to remove the file, or there is no file with the name " << fileName << endl; 
		cout << "---------------------------------------------------------------------------" << endl << endl;
	}
	else
	{
		cout << "-----------------------------------------------------" << endl;
		cout << "The file: " << fileName << " was successfully removed" << endl;
		cout << "-----------------------------------------------------" << endl << endl;
	}
	rmDir(); // removes the file directory
}
// Function for removing a file END

void fileMenu()
{
	char menuChoice;
	
	while(menuChoice != '4')
	{
		cout << "* * * * * * * * * * * * * * * * * * * * * * * *" << endl;
		cout << "This is a File stream Test, for recall purpose." << endl;
		cout << "* * * * * * * * * * * * * * * * * * * * * * * *" << endl << endl;
		
		cout << "What do you want to do?" << endl;
		cout << "1.Create a file" << endl;
		cout << "2.Load a file" << endl;
		cout << "3.Remove a file" << endl;
		cout << "4.Exit program" << endl;
		cin >> menuChoice;

		switch (menuChoice)
		{
			case '1':
				system("cls");
				saveFile();
				break;

			case '2':
				system("cls");
				loadFile();
				break;

			case '3':
				system("cls");
				removeFile();
				break;

			case '4':
				system("cls");
				cout << "Good-Bye!" << endl;
				break;

			default:
				break;
		}
	}

}

int main()
{
	fileMenu();
	return 0;
}