-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
95 lines (71 loc) · 2.49 KB
/
parser.cpp
File metadata and controls
95 lines (71 loc) · 2.49 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
#include <bits/stdc++.h>
#include "parser.h"
using namespace std;
// Extract both model and wirings of the rotor referenced in string s. This is done by splitting a given line into a model name, and its respective wirings.
pair<string, string> getFields(string s)
{
string model = "";
string wiring = "";
for (auto c : s)
{
// The model and the wiring for each rotor are separated by a ":".
if (c != ':')
model += c;
else
{
// Removes the model name and the two following characters ": " from s. The resultant string is the wiring for the rotor referenced by model.
wiring = s.erase(0, model.length() + 2);
break;
}
}
pair<string, string> pair_model_wiring = make_pair(model, wiring);
return pair_model_wiring;
}
pair<string, string> searchRotor(int rotor_index)
{
map<int, string> map_rotorModels =
{
{1, "ROTOR_I"},
{2, "ROTOR_II"},
{3, "ROTOR_III"}};
//Allocate a file enabled for read-only
ifstream f_rotor_wirings;
//Open specified .GraphModellingLanguage file
f_rotor_wirings.open("wirings.txt");
if (!f_rotor_wirings)
{
cout << "Unable to read rotor wirings. Check if the file 'wirings.txt' exists.\n";
}
string line;
pair<string, string> pair_model_wirings;
while (getline(f_rotor_wirings, line))
{
pair_model_wirings = getFields(line);
if (pair_model_wirings.first == map_rotorModels[rotor_index])
break;
}
return pair_model_wirings;
}
pair<string, string> searchReflector(int reflector_index)
{
map<int, string> map_reflectorModels =
{
{1, "REFLECTOR_A"}};
//Allocate a file enabled for read-only
ifstream f_reflector_wirings;
//Open specified .GraphModellingLanguage file
f_reflector_wirings.open("reflector-wiring.txt");
if (!f_reflector_wirings)
{
cout << "Unable to read reflector wirings. Check if the file 'reflector.txt' exists.\n";
}
string line;
pair<string, string> pair_model_wirings;
while (getline(f_reflector_wirings, line))
{
pair_model_wirings = getFields(line);
if (pair_model_wirings.first == map_reflectorModels[reflector_index])
break;
}
return pair_model_wirings;
}