-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugboard.cpp
105 lines (92 loc) · 2.26 KB
/
plugboard.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
#include "helper.h"
#include "plugboard.h"
#include "errors.h"
#include <fstream>
#include <istream>
#include <iostream>
using namespace std;
void Plugboard::operate_plugboard(int &i)
{
for (int a = 0; a <= plugboard_size; a++)
{
if (i == plugboard[a])
{
if (a % 2)
{
i = (plugboard[a - 1]);
return;
}
if (!(a % 2))
{
i = (plugboard[a + 1]);
return;
}
}
}
}
bool Plugboard::set_plugboard(char const filename[], int& error)
{
ifstream plugboard_file;//Open file
plugboard_file.open(filename);
if(plugboard_file.fail())//If error opening file
{
cerr << "Plugboard file " << filename << " open failed."
<< endl;
plugboard_file.close();
error = ERROR_OPENING_CONFIGURATION_FILE;
return false;
}
int index, repeat_index = 0;
for (index = 0 ; index <= 25 && !(eof_test(plugboard_file)) ; index++)
{
if(!(symbol_test(plugboard_file)))//Check for non-numeric chars
{
cerr << "Non-numeric character in plugboard file "
<< filename << "." << endl;
plugboard_file.close();
error = NON_NUMERIC_CHARACTER;
return false;
}
plugboard_file >> plugboard[index];//Read in
if (!(range_test(plugboard, index)))//Check number within range
{
cerr << "Number out of range in plugboard file "
<< filename << "." << endl;
plugboard_file.close();
error = INVALID_INDEX;
return false;
}
if (index > 0)//Check for repetition of read in numbers
{
if(!(repetition_test(plugboard, index, repeat_index)))
{
cerr << "Invalid mapping of input "
<< plugboard[index] << " in " << filename
<< "." << endl;
plugboard_file.close();
error = IMPOSSIBLE_PLUGBOARD_CONFIGURATION;
return false;
}
}
}
plugboard_size = index - 1;
//Check for extra mappings
if (plugboard_size == 25 && !(eof_test(plugboard_file)))
{
cerr << "Too many mappings in plugboard file " << filename
<< "." << endl;
plugboard_file.close();
error = INCORRECT_NUMBER_OF_PLUGBOARD_PARAMETERS;
return false;
}
if (!(plugboard_size % 2))//Check if number of mappings is odd
{
cerr << "Odd number of mappings in plugboard file "
<< filename << "." << endl;
plugboard_file.close();
error = INCORRECT_NUMBER_OF_PLUGBOARD_PARAMETERS;
return false;
}
plugboard_file.close();
return true;
}