-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
286 lines (253 loc) · 5.35 KB
/
util.cpp
File metadata and controls
286 lines (253 loc) · 5.35 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include "util.h"
using namespace std;
fcstream::fcstream(const char* fileName)
{
m_fileStream.open(fileName, ofstream::app);
}
fcstream::~fcstream()
{
m_fileStream.flush();
m_fileStream.close();
}
void push_list(string &element, int &index, vector<Entity>& list)
{
bool duplicate = false;
for (int i = 0; i < index; i++) //checking if element already exists in the list
{
if (list[i].oldname == element)
{
duplicate = true;
break;
}
}
if (!duplicate)
{
list.push_back(Entity());
list[index] = { element };
index++;
}
}
void push_list(string &element, vector<string>& list)
{
bool duplicate = false;
for (size_t i = 0; i < list.size(); i++) //checking if element already exists in the list
{
if (list[i] == element)
{
duplicate = true;
break;
}
}
if (!duplicate)
{
string s;
list.push_back(s);
//printf("%s\n", element.c_str());
list[(list.size() - 1)] = { element };
}
}
bool contains(string& str, const string& str1)
{
string::size_type found = str.find(str1);
return (found != string::npos);
}
vector<string> get_file_list(string DATA_DIR)
{
HANDLE hFind;
WIN32_FIND_DATAA data;
vector<string> files;
hFind = FindFirstFileA(DATA_DIR.c_str(), &data);
if (hFind != INVALID_HANDLE_VALUE) {
do {
files.push_back(data.cFileName);
//printf("%s\n", data.cFileName);
} while (FindNextFileA(hFind, &data));
FindClose(hFind);
}
return files;
}
string fetch_property(const string& str)
{
size_t max = (str.find_first_of("(") - 1);
string result;
for (int i = max; i > -1; i--)
{
if (isblank(str[i]) || (i == 0) )
{
result = str.substr((i+1), (max - i));
break;
}
}
return result;
}
string fetch_defaultv(const string& str)
{
size_t n = (str.find_first_of(":")) + 1;
string s = str.substr(n, (str.length() - n));
size_t m = s.find_first_of(":");
if (m == string::npos)
{
s.clear();
return s;
}
n += m;
s = str.substr(n, (str.length() - n));
s.clear();
for (size_t i = (n + 1); i < str.length(); i++)
{
if (str[i] == ':')
{
break;
}
else if (!isblank(str[i]) && (str[i] != '"'))
{
s += str[i];
}
}
return s;
}
bool is_number(const string& s)
{
return !s.empty() && find_if(s.begin(), s.end(), [](char c) { return !(isdigit(c) | (c == '-') | (c == '.')); }) == s.end();
}
bool has_number(const string& s)
{
return !s.empty() && find_if(s.begin(), s.end(), [](char c) { return isdigit(c); }) != s.end();
}
unsigned int in_list(vector<Entity>& list, string& s)
{
for (unsigned int i = 0; i < size(list); i++)
{
if (list[i].oldname == s)
{
return i;
}
}
return UINT_MAX;
}
unsigned int in_list(vector<string>& list, string& s)
{
for (unsigned int i = 0; i < size(list); i++)
{
if (list[i] == s)
{
return i;
}
}
return UINT_MAX;
}
void scoop_fgd(ifstream& inf, vector<string>& keyvalue_list, vector<string>& input_list)
{
while (inf)
{
string s;
string str;
getline(inf, str);
transform(str.begin(), str.end(), str.begin(), ::tolower);
if (contains(str, "target_destination") || contains(str, "target_source"))
{
s = fetch_property(str);
}
/*
else
{
if (contains(str, "string"))
{
if (contains(str, "target"))
{
if (!is_number(str))
{
s = fetch_property(str);
}
}
}
}
*/
if (!s.empty())
{
if (contains(str.substr(0, 8), "input "))
{
push_list(s, input_list);
}
else if (!contains(str.substr(0, 8), "output "))
{
string t = fetch_defaultv(str);
//printf("%s\n", s.c_str());
if (t.empty() || !is_number(t))
{
push_list(s, keyvalue_list);
}
}
}
}
}
vector<string> fetch_keyvalue(string& s)
{
vector<string> keyvalues;
size_t i = 0;
size_t j = 0;
while (i < s.length())
{
i = ((s.substr(i).find("\"")) + 1) + i;
if (i == string::npos) { break; }
size_t j = s.substr(i).find("\"");
keyvalues.push_back(s.substr(i, j));
i = i + j + 1;
}
return keyvalues;
}
vector<string> fetch_output(string& s, const string& c)
{
vector<string> addoutput;
size_t j = 0;
size_t k = 0;
while (1)
{
j = s.substr(j).find_first_of(c);
if (j == string::npos) { addoutput.push_back(s.substr(k)); break; }
else { j += (k + 1); }
addoutput.push_back(s.substr(k, ((j - 1) - k)));
k = j;
}
return addoutput;
}
void split_output(vector<string>& list)
{
unsigned int length = size(list);
for (unsigned int i = 0; i < length; i++)
{
size_t index = list[i].find(" ");
if (index != string::npos)
{
string extract = list[i].substr((index + 1));
list.insert((list.begin() + (i + 1)), extract);
list[i].resize(index);
length++;
i++;
}
}
}
string makestring(char* chars, unsigned int value, unsigned int& size) //generates a unique string from an integer
{
string result(1, chars[(value - (size * (value / size)))]); // # -> ~
if (value > (size - 1))
{
string bstr(1, chars[(value / size) - 1]);
result = bstr + result;
}
return result;
}
int get_first_nonctrl(string& line)
{
for (unsigned int i = 0; i < line.size(); i++)
{
if (!iscntrl(line[i]))
{
return i;
}
}
return string::npos;
}