-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreflector.cpp
121 lines (105 loc) · 2.66 KB
/
reflector.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
#include "reflector.h"
#include "helper.h"
#include "errors.h"
#include <fstream>
#include <istream>
#include <iostream>
using namespace std;
void Reflector::operate_reflector(int &i)
{
for (int a = 0; a <= 25; a++)
{
if (i == reflector[a])
{
if (a % 2)
{
i = (reflector[a - 1]);
return;
}
if (!(a % 2))
{
i = (reflector[a + 1]);
return;
}
}
}
}
bool Reflector::set_reflector(char const filename[], int& error)
{
ifstream reflector_file;//Open file
reflector_file.open(filename);
if(reflector_file.fail())//If error opening file
{
cerr << "Reflector file " << filename << " open failed."
<< endl;
error = ERROR_OPENING_CONFIGURATION_FILE;
return false;
}
if (eof_test(reflector_file))//Check if file empty
{
cerr << "Reflector file " << filename << " empty." << endl;
reflector_file.close();
error = INCORRECT_NUMBER_OF_REFLECTOR_PARAMETERS;
return false;
}
int index, repeat_index = 0;
for (index = 0 ; index <= 26 && !(eof_test(reflector_file)) ; index++)
{
if(index <= 25)
{
if(!(symbol_test(reflector_file)))//Check for non-numeric chars
{
cerr << "Non-numeric character in reflector file "
<< filename << "." << endl;
reflector_file.close();
error = NON_NUMERIC_CHARACTER;
return false;
}
reflector_file >> reflector[index];//Read in
if (!(range_test(reflector, index)))//Check number within range
{
cerr << "Number out of range in reflector file "
<< filename << "." << endl;
reflector_file.close();
error = INVALID_INDEX;
return false;
}
if (index > 0)//Check for reptitions of read in numbers
{
if(!(repetition_test(reflector, index,
repeat_index)))
{
cerr << "Invalid mapping of input " <<
reflector[index] << " in " << filename
<< "." << endl;
reflector_file.close();
error = INVALID_REFLECTOR_MAPPING;
return false;
}
}
}
}
if (index % 2)//Check if number read in is odd
{
cerr << "Incorrect (odd) number of mappings in reflector file "
<< filename << "." << endl;
reflector_file.close();
error = INCORRECT_NUMBER_OF_REFLECTOR_PARAMETERS;
return false;
} else if (index < 26)//Check if number read in is less than 26
{
cerr << "Insufficient number of mappings in reflector file "
<< filename << "." << endl;
reflector_file.close();
error = INCORRECT_NUMBER_OF_REFLECTOR_PARAMETERS;
} else if (index > 26)//Check if number read in is over 26
{
cerr << "Too many mappings in reflector file " << filename
<< "." << endl;
reflector_file.close();
error = INCORRECT_NUMBER_OF_REFLECTOR_PARAMETERS;
return false;
}
reflector_file.close();
return true;
}