diff options
Diffstat (limited to 'CST 126/Homework2/program.cpp')
| -rw-r--r-- | CST 126/Homework2/program.cpp | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/CST 126/Homework2/program.cpp b/CST 126/Homework2/program.cpp index e69de29..fb5e8c5 100644 --- a/CST 126/Homework2/program.cpp +++ b/CST 126/Homework2/program.cpp @@ -0,0 +1,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; + + } +}
\ No newline at end of file |