aboutsummaryrefslogtreecommitdiff
path: root/CST 126/Homework2/program.cpp
blob: fb5e8c5ae661458ea16a3f468d06992f7cddbe7a (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Name: Wesley Richelderfer
// Class: CST 126
// Date: 4/27/24
// Assignment: Homework 2

#include <iostream>
#include <string>

#include "helpers.hpp"
#include "Base64Converter.hpp"

using std::string;

constexpr short ARG_COUNT = 4;

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


int main (const int argc, char* argv[])
{

	if(argc == ARG_COUNT)
	{
		//run command line version
		Worker(argv);
	}


	//console app version
	return Worker();
}

bool Worker()
{
	//files and choice
	char option;
	string destination;
	string source;

	//variables for switch statement
	bool success;
	vector<unsigned char> buffer;
	string product;
	size_t size = 0;

	cout << "Would you like to encode or decode? (e or d): ";
	cin >> option;
	cout << "\n\nWhat is the source file name? ";
	cin >> source;
	cout << "\n\nWhat is the destination file name? ";
	cin >> destination;

	const char* arg2 = source.c_str();
	const char* arg3 = destination.c_str();

	switch (option)
	{
	case 'e':

		buffer = ReadFileAsBinary(arg2, buffer);

		size = buffer.size();

		product = Base64Encode(&buffer[0], size);

		success = WriteTextToFile(arg3, product);

		return success;
		
	case 'd':

		ReadTextFromFile(arg2, product);

		buffer = Base64Decoder(product);

		success = WriteFileFromBinary(arg3, buffer);

		return success;
		
	default:
		std::cerr << "Invalid command option\n" <<
			"Valid commands:\n" <<
			"\t-e source_file.exe destination_file.exe" <<
			"\t\tEncodes file in source into text in destination txt file." <<
			"\t-d source_file.txt destination_file.exe" <<
			"\t\tDecodes text in source file into the destination file.\n\n";
		return false;
	}
}

bool Worker(char** argv)
{
	//files and choice
	const char* arg1 = argv[1];  //-e -d
	const char* arg2 = argv[2];  //source file name
	const char* arg3 = argv[3];  //destination file name

	const char option = arg1[1]; //e or d

	//variables for switch statement
	bool success;
	vector<unsigned char> buffer;
	string product;
	size_t size = 0;

	switch(option)
	{
	case 'e':
		
		buffer = ReadFileAsBinary(arg2, buffer);

		size = buffer.size();
		
		product = Base64Encode(&buffer[0], size);

		success = WriteTextToFile(arg3, product);

		return success;

	case 'd':

		ReadTextFromFile(arg2, product);

		buffer = Base64Decoder(product);

		success = WriteFileFromBinary(arg3, buffer);

		return success;

	default:

		std::cerr << "Invalid command option\n" <<
			"Valid commands:\n" <<
			"\t-e source_file.exe destination_file.exe" <<
			"\t\tEncodes file in source into text in destination txt file." <<
			"\t-d source_file.txt destination_file.exe" <<
			"\t\tDecodes text in source file into the destination file.\n\n";

		return false;

	}
}