-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugboard.cpp
76 lines (66 loc) · 1.72 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
//
// Created by Andi Koh on 15/10/2016.
//
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <map>
#include "Plugboard.hpp"
#include "utilities.hpp"
using namespace std;
Plugboard::Plugboard(string filename) {
this->filename = filename;
this->values = new int[2*NUMLETTERS];
this->numValues = 0;
generateIntCharMap();
this->readfile();
generateMap();
}
void Plugboard::readfile() {
ifstream pbFile;
pbFile.open(this->filename);
string contents;
if (pbFile.is_open()) {
getline(pbFile, contents);
pbFile.close();
} else {
cout << "failure opening file" << endl;
exit(EXIT_FAILURE);
}
string token;
string delim = " ";
size_t pos = 0;
for (int i = 0; i < 2*NUMLETTERS; i++) {
pos = contents.find(delim);
if (pos != string::npos) {
token = contents.substr(0, pos);
this->values[i] = atoi(token.c_str());
contents.erase(0, pos + delim.length());
} else {
this->numValues = i+1;
break;
}
}
this->values[numValues - 1] = atoi((contents).c_str());
}
void Plugboard::generateMap() {
for (int i = 0; i < this->numValues; i+=2) {
this->valueMap[values[i]] = values[i+1];
this->valueMap[values[i+1]] = values[i];
}
}
char Plugboard::encodeOne(char in) {
char out;
int lookup = this->charToInt(in);
map<int, int>::iterator it = valueMap.find(lookup);
if (it != valueMap.end()) {
int result = valueMap[lookup];
out = this->intToChar(result);
} else {
//lookup value is not in map
//value is mapped to itself
out = in;
}
return out;
}
Plugboard::Plugboard() {}