-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMachine.cpp
More file actions
96 lines (86 loc) · 3.31 KB
/
Machine.cpp
File metadata and controls
96 lines (86 loc) · 3.31 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
#include "Machine.hpp"
#include <ctype.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include "CharacterException.hpp"
using namespace::std;
class MachineImpl : public Machine {
public:
static Transformer* load_machine( char* plugboard_file, std::vector<std::string> rotor_files );
// Encrpyts a char.
// If the char is A-Z converts it to integer
// representation then passes through the plugboard, rotors, reflector,
// inverse rotors and finally back through the plugboard before
// displaying it on screen.
// If the char is white space it ignores it.
// Anything else and it produces an error.
bool encode( EnigmaLetter value );
bool bind( Receptor* receptor );
private:
Machine();
~Machine();
std::vector<Rotor> m_rotors;
Transformer* m_last;
Transformer* m_start;
// Plugboard m_plugboard;
// Reflector m_reflector;
Ui::EnigmaMachine ui;
};
// Move this to builder.
Transformer* Machine::load_machine( char* plugboard_file, vector<string> rotor_files ){
Machine* machine = new Machine();
// Make all these pointers owned by machine.
// Use scoped pointer (auto_pointer in memory library).
// Destory vs. Delete
// Object a;
// Object* b = new Object();
//
// b->~Object();
// free(b);
Transformer* forwards_plugboard = Plugboard::load_plugboard(plugboard_file);
Transformer* backwards_plugboard = Plugboard::load_plugboard(plugboard_file);
Rotor* next_rotor = NULL;
vector<Rotor*> rotors;
for( vector<string>::reverse_iterator rev_itr = rotor_files.rbegin(); rev_itr != rotor_files.rend(); ++rev_itr ){
Rotor* rotor = Rotor::load_rotor((*rev_itr).c_str(), next_rotor);
rotors.push_back(rotor);
next_rotor = rotor;
}
// **********************************
// AHHH WHERE DO I DELETE ALL THESE POINTERS?, IN OTHERS DESTRUCTORS? WHAT ABOUT NEXT_ROTOR?
// Shared pointers from boost, if you don't have cycles...
//
// **********************************
Transformer* reflector = new Reflector();
Transformer* last_backwards_rotor = rotors.back()->get_backward();
Transformer* last_forwards_rotor = rotors.back()->get_forward();
Transformer* first_backwards_rotor = rotors.front()->get_backward();
Transformer* first_forwards_rotor = rotors.front()->get_forward();
machine->m_last = last_backwards_rotor;
machine->m_start = forwards_plugboard;
first_backwards_rotor->bind(backwards_plugboard);
reflector->bind(last_backwards_rotor);
last_forwards_rotor->bind(reflector);
forwards_plugboard->bind(first_forwards_rotor);
return machine;
}
Machine::Machine(){
}
Machine::~Machine(){
delete m_last;
delete m_start;
}
bool Machine::bind(Receptor* receptor){
return m_last->bind(receptor);
}
// TODO: Work out what to do with encode.
bool Machine::encode( EnigmaLetter value )
{
// This is broken, isupper does not perform the same test that you rely on in
// convert_to_int, it understands non english upper case letters.
// In general, don't separate tests from operations when they are fundamentaly
// tied together, write a single operation and test it succeeds instead.
return m_start->encode(value);
}