-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpags-cipher.cpp
128 lines (108 loc) · 3.52 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
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
// Standard Library includes
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
// For std::isalpha and std::isupper
#include <cctype>
//Include the headers for the separate functions
#include "TransformChar.hpp"
#include "processCommandLine.hpp"
#include "runCaesarCipher.hpp"
// Main function of the mpags-cipher program
int main(int argc, char* argv[])
{
// Convert the command-line arguments into a more easily usable form
const std::vector<std::string> args {argv, argv+argc};
// Options that might be set by the command-line arguments
bool helpRequested {false};
bool versionRequested {false};
bool encrypt {true};
std::string inputFileName {""};
std::string outputFileName {""};
// Initialize key
size_t key{0};
//Call process command line function and return true or false.
bool allok { processCommandLine(args, helpRequested, versionRequested, encrypt, inputFileName, outputFileName, key) };
//If fails
if(allok == false)
{
std::cout << "Command line error" << std::endl;
return 1;
}
// Handle help, if requested
if (helpRequested)
{
// Line splitting for readability
std::cout
<< "Usage: mpags-cipher [-i <file>] [-o <file>]\n\n"
<< "Encrypts/Decrypts input alphanumeric text using classical ciphers\n\n"
<< "Available options:\n\n"
<< " -h|--help Print this help message and exit\n\n"
<< " --version Print version information\n\n"
<< " -i FILE Read text to be processed from FILE\n"
<< " Stdin will be used if not supplied\n\n"
<< " -o FILE Write processed text to FILE\n"
<< " Stdout will be used if not supplied\n\n"
<< " -encrypt Activate encryption mode (the default)\n\n"
<< " -decrypt Activate decryption mode\n";
// 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)
{
std::cout << "0.2.0" << std::endl;
return 0;
}
// Initialise variables for processing input text
char inputChar {'x'};
std::string inputText {""};
// Read in user input from stdin/file if file is NOT empty
if (!inputFileName.empty())
{
std::ifstream in_file {inputFileName};
//Check if file ok to read from
bool ok_to_read = in_file.good();
if(ok_to_read)
{
while(in_file >> inputChar)
{
inputText += transformChar(inputChar);
}
}
}
// Loop over each character from user input if no input file exists
// (until Return then CTRL-D (EOF) pressed)
else
{
while(std::cin >> inputChar)
{
inputText += transformChar(inputChar);
}
}
//Define final output string "run" and set it to the result of the Caesar function
std::string run { runCaesarCipher(inputText, key, encrypt) };
// Check that file is opened and if so write result to file
if (!outputFileName.empty())
{
std::ofstream out_file {outputFileName};
bool ok_to_write = out_file.good();
if(ok_to_write)
{
out_file << run << std::endl;
}
}
else
{
//Print result
//std::cout << inputText << std::endl;
std::cout << run << std::endl;
}
// No requirement to return from main, but we do so for clarity
// and for consistency with other functions
return 0;
}