Repository navigation
Expand file tree
/
Copy pathStateIntercept.cpp
More file actions
171 lines (145 loc) · 3.79 KB
/
Copy pathStateIntercept.cpp
File metadata and controls
171 lines (145 loc) · 3.79 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
#include <fstream>
#include <sstream> //for std::stringstream
#include <windows.h>
#include <string>
using namespace std;
template <typename T>
class Intercept
{
public:
Intercept() : t_()
{
t_ = -1;
label = 0;
}
Intercept(const T &t) : t_(t)
{
label = 0;
}
Intercept(const T &t, int l) : t_(t)
{
label = l;
}
operator T &() { return t_; }
operator const T &() const { return t_; }
T t_;
int label;
};
class StateIntercept
{
int SIZE;
Intercept<int> state[];
public:
StateIntercept(Intercept<int> intercepts[], int length)
{
SIZE = length;
state[length];
for (int i = 0; i < length; i++)
{
state[i] = intercepts[i];
}
initStateTraceText();
}
Intercept<int> &operator[](int i)
{
if (i > SIZE)
{
cout << "Index out of bounds" << endl;
// return first element. Should be unreachable.
return state[0];
}
logCurrentState(state, SIZE);
return state[i];
}
void logCurrentState()
{
logCurrentState(state, SIZE);
}
void logCurrentState(Intercept<int> state[], int size)
{
string currentStateText = "";
currentStateText += ", [";
for (int i = 0; i < size; i++)
{
string stateValueText = to_string(state[i].t_);
if (i == 0)
currentStateText += stateValueText;
else
currentStateText += ", " + stateValueText;
}
currentStateText += "]";
logStateTraceText(currentStateText);
}
string getCurrentFileName()
{
char buffer[MAX_PATH];
GetModuleFileName(NULL, buffer, MAX_PATH);
string::size_type pos = string(buffer).find_last_of("\\/");
// std::cerr << "Got file name: " << string(buffer).substr(pos+1, string(buffer).find_last_of("."));
return string(buffer).substr(pos + 1, string(buffer).find_last_of("."));
}
void initStateTraceText()
{
std::stringstream addr;
addr << &state;
ifstream f("out-datasetText/logState-" + getCurrentFileName() + "-" + addr.str() + ".json");
if (f.good())
return;//file exist, continue logging
string statesLabelArray = "";
statesLabelArray += "[";
for (int i = 0; i < SIZE; i++)
{
string label = to_string(state[i].label);
if (i == 0)
statesLabelArray += label;
else
statesLabelArray += ", " + label;
}
statesLabelArray += "]";
ofstream logFile("out-datasetText/logState-" + getCurrentFileName() + "-" + addr.str() + ".json");
logFile << "{\"labels\": " << statesLabelArray << ", \"stateTraces\": ["
<< " ]}";
}
//replace the " ]}" with a new element
void logStateTraceText(string stateText)
{
std::stringstream addr;
addr << &state;
ifstream in("out-datasetText/logState-" + getCurrentFileName() + "-" + addr.str() + ".json");
// ofstream out("out-datasetText/logStateTemp-" + getCurrentFileName() + "-" + addr.str() + ".json");
ofstream out;
out.open("out-datasetText/logState-" + getCurrentFileName() + "-" + addr.str() + ".json", std::ios_base::app);
string wordToReplace(" ]}");
string wordToReplaceWith(stateText + " ]}");
if (!in)
{
cerr << "Could not open "
<< "out-datasetText/logState-" + getCurrentFileName() + "-" + addr.str() + ".json"
<< "\n";
return;
}
if (!out)
{
cerr << "Could not open "
<< "out-datasetText/logState-" + getCurrentFileName() + "-" + addr.str() + ".json"
<< "\n";
return;
}
string line;
size_t len = wordToReplace.length();
getline(in, line);
size_t pos = line.find(wordToReplace);
if (pos != string::npos)
{
// std::cerr << "Reading line: " << line << "\n";
if (string::npos != line.find("[ ]}")) // first element
wordToReplaceWith.erase(wordToReplaceWith.begin()); //remove the ","
line.replace(pos, len, wordToReplaceWith);
// std::cerr << "Line replaced: " << line << "\n";
// std::cerr << "\n";
}
out = ofstream("out-datasetText/logState-" + getCurrentFileName() + "-" + addr.str() + ".json");
out << line;
}
};