-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple_fso.cpp
134 lines (110 loc) · 3.16 KB
/
simple_fso.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
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "simple_fso.h"
#include "gm.h"
#include <fstream>
#include <sstream>
#include <stdlib.h>
SimpleFSO::~SimpleFSO() {
if(gm1 != nullptr) {
delete gm1;
}
if(gm2 != nullptr) {
delete gm2;
}
}
void SimpleFSO::load() {
// Open file
std::ifstream ifstr(filename, std::ifstream::in);
// Read in contents
std::string line, token;
std::string gm1_serial_number = "", gm2_serial_number = "";
int gm1_channel = -1, gm2_channel = -1;
int gm1_starting_value = -1, gm2_starting_value = -1;
while(std::getline(ifstr, line)) {
token = "";
std::stringstream sstr(line);
sstr >> token;
bool match = true;
if(token == "gm1_serial_number") {
sstr >> gm1_serial_number;
} else if(token == "gm2_serial_number") {
sstr >> gm2_serial_number;
} else if(token == "gm1_channel") {
sstr >> gm1_channel;
} else if(token == "gm2_channel") {
sstr >> gm2_channel;
} else if(token == "gm1_starting_value") {
sstr >> gm1_starting_value;
} else if(token == "gm2_starting_value") {
sstr >> gm2_starting_value;
} else {
if((gm1_starting_value == -1 || gm2_starting_value == -1) && token == "link") {
std::string tmp;
sstr >> tmp >> tmp >> gm1_starting_value >> gm2_starting_value;
}
match = false;
}
if(match) {
file_order.push_back(token);
} else {
file_order.push_back(line);
}
}
ifstr.close();
// Make GMs
gm1 = new GM(gm1_channel, gm1_serial_number);
gm2 = new GM(gm2_channel, gm2_serial_number);
gm1->connectDevice();
gm2->connectDevice();
setGM1Val(gm1_starting_value);
setGM2Val(gm2_starting_value);
std::cout << "FSO loaded from: " << filename << std::endl;
}
void SimpleFSO::save() {
if(gm1 == nullptr || gm2 == nullptr) {
std::cerr << "GMs are NULL when trying to save" << std::endl;
exit(1);
}
std::ofstream ofstr(filename, std::ofstream::out);
bool pso1 = false, pso2 = false;
for(unsigned int i = 0; i < file_order.size(); ++i) {
if(file_order[i] == "gm1_serial_number") {
ofstr << file_order[i] << " " << gm1->getSerialNumber() << std::endl;
} else if(file_order[i] == "gm2_serial_number") {
ofstr << file_order[i] << " " << gm2->getSerialNumber() << std::endl;
} else if(file_order[i] == "gm1_channel") {
ofstr << file_order[i] << " " << gm1->getChannel() << std::endl;
} else if(file_order[i] == "gm2_channel") {
ofstr << file_order[i] << " " << gm2->getChannel() << std::endl;
} else if(file_order[i] == "gm1_starting_value") {
ofstr << file_order[i] << " " << getGM1Val() << std::endl;
pso1 = true;
} else if(file_order[i] == "gm2_starting_value") {
ofstr << file_order[i] << " " << getGM2Val() << std::endl;
pso2 = true;
} else {
ofstr << file_order[i];
}
}
if(!pso1) {
ofstr << "gm1_starting_value " << getGM1Val() << std::endl;
}
if(!pso2) {
ofstr << "gm2_starting_value " << getGM2Val() << std::endl;
}
ofstr.close();
std::cout << "FSO saved to: " << filename << std::endl;
}
int SimpleFSO::getGMVal(GM* gm) const {
if(gm == nullptr) {
std::cerr << "gm is NULL" << std::endl;
exit(1);
}
return gm->getValue();
}
void SimpleFSO::setGMVal(GM* gm, int v) const {
if(gm == nullptr) {
std::cerr << "gm is NULL" << std::endl;
exit(1);
}
gm->setValue(v);
}