-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotor.cpp
287 lines (225 loc) · 5.67 KB
/
rotor.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "rotor.h"
#include "helper.h"
#include "errors.h"
#include <fstream>
#include <istream>
#include <iostream>
using namespace std;
bool Rotor::set_rotor(char const filename[], int& error)
{
ifstream rotor_file;//Open file
rotor_file.open(filename);
if(rotor_file.fail())//If error opening file
{
cerr << "Rotor file " << filename << " open failed." << endl;
error = ERROR_OPENING_CONFIGURATION_FILE;
return false;
}
if (eof_test(rotor_file))//Check if file empty
{
cerr << "Rotor file " << filename << " empty." << endl;
rotor_file.close();
error = INVALID_ROTOR_MAPPING;
return false;
}
int rotor_[26];
int index, repeat_index = 0;
for (index = 0 ; index <= 25 && !(eof_test(rotor_file)) ; index++)
{
if(!(symbol_test(rotor_file)))//Check for non-numeric chars
{
cerr << "Non-numeric character in rotor file "
<< filename << "." << endl;
rotor_file.close();
error = NON_NUMERIC_CHARACTER;
return false;
}
rotor_file >> rotor_[index];//Read in
if (!(range_test(rotor_, index)))//Check number within range
{
cerr << "Number out of range found in rotor file "
<< filename << "." << endl;
rotor_file.close();
error = INVALID_INDEX;
return false;
}
if (index > 0)//Check for repetition of read in numbers
{
if(!(repetition_test(rotor_, index, repeat_index)))
{
cerr << "Invalid mapping of input " << index
<< " to output " << rotor_[index] << " in "
<< filename << "." << endl << "Output " <<
rotor_[index] << " is already mapped to input "
<< repeat_index << "." << endl;
error = INVALID_ROTOR_MAPPING;
return false;
}
}
}
//Check if number of mappings is insufficient
if (index < 26 && eof_test(rotor_file))
{
cerr << "Insufficient number of mappings in rotor file "
<< filename << "." << endl;
rotor_file.close();
error = INVALID_ROTOR_MAPPING;
return false;
}
if(!set_notches(filename, rotor_file, error))//Read in notches
return false;
convert_rotor(rotor_);//Form mapping and backwards_mapping
return true;
}
bool Rotor::set_notches(char const filename[], ifstream& rotor_file,
int& error)
{
int index, repeat_index = 0;
//Read in notches
for (index = 0 ; index <= 25 && !(eof_test(rotor_file)) ; index++)
{
if(!(symbol_test(rotor_file)))//Check for non-numeric symbols
{
cerr << "Non-numeric character found in rotor file "
<< filename << "." << endl;
rotor_file.close();
error = NON_NUMERIC_CHARACTER;
return false;
}
rotor_file >> notches[index];
if (!(range_test(notches, index)))//Check if number within range
{
cerr << "Number out of range found in rotor file "
<< filename << "." << endl;
rotor_file.close();
error = INVALID_INDEX;
return false;
}
if (index > 0)//Check for repetition of read in numbers
{
if(!(repetition_test(notches, index, repeat_index)))
{
cerr << "Notch repetition found in rotor file "
<< filename << "." << endl;
rotor_file.close();
error = INVALID_ROTOR_MAPPING;
return false;
}
}
}
if (index > 26)//Check for extra notch mappings
{
cerr << "Too many notch parameters in rotor file " << filename
<< "." << endl;
rotor_file.close();
error = INVALID_ROTOR_MAPPING;
return false;
}
rotor_file.close();
num_notches = index;
return true;
}
void Rotor::rotor_rotate()
{
forwards_rotor_rotate();
backwards_rotor_rotate();
for (int i = 0 ; i < num_notches ; i++)
{
if (notches[i] == mapping[0][0])
{
notch = true;
}
}
}
void Rotor::forwards_rotor_rotate()
{
int temp1 = mapping[0][0], temp2 = mapping[0][1];
for (int i = 0; i < 25; i++)
{
mapping[i][0] = mapping[i + 1][0];
mapping[i][1] = mapping[i + 1][1];
}
mapping[25][0] = temp1;
mapping[25][1] = temp2;
}
void Rotor::backwards_rotor_rotate()
{
int temp1 = mapping_backwards[0][0], temp2 = mapping_backwards[0][1];
for (int i = 0; i < 25; i++)
{
mapping_backwards[i][0] = mapping_backwards[i + 1][0];
mapping_backwards[i][1] = mapping_backwards[i + 1][1];
}
mapping_backwards[25][0] = temp1;
mapping_backwards[25][1] = temp2;
}
void Rotor::convert_rotor(int rotor_[])
{
for (int i = 0; i <= 25; i++)
{
mapping[i][0] = i;
mapping[i][1] = rotor_[i];
}
create_backwards_mapping();
}
void Rotor::calibrate_start_pos(int positions[], int rotor_index,
int num_rotors)
{
while (mapping[0][0] != positions[(num_rotors - 1) - rotor_index])
{
rotor_rotate();
}
while (mapping_backwards[0][1] != positions[(num_rotors - 1)
- rotor_index])
{
backwards_rotor_rotate();
}
}
char Rotor::rtol(int i)
{
int x, j = mapping[i][1];
for (x = 0 ; x <= 25 && mapping[x][0] != j; x++);
return x;
}
char Rotor::ltor(int i)
{
int x, j = mapping_backwards[i][0];
for (x = 0 ; x <= 25 && mapping_backwards[x][1] != j; x++);
return x;
}
void Rotor::create_backwards_mapping()
{
for (int j = 0 ; j <= 25 ; j++)
{
mapping_backwards[j][0] = mapping[j][0];
mapping_backwards[j][1] = mapping[j][1];
}
sort_backwards_mapping();
}
void Rotor::sort_backwards_mapping()
{
int smallest_index;
for(int i = 0; i < 26; i++)
{
smallest_index = next_smallest_index(i);
int temp1 = mapping_backwards[i][0];
int temp2 = mapping_backwards[i][1];
mapping_backwards[i][0] = mapping_backwards[smallest_index][0];
mapping_backwards[i][1] = mapping_backwards[smallest_index][1];
mapping_backwards[smallest_index][0] = temp1;
mapping_backwards[smallest_index][1] = temp2;
}
}
int Rotor::next_smallest_index(int start_index)
{
int min = mapping_backwards[start_index][1], min_index = start_index;
for (int i = start_index + 1; i < 26; i++)
{
if (mapping_backwards[i][1] < min)
{
min = mapping_backwards[i][1];
min_index = i;
}
}
return min_index;
}