-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
67 lines (61 loc) · 1.42 KB
/
main.cpp
File metadata and controls
67 lines (61 loc) · 1.42 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
/*
** SIDE PROJECT, 2021
** cipher
** File description:
** main
*/
#include <unistd.h>
#include <iostream>
#include <string>
#include <fstream>
void print_help()
{
std::cout << "Usage:\n\t./cipher [textToEncrypt] [-f 'pathToFileToEncrypt'] [-n 'sizeOfShift']" << std::endl;
}
std::string caesar_cipher(std::string *text, int shift)
{
for (size_t i = 0; i < text->length(); i++)
text->at(i) += shift;
return *text;
}
std::string readFile(std::string filename)
{
std::ifstream input(filename, std::ios_base::in);
std::string buffer;
std::string content;
while (std::getline(input, buffer))
content += buffer;
return content;
}
int main(int ac, char **av)
{
int c = 0;
std::string text, filename, remaining;
bool fileMode = false;
int shift = 3;
while ((c = getopt(ac, av, "f:n:h")) != -1) {
switch (c) {
case 'f':
if (optarg) {
filename = optarg;
fileMode = true;
}
break;
case 'n':
if (optarg)
shift = atoi(optarg);
break;
case 'h':
print_help();
break;
}
}
if (optind != ac)
text = av[optind];
if (fileMode) {
text = readFile(filename);
}
caesar_cipher(&text, shift);
std::cout << text << std::endl;
return 0;
}