//To be compiled using VS 2013

#include <iostream>
#include <conio.h>
#include <fstream>
#include <windows.h>
#include <string>
using namespace std;

void createFile(string path) {
	string fullpath = path;
	ofstream d(path);
	if (d.good() == true)
	{
		d.close();
		MessageBox(NULL, L"Action done successfully!", L"Done!", MB_OK | MB_ICONINFORMATION);
	}
	else MessageBox(NULL, L"Error while creating file!", NULL, MB_OK | MB_ICONSTOP);
}

void deleteFile(string path) {
	const char* fullpathc = path.c_str();
	if (remove(fullpathc) != 0)
	{
		MessageBox(NULL, L"Error while deleting file!", NULL, MB_OK | MB_ICONSTOP);
	}
	else
	{
		MessageBox(NULL, L"File deleted successfully!", L"Done!", MB_OK | MB_ICONINFORMATION);
	}
}

void writeToFile(string path, string toWrite, bool isNew)
{
	const char* dane = toWrite.c_str();
	ofstream d;
	d.open(path, ofstream::out | ofstream::app);
	if (d.good() == true)
	{
		d.seekp(0, d.end);
		if (isNew == true)
		{
			d.write("\n", 1);
		}
		d.write(dane, toWrite.length());
		d.close();
		MessageBox(NULL, L"Action done successfully!", L"Done!", MB_OK | MB_ICONINFORMATION);
	}
	else MessageBox(NULL, L"Error while accessing file!", NULL, MB_OK | MB_ICONSTOP);
}

void printFile(string path)
{
	ifstream d;
	string dane = "";
	d.open(path, ifstream::in);
	if (d.good() == true)
	{
		while (d.eof() == false) {
			getline(d, dane);
			cout << dane << endl;
		}
		d.close();
		_getch();
	}
	else MessageBox(NULL, L"Error while accessing file!", NULL, MB_OK | MB_ICONSTOP);
}

void renameFile(string pathsz, string newnames)
{
	size_t found = pathsz.find_last_of("\\");
	const char* path = pathsz.c_str();
	newnames.insert(0, pathsz, 0, found + 1);
	newnames = newnames;
	const char* newname = newnames.c_str();
	int result = rename(path, newname);
	if (result == 0)
	{
		MessageBox(NULL, L"Action done successfully!", L"Done!", MB_OK | MB_ICONINFORMATION);
	}
	else MessageBox(NULL, L"Error while renaming file!", NULL, MB_OK | MB_ICONSTOP);
}

int main()
{
	unsigned char wybor;
	string buf1;
	string buf3;
	int isNew;
	bool isNewB;
	do
	{
		system("cls");
		cin.clear();
		cin.sync();
		buf1 = "";
		buf3 = "";
		isNew = 0;
		isNewB = false;
		cout << "File managing. Press:\n1 - create a file\n2 - delete a file\n3 - write to an existent file\n4 - read from a file\n5 - rename a file\n0 - quit\nChoose an option: ";
		wybor = _getch();
		system("cls");
		switch (wybor)
		{
		case '1':
		{
					cout << "Give your precious file a path: ";
					getline(cin, buf1);
					createFile(buf1);
		}
			break;

		case '2':
		{
					cout << "Path of file to delete: ";
					getline(cin, buf1);
					deleteFile(buf1);
		}
			break;

		case '3':
		{
					cout << "Path of file to write to: ";
					getline(cin, buf1);
					cout << "Data to write to file: ";
					getline(cin, buf3);
					cout << "Do you wish to write the data in a new line?\n0 is no, 1 is yes: ";
					while (!(cin >> isNew)) {
						cout << "Incorrect data!";
						cin.clear();
						cin.sync();
					}
					if (isNew != 0)
					{
						isNewB = true;
					}
					writeToFile(buf1, buf3, isNewB);
		}
			break;
		case '4':
		{
					cout << "Path of file to read from: ";
					getline(cin, buf1);
					printFile(buf1);
		}
			break;
		case '5':
		{
					cout << "Path of file to rename: ";
					getline(cin, buf1);
					cout << "New name: ";
					getline(cin, buf3);
					renameFile(buf1, buf3);
		}
			break;
		}
	} while (wybor != '0');
	return 0;
}