1
1
#include " Enigma.hxx"
2
+ #include < algorithm>
2
3
3
4
int Enigma::rotor_index (const std::string label) const
4
5
{
5
6
RotorLabels::iterator it = std::find (_impl->_rotor_labels .begin (), _impl->_rotor_labels .end (), label);
6
7
if (it == _impl->_rotor_labels .end ())
7
8
{
8
- _impl-> _logger . Critical (" Could not find Rotor, '" +label+ " ' in rotor list" );
9
+ spdlog::critical (" Could not find Rotor, '{} ' in rotor list" , label );
9
10
10
11
std::string rotor_list = " " ;
11
12
@@ -14,7 +15,7 @@ int Enigma::rotor_index(const std::string label) const
14
15
rotor_list += label+" " ;
15
16
}
16
17
17
- _impl-> _logger . Critical (" Rotor List is [%1% ]" , rotor_list);
18
+ spdlog::critical (" Rotor List is [{} ]" , rotor_list);
18
19
}
19
20
return std::distance (_impl->_rotor_labels .begin (), it);
20
21
}
@@ -23,14 +24,12 @@ void _enigma_impl::_init()
23
24
{
24
25
srand (time (NULL ));
25
26
26
- setLoggerLevel ( (_debug) ? " DEBUG" : " INFO" );
27
-
28
27
if (_enigma_type == " M3" )
29
28
{
30
29
_rotor_labels = {" left" , " middle" , " right" };
31
30
if (_rotor_ids.size () != 3 )
32
31
{
33
- _logger. Error (" Three rotor types only must be provided for Enigma machine 'M3'" );
32
+ spdlog::error (" Three rotor types only must be provided for Enigma machine 'M3'" );
34
33
exit (EXIT_FAILURE);
35
34
}
36
35
@@ -40,7 +39,7 @@ void _enigma_impl::_init()
40
39
41
40
if (std::distance (_temp.begin (), it) != 3 )
42
41
{
43
- _logger. Error (" All chosen rotor IDs must be unique" );
42
+ spdlog::error (" All chosen rotor IDs must be unique" );
44
43
exit (EXIT_FAILURE);
45
44
}
46
45
@@ -53,7 +52,7 @@ void _enigma_impl::_init()
53
52
{
54
53
if (_rotor_ids.size () != 4 )
55
54
{
56
- _logger. Error (" Four rotor types only must be provided for Enigma machine 'M4'" );
55
+ spdlog::error (" Four rotor types only must be provided for Enigma machine 'M4'" );
57
56
exit (EXIT_FAILURE);
58
57
}
59
58
@@ -64,7 +63,7 @@ void _enigma_impl::_init()
64
63
65
64
if (std::distance (_temp.begin (), it) != 4 )
66
65
{
67
- _logger. Error (" All chosen rotor IDs must be unique" );
66
+ spdlog::error (" All chosen rotor IDs must be unique" );
68
67
exit (EXIT_FAILURE);
69
68
}
70
69
_rotors[_rotor_labels[0 ]] = Rotors (_rotor_ids[0 ]);
@@ -75,14 +74,14 @@ void _enigma_impl::_init()
75
74
76
75
else
77
76
{
78
- _logger. Error (" Unrecognised Enigma type '%1%'" , _enigma_type);
77
+ spdlog::error (" Unrecognised Enigma type '%1%'" , _enigma_type);
79
78
exit (EXIT_FAILURE);
80
79
}
81
80
}
82
81
83
82
void _enigma_impl::_move_rotor (const std::string rotor, const int amount)
84
83
{
85
- _logger. Debug (" Rotating rotor %1% by %2%" , rotor, std::to_string (amount));
84
+ spdlog::debug (" Rotating rotor %1% by %2%" , rotor, std::to_string (amount));
86
85
for (int i{0 }; i < amount; ++i)
87
86
{
88
87
_rotors[rotor]->rotate_rotor ();
@@ -92,23 +91,22 @@ void _enigma_impl::_move_rotor(const std::string rotor, const int amount)
92
91
void Enigma::ringstellung (const std::string name, const int amount)
93
92
{
94
93
RotorMap rotors = static_cast <RotorMap>(_impl->_rotors );
95
- Logger logger = static_cast <Logger>(_impl->_logger );
96
94
for (int i{0 }; i < amount; ++i)
97
95
{
98
96
char letter = ' A' ;
99
- logger. Debug (" Ringstellung: Conversion for rotor %1% was %2% to %3%" ,
97
+ spdlog::debug (" Ringstellung: Conversion for rotor %1% was %2% to %3%" ,
100
98
name, std::string (1 , letter),
101
99
std::string (1 , rotors[name]->get_rotor_conversion (letter)));
102
100
rotors[name]->rotate_inner_ring ();
103
- logger. Debug (" Ringstellung: Conversion for rotor %1% now %2% to %3%" ,
101
+ spdlog::debug (" Ringstellung: Conversion for rotor %1% now %2% to %3%" ,
104
102
name, std::string (1 , letter),
105
103
std::string (1 , rotors[name]->get_rotor_conversion (letter)));
106
104
}
107
105
}
108
106
109
107
void _enigma_impl::_set_rotor (const std::string name, const char letter)
110
108
{
111
- _logger. Debug (" Setting rotor %1% to %2%" , name, std::string (1 , letter));
109
+ spdlog::debug (" Setting rotor %1% to %2%" , name, std::string (1 , letter));
112
110
113
111
while (_rotors[name]->get_face_letter () != letter)
114
112
{
@@ -119,15 +117,15 @@ void _enigma_impl::_set_rotor(const std::string name, const char letter)
119
117
char _enigma_impl::_get_rotor_conv (const std::string name, const char letter)
120
118
{
121
119
const char converted_letter = _rotors[name]->get_rotor_conversion (letter);
122
- _logger. Debug (" Rotor %1% conversion: %2% to %3%" , name, std::string (1 , letter),
120
+ spdlog::debug (" Rotor %1% conversion: %2% to %3%" , name, std::string (1 , letter),
123
121
std::string (1 , converted_letter));
124
122
return converted_letter;
125
123
}
126
124
127
125
char _enigma_impl::_get_rotor_conv_inv (const std::string name, const char letter)
128
126
{
129
127
const char converted_letter = _rotors[name]->get_rotor_conversion_inv (letter);
130
- _logger. Debug (" Rotor %1% conversion: %2% to %3%" , name, std::string (1 , letter),
128
+ spdlog::debug (" Rotor %1% conversion: %2% to %3%" , name, std::string (1 , letter),
131
129
std::string (1 , converted_letter));
132
130
return converted_letter;
133
131
}
@@ -158,7 +156,7 @@ char _enigma_impl::_get_inter_rotor_conv(const std::string name_1,
158
156
159
157
const char output = _rotors[name_2]->get_letters_dict ()[n];
160
158
161
- _logger. Debug (" Rotor %1% rotor to %2% rotor conversion: %3% to %4%" ,
159
+ spdlog::debug (" Rotor %1% rotor to %2% rotor conversion: %3% to %4%" ,
162
160
name_1, name_2, std::string (1 , letter),
163
161
std::string (1 , output));
164
162
@@ -168,13 +166,12 @@ char _enigma_impl::_get_inter_rotor_conv(const std::string name_1,
168
166
char Enigma::type_letter (const char letter)
169
167
{
170
168
const char l = std::toupper (letter);
171
- Logger logger = static_cast <Logger>(_impl->_logger );
172
169
const RotorLabels rotor_labels = static_cast <RotorLabels>(_impl->_rotor_labels );
173
170
RotorMap rotors = static_cast <RotorMap>(_impl->_rotors );
174
- logger. Debug (" -----------------------" );
171
+ spdlog::debug (" -----------------------" );
175
172
Plugboard* plug_board = static_cast <Plugboard*>(_impl->_plugboard );
176
173
char cipher = plug_board->plugboard_conversion (l);
177
- logger. Debug (" Plugboard conversion: %1% to %2%" , std::string (1 , l), std::string (1 , cipher));
174
+ spdlog::debug (" Plugboard conversion: %1% to %2%" , std::string (1 , l), std::string (1 , cipher));
178
175
// Move the rightmost rotor
179
176
180
177
_impl->_move_rotor (rotor_labels[rotor_labels.size ()-1 ], 1 );
@@ -237,16 +234,16 @@ char Enigma::type_letter(const char letter)
237
234
}
238
235
239
236
const char cipher_out = plug_board->plugboard_conversion_inv (cipher);
240
- logger. Debug (" Plugboard conversion: %1% to %2%" , std::string (1 , cipher), std::string (1 , cipher_out));
241
- logger. Debug (" -----------------------" );
237
+ spdlog::debug (" Plugboard conversion: %1% to %2%" , std::string (1 , cipher), std::string (1 , cipher_out));
238
+ spdlog::debug (" -----------------------" );
242
239
243
240
return cipher_out;
244
241
}
245
242
246
243
char _enigma_impl::_get_reflector_conv (const char letter)
247
244
{
248
245
const char out = _reflector->reflector_conversion (letter);
249
- _logger. Debug (" Reflector conversion: %1% to %2%" , std::string (1 , letter), std::string (1 , out));
246
+ spdlog::debug (" Reflector conversion: %1% to %2%" , std::string (1 , letter), std::string (1 , out));
250
247
return out;
251
248
}
252
249
@@ -289,7 +286,7 @@ void Enigma::set_key(const std::string user_key)
289
286
throw std::invalid_argument (" Key length must match no. of rotors." );
290
287
}
291
288
292
- boost::to_upper (key);
289
+ std::transform (key. begin (), key. end (), key. begin (), []( const char c) { return std::toupper (c);} );
293
290
294
291
for (unsigned int i{0 }; i < rotor_labels.size (); ++i)
295
292
{
0 commit comments