diff options
Diffstat (limited to 'CST 126')
| -rw-r--r-- | CST 126/Homework 2/0 | 1 | ||||
| -rw-r--r-- | CST 126/Homework 2/1 | 1 | ||||
| -rw-r--r-- | CST 126/Homework 2/Homework 2.vcxproj | 4 | ||||
| -rw-r--r-- | CST 126/Homework 2/Homework 2.vcxproj.filters | 4 | ||||
| -rw-r--r-- | CST 126/Homework 2/encode.txt | 1 | ||||
| -rw-r--r-- | CST 126/Homework 2/input.txt (renamed from CST 126/Homework 2/decode.txt) | 0 | ||||
| -rw-r--r-- | CST 126/Homework 2/main.cpp | 180 | ||||
| -rw-r--r-- | CST 126/Homework 2/output.txt | 1 |
8 files changed, 125 insertions, 67 deletions
diff --git a/CST 126/Homework 2/0 b/CST 126/Homework 2/0 new file mode 100644 index 0000000..580c20e --- /dev/null +++ b/CST 126/Homework 2/0 @@ -0,0 +1 @@ +aGk=
\ No newline at end of file diff --git a/CST 126/Homework 2/1 b/CST 126/Homework 2/1 new file mode 100644 index 0000000..580c20e --- /dev/null +++ b/CST 126/Homework 2/1 @@ -0,0 +1 @@ +aGk=
\ No newline at end of file diff --git a/CST 126/Homework 2/Homework 2.vcxproj b/CST 126/Homework 2/Homework 2.vcxproj index c53ce28..1653071 100644 --- a/CST 126/Homework 2/Homework 2.vcxproj +++ b/CST 126/Homework 2/Homework 2.vcxproj @@ -130,8 +130,8 @@ <ClCompile Include="main.cpp" /> </ItemGroup> <ItemGroup> - <Text Include="decode.txt" /> - <Text Include="encode.txt" /> + <Text Include="input.txt" /> + <Text Include="output.txt" /> </ItemGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets"> diff --git a/CST 126/Homework 2/Homework 2.vcxproj.filters b/CST 126/Homework 2/Homework 2.vcxproj.filters index 5ae23c5..789f73e 100644 --- a/CST 126/Homework 2/Homework 2.vcxproj.filters +++ b/CST 126/Homework 2/Homework 2.vcxproj.filters @@ -20,7 +20,7 @@ </ClCompile> </ItemGroup> <ItemGroup> - <Text Include="encode.txt" /> - <Text Include="decode.txt" /> + <Text Include="output.txt" /> + <Text Include="input.txt" /> </ItemGroup> </Project>
\ No newline at end of file diff --git a/CST 126/Homework 2/encode.txt b/CST 126/Homework 2/encode.txt deleted file mode 100644 index 32f95c0..0000000 --- a/CST 126/Homework 2/encode.txt +++ /dev/null @@ -1 +0,0 @@ -hi
\ No newline at end of file diff --git a/CST 126/Homework 2/decode.txt b/CST 126/Homework 2/input.txt index 32f95c0..32f95c0 100644 --- a/CST 126/Homework 2/decode.txt +++ b/CST 126/Homework 2/input.txt diff --git a/CST 126/Homework 2/main.cpp b/CST 126/Homework 2/main.cpp index 92a5a59..c5eed33 100644 --- a/CST 126/Homework 2/main.cpp +++ b/CST 126/Homework 2/main.cpp @@ -9,69 +9,118 @@ using namespace std; -string encodeFileToBase64(const string& filename); +string encodeFileToBase64(const string& input, const string& output); string decodeBase64(const string& encoded); -bool decodeBase64ToFile(const string& base64Data, const string& filename); +bool decodeBase64ToFile(const string& input, const string& output); -int main() +constexpr short ARG_COUNT = 4; + +//bool Worker(); +bool Worker(char** argv); + +int main(const int argc, char* argv[]) { - int choice = 0; - do { - cout << "Welcome to my program! Here are your options:" << endl; - cout << "0. Quit" << endl; - cout << "1. Encode File to Base64" << endl; - cout << "2. Decode Base64 to File" << endl; - cin >> choice; - switch (choice) { - case 1: - { - string filename; - cout << "Enter input filename: "; - std::cin.clear(); - std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); - getline(cin, filename); - string base64Data = encodeFileToBase64(filename); - cout << "Base64 Encoded Data:\n" << base64Data << endl; - break; - } - case 2: - { - string base64Data; - cout << "Enter Base64 encoded data: "; - cin >> base64Data; - - string filename; - cout << "Enter output filename: "; - std::cin.clear(); - std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); - getline(cin, filename); - - if (decodeBase64ToFile(base64Data, filename)) { - cout << "File successfully decoded and written: " << filename << endl; + if (argc == ARG_COUNT) { + Worker(argv); + } + else { + int choice = 0; + do { + cout << "Welcome to my program! Here are your options:" << endl; + cout << "0. Quit" << endl; + cout << "1. Encode File to Base64" << endl; + cout << "2. Decode Base64 to File" << endl; + cin >> choice; + switch (choice) { + case 1: + { + string input; + string output; + cout << "Enter input filename: "; + std::cin.clear(); + std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); + getline(cin, input); + cout << "Enter output filename: "; + getline(cin, output); + string base64Data = encodeFileToBase64(input, output); + cout << "Base64 Encoded Data:\n" << base64Data << endl; + break; } - else { - cout << "Failed to decode and write file." << endl; + case 2: + { + string input; + cout << "Enter input filename: "; + std::cin.clear(); + std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); + getline(cin, input); + + string output; + cout << "Enter output filename: "; + getline(cin, output); + + if (decodeBase64ToFile(input, output)) { + cout << "File successfully decoded and written: " << output << endl; + } + else { + cerr << "Failed to decode and write file." << endl; + } + break; } - break; - } - } - } while (choice != 0); + } + } while (choice != 0); + } + //return Worker(); } -string encodeFileToBase64(const string& filename) { - ifstream file(filename, ios::binary | ios::ate); - if (!file.is_open()) { - cout << "Error: Unable to open file " << filename << endl; +/*bool Worker() { + char option = 'e'; + + switch (option) { + case 'e': + return true; + case 'd': + return true; + default: + return false; + } +}*/ +bool Worker(char** argv) { + const char* arg1 = argv[1]; + const char* arg2 = argv[2]; + const char* arg3 = argv[3]; + + const char option = arg1[1]; + + string str2 = string("") + arg2; + string str3 = string("") + arg3; + + switch (option) { + case 'e': + encodeFileToBase64(str2, str3); + return true; + case 'd': + decodeBase64ToFile(str2, str3); + return true; + default: + cerr << "Invalid command option\n" << "Valid Commands:\n" << "\t-e source_file.exe destination_file.txt\n" << "\t\tEncodes file in source into text in destination txt file.\n" << "\t-d source_file.txt destination_file.exe\n" << "\t\tDecodes text in source file into the destination file.\n\n"; + return false; + } +} + +string encodeFileToBase64(const string& input, const string& output) { + ifstream fileInput(input, ios::binary | ios::ate); + if (!fileInput.is_open()) { + cerr << "Error: Unable to open file " << input << endl; return ""; } // Get file size - streamsize fileSize = file.tellg(); - file.seekg(0, ios::beg); + streamsize fileSize = fileInput.tellg(); + fileInput.seekg(0, ios::beg); // Read file into buffer char* buffer = new char[fileSize]; - file.read(buffer, fileSize); + fileInput.read(buffer, fileSize); // Base64 encoding table const string base64_chars = @@ -100,16 +149,18 @@ string encodeFileToBase64(const string& filename) { encoded << '='; } - /*// Calculate padding - int padding = (3 - (fileSize % 3)) % 3; + delete[] buffer; + fileInput.close(); - // Add padding with '=' - while (padding--) { - encoded << '='; - }*/ + ofstream fileOutput(output); - delete[] buffer; - file.close(); + if (!fileOutput.is_open()) { + cerr << "Error: Unable to open file " << input << endl; + return ""; + } + + fileOutput << encoded.str(); + fileOutput.close(); return encoded.str(); } @@ -126,7 +177,7 @@ string decodeBase64(const string& encoded) { for (char c : encoded) { if (c == '=') break; if (base64_chars.find(c) == string::npos) { - cout << "Error: Invalid Base64 character detected!" << endl; + cerr << "Error: Invalid Base64 character detected!" << endl; return ""; } val = (val << 6) + base64_chars.find(c); @@ -140,16 +191,21 @@ string decodeBase64(const string& encoded) { } -bool decodeBase64ToFile(const string& base64Data, const string& filename) { - ofstream file(filename, ios::binary); +bool decodeBase64ToFile(const string& input, const string& output) { + string base64Data; + ifstream inputfile(input); + inputfile >> base64Data; + inputfile.close(); + + ofstream file(output, ios::binary); if (!file.is_open()) { - cout << "Error: Unable to create file " << filename << endl; + cerr << "Error: Unable to create file " << output << endl; return false; } string decodedData = decodeBase64(base64Data); if (decodedData.empty()) { - cout << "Error: Failed to decode Base64 data" << endl; + cerr << "Error: Failed to decode Base64 data" << endl; return false; } diff --git a/CST 126/Homework 2/output.txt b/CST 126/Homework 2/output.txt new file mode 100644 index 0000000..580c20e --- /dev/null +++ b/CST 126/Homework 2/output.txt @@ -0,0 +1 @@ +aGk=
\ No newline at end of file |