forked from cassiopeia315/EncodedCPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
255 lines (208 loc) · 5.65 KB
/
Copy pathmain.cpp
File metadata and controls
255 lines (208 loc) · 5.65 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <iostream>
#include <random>
#include <fstream>
#include <iomanip>
#include <string>
#include "headers/json.hpp"
using namespace std;
using json = nlohmann::json;
class alphabet{
public:
char baseAlphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char legend[2][26] = {{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'},
{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}};
void allVariables(){
for (int i = 0; i <= 25; ++i){
randomInator(legend[1][i]);
}
}
void randomInator(char& letter){
random_device randInat;
unsigned int randLetter;
do {
randLetter = (randInat() % 26);
} while (baseAlphabet[randLetter] == '#');
letter = baseAlphabet[randLetter];
baseAlphabet[randLetter] = '#';
}
void encode(const string& message, string& encodedMessage){
for (char i : message) {
if (isspace(i)){
encodedMessage += ' ';
}
else {
encodedMessage += legend[1][int(i) - 97];
}
}
}
void decode(string encodedMessage, string& message){
for (int i = 0; i <= encodedMessage.length(); ++i) {
if (isspace(encodedMessage[i])){
message += ' ';
}
else {
for (int e = 0; e <= 25; ++e){
if (encodedMessage[i] == legend[1][e]){
message += legend[0][e];
break;
}
}
}
}
}
static void input(string& message){
cout << "Enter a message: ";
cin.ignore();
getline(cin, message);
}
void inputEncode(string& message, string& encodedMessage){
input(message);
encode(message, encodedMessage);
cout << "\nEncoded message is "<<encodedMessage<<endl;
}
void inputDecodeFunc(string& decodedMessage){
string encodedMessage;
input(encodedMessage);
decode(encodedMessage, decodedMessage);
cout << "\nDecoded message is "<<decodedMessage<<endl;
}
};
bool osCheck(){
#ifndef _WIN32
return false;
#else
return true;
#endif
}
class codeLegendStorage{
public:
char legend[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
string cwd;
string legendName;
string storagePath;
json legendData;
bool weenDOS = osCheck();
static bool useOldCode(){
char input;
do {
cout << "Do you have an old code to use? [y/n]: ";
cin >> input;
} while (!(input == 'Y' || input == 'y' || input == 'N' || input == 'n'));
cout << endl;
if (input == 'Y' || input == 'y'){
return true;
}
else {
return false;
}
}
void reuseOldCode(){
string codeName;
cout << "Enter the name of the code: ";
cin >> codeName;
ifstream legendStorage(cwd + codeName + ".json");
legendStorage >> legendData;
int i = 0;
for (auto z: legendData["code"].get<string>()){
legend[i] = z;
++i;
}
legendStorage.close();
}
void legendDir(){
if (weenDOS) {
cout << "PATH: " << getenv("PATH") << endl;
int count = 0;
cwd = "";
for (auto z : string(getenv("PATH"))){
if (count != 3){
cwd += z;
if (z == '\\'){
++count;
}
}
}
cwd += "Documents\\CodeLegends\\";
}
else {
int count = 0;
cwd = "";
for (auto z : string(getenv("PATH"))){
if (count != 3){
cwd += z;
if (z == '/'){
++count;
}
}
}
cwd += "Documents/CodeLegends/";
}
}
void jsonLegendStorage(){
storagePath = cwd + legendName + string(".json");
fstream jsonFile(storagePath.c_str(), fstream::out | fstream::trunc);
if (!(jsonFile.is_open())){
cout << "ERROR - File " << legendName << ".json" << " was not created" << endl;
}
legendData["code"] = legend;
jsonFile << setw(4) << legendData << endl;
jsonFile.close();
}
void storeLegend(){
char store;
do {
cout << "Would you like to store this code? [y/n]: ";
cin >> store;
} while (!(store == 'Y' || store == 'y' || store == 'N' || store == 'n'));
cout <<endl;
if (store == 'Y' || store == 'y') {
cout << "Enter a name for this code: "; /// TODO: Later needs to make sure no duplicate names are entered
cin >> legendName;
cout << endl;
jsonLegendStorage();
}
}
};
int main() {
alphabet encode;
codeLegendStorage legendStorage;
legendStorage.legendDir(); /// Sets up Legend Storage Directories
if (codeLegendStorage::useOldCode()){
legendStorage.reuseOldCode();
for (int i = 0; i <= 26; ++i) {
encode.legend[1][i] = legendStorage.legend[i];
}
}
else {
encode.allVariables();
/// Moves a copy of the legend into the legendStorage class to turn into JSON data
for (int i = 0; i <= 26; ++i) {
legendStorage.legend[i] = encode.legend[1][i];
}
legendStorage.storeLegend();
}
string message, encodedMessage, decodedMessage;
char encodeDecode = 'i';
do {
message = "";
encodedMessage = "";
decodedMessage = "";
cout << "Would you like to decode or encode? [e/d] ([x] to exit): ";
cin >> encodeDecode;
while (encodeDecode != 'e' && encodeDecode != 'd' && encodeDecode != 'x'){
cout << "\n[e/d] ([x] to exit)\n"<<endl;
cout << "Would you like to decode or encode?: ";
cin >> encodeDecode;
}
if (encodeDecode == 'e') {
cout << endl;
encode.inputEncode(message, encodedMessage);
cout << endl;
} else if (encodeDecode == 'd') {
cout << endl;
encode.inputDecodeFunc(decodedMessage);
cout << endl;
}
} while (encodeDecode != 'x');
return 0;
}