-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargumentUI.cpp
More file actions
98 lines (82 loc) · 3.57 KB
/
Copy pathargumentUI.cpp
File metadata and controls
98 lines (82 loc) · 3.57 KB
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
#include "argumentUI.hpp"
#include <exception>
#include <istream>
#include <sstream>
#include <stdexcept>
#include <string>
#include "cxxopts.hpp"
#include <fstream>
void processStream(Enigma &en, std::istream &inStream, std::ostream &outStream) {
std::stringstream buffer;
buffer << inStream.rdbuf();
std::string input = buffer.str();
std::string output = en.encodeText(input);
outStream << output;
}
std::vector<std::string> parseRotors(std::string rotors) {
std::vector<std::string> result;
std::string singleRotor;
std::istringstream iss(rotors);
while(iss >> singleRotor) {
result.push_back(singleRotor);
}
return result;
}
void argumentMode(int argc, char* argv[], bool stdinIsTty) {
cxxopts::Options options("Enigma-Machine", "CLI Simulator for Enigma encryption machine used in WW2.");
options.add_options()
("f,file", "Input file name", cxxopts::value<std::string>())
("o,output", "Output file name", cxxopts::value<std::string>())
("rotors", "Rotors to use in order (ex: \"I II III\")", cxxopts::value<std::string>()->default_value("I II III"))
("p,positions", "Initial position of rotors", cxxopts::value<std::string>()->default_value("AAA"))
("r,reflector", "Used reflector", cxxopts::value<std::string>()->default_value("B"))
("plugboard", "Plugboard connections", cxxopts::value<std::string>()->default_value(""))
("h,help", "Print usage")
;
try {
auto result = options.parse(argc, argv);
if (result.count("help")) {
std::cout << options.help() << std::endl;
exit(0);
}
EnigmaConfig config;
config.rotorNames = parseRotors(result["rotors"].as<std::string>());
config.rotorInitial = result["positions"].as<std::string>();
config.reflector = result["reflector"].as<std::string>();
config.plugboardConnections = result["plugboard"].as<std::string>();
Enigma en(config);
// has input validation
bool has_file_input = result.count("file");
bool stdin_is_pipe = !stdinIsTty;
if (!has_file_input && !stdin_is_pipe) {
std::cerr << "ERROR: Input text not found." << std::endl;
std::cerr << "Use -f <file> or give input values by pipe (ex: echo '...' | ./enigma_machine ...)" << std::endl;
std::cerr << "\n" << options.help() << std::endl;
exit(1);
}
// input open file
std::ifstream inFile;
if(result.count("file")) {
inFile.open(result["file"].as<std::string>());
if(!inFile.is_open()) {
throw std::runtime_error("Could not open input file '" + result["file"].as<std::string>() + "'");
}
}
std::istream& inStream = result.count("file") ? inFile : std::cin;
// output open file
std::ofstream outFile;
if(result.count("output")) {
outFile.open(result["output"].as<std::string>());
if(!outFile.is_open()) {
throw std::runtime_error("Could not open output file '" + result["file"].as<std::string>() + "'");
}
}
std::ostream& outStream = result.count("output") ? outFile : std::cout;
processStream(en, inStream, outStream);
} catch (const cxxopts::exceptions::exception& e) {
std::cerr << "ARGUMENT ERROR: " << e.what() << std::endl;
std::cerr << "Use -h or --help to see all valid options." << std::endl;
} catch (const std::exception& e) {
std::cerr << "Caught exception: " << e.what() << std::endl;
}
}