aboutsummaryrefslogtreecommitdiff
path: root/CST 126/Homework2/main.cpp
blob: 70ea25541c16351267b7e315cca062f833eeb987 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//Name:Reece Warner
//Date:4/24/24
//Assignment:Homework2
#include "Base64Conversion.hpp"

constexpr short ARG_COUNT = 4;

bool Worker();
bool Worker(char** argv);

int main(const int argc, char* argv[])
{
	if (argc == ARG_COUNT)
	{
		Worker(argv);
	}
	else
	{
		Worker();
	}

	return 0;
}

bool Worker()
{
	bool success = false;
	char* MyEncodedCharArray = nullptr;
	const char* MyFile = nullptr;
	const char* Destination = nullptr;
	char* buffer = nullptr;
	size_t size = 0;
	
	char Option = '\0';
	do
	{
		Prompts("!Welcome to the Base64 Encoding/Decoding Menu!\n\ne)Base64 Encode Your File\nd)Decode Your Encoded File\nf)Exit\n\n");
		
		std::cin >> Option;
		switch (Option)
		{
		case 'e':
			MyFile = InputFileName("What is the name of the file you wish to encode?: ");
			Destination = InputFileName("\nWhat is the name of the file that you wish to save the encoded information?: ");
			size = SizeOfFile(MyFile);
			buffer = new char[size];
			buffer = ReadFileAsBinary(MyFile, buffer, size);
			success = WriteTextToFile(Destination, Base64Encode(buffer, size));
			delete[] buffer;
			return true;
		case 'd':
			
			MyFile = InputFileName("What is the name of the file you wish to decode?: ");
			Destination = InputFileName("\nWhat is the name of the file you wish to save the decoded information?: ");
			size = SizeOfFile(MyFile);
			buffer = new char[size];
			ReadTextFromFile(MyFile, buffer);
			success = WriteFileFromBinary(Destination,buffer, Base64Decode(buffer, size));
			
			return true;
		case'f':
			std::cout << "Have a great day!" << std::endl;
			break;
		default:
			std::cerr << "Error, invalid command option\n" <<
				"Valid commands:\n" <<
				"\t -e source_file.exe destination_file.exe" <<
				"\tEncodes file in source to text in destination txt file.\n\n" <<
				"\t-d source_file.text destination_file.exe" <<
				"\tDecodes text in source file into the destination file.\n\n";
			return false;
		}
	} while (Option != 'f');
	
	return false;
}

bool Worker(char** argv)
{
	const char* arg1 = argv[1];
	const char* arg2 = argv[2];
	const char* arg3 = argv[3];
	char* buffer = nullptr;
	bool success = false;
	char* MyEncodedCharArray = nullptr;
	const size_t size = SizeOfFile(arg2);
	const char option = arg1[1];

	switch (option)
	{
	case 'e':
		buffer = new char[size];

		buffer = ReadFileAsBinary(arg2, buffer, size);

		success = WriteTextToFile(arg3, Base64Encode(buffer, size));
		delete[] buffer;

		return success;
	case 'd':
		buffer = new char[size];
		buffer = ReadTextFromFile(arg2, buffer);
		success = WriteFileFromBinary(arg3, buffer, Base64Decode(buffer, size));
		return true;
	default:
		std::cerr << "Error, invalid command option\n" <<
			"Valid commands:\n" <<
			"\t -e source_file.exe destination_file.exe" <<
			"\tEncodes file in source to text in destination txt file.\n\n" <<
			"\t-d source_file.text destination_file.exe" <<
			"\tDecodes text in source file into the destination file.\n\n";
		return false;
	}
}