-
Notifications
You must be signed in to change notification settings - Fork 15
/
mpags-cipher.cpp
95 lines (82 loc) · 2.47 KB
/
mpags-cipher.cpp
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
// Standard Library includes
#include <iostream>
#include <fstream>
#include <string>
// Our project headers
#include "CaesarCipher.hpp"
#include "ProcessCommandLine.hpp"
#include "TransformChar.hpp"
//! Main function of the mpags-cipher program
int main(int argc, char* argv[]) {
// Command line inputs
bool helpRequested {false};
bool versionRequested {false};
std::string inputFile {""};
std::string outputFile {""};
std::string cipherKey {""};
bool cipherDecrypt {false};
bool commandLineParsed {processCommandLine(argc, argv, helpRequested, versionRequested, inputFile, outputFile, cipherKey, cipherDecrypt)};
if(!commandLineParsed) {
return 1;
}
// Handle help, if requested
if (helpRequested) {
doPrintCommandLineHelp();
// Help requires no further action, so return from main
// with 0 used to indicate success
return 0;
}
// Handle version, if requested. Like help, requires no further action,
// so return from main with zero to indicate success
if (versionRequested) {
doPrintCommandLineVersion();
return 0;
}
// Read in user input from stdin/file
char inputChar {'x'};
std::string inputText {""};
if(inputFile.empty()) {
// Loop over stdin until Return then CTRL-D pressed (EOF)
while (std::cin >> inputChar) {
inputText += transformChar(inputChar);
}
}
else {
// Create ifstream and loop over that until EOF
std::ifstream inFileStream {inputFile};
if (inFileStream.good()) {
while (inFileStream >> inputChar) {
inputText += transformChar(inputChar);
}
}
else {
std::cerr << "[error] bad input stream, cannot open/read file '"
<< inputFile
<<"'\n";
}
}
// TEXT ENCRYPTION/DECRYPTION
std::string outputText {""};
bool cryptOK {CaesarCipher(inputText, cipherKey, cipherDecrypt, outputText)};
if (!cryptOK) {
std::cerr << "[error] text processing with Caesar Cipher failed\n";
}
// Output the input text to stdout or file if filename supplied
if (outputFile.empty()) {
std::cout << outputText << "\n";
}
else {
std::ofstream outFileStream {outputFile};
if (outFileStream.good()) {
outFileStream << outputText << "\n";
}
else {
std::cerr << "[error] bad output stream, cannot open/write file '"
<< outputFile
<<"'\n";
}
}
// No requirement to return from main, but we do so for clarity and
// consistency with other functions.
return 0;
}