generated from ut-issl/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathinitialize_file_access.cpp
282 lines (250 loc) · 8.8 KB
/
initialize_file_access.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
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
/**
* @file initialize_file_access.cpp
* @brief Class to read and get parameters for the `ini` format file
*/
#include "initialize_file_access.hpp"
#include <string.h>
#include <algorithm>
#include <cstring>
#include <limits>
#include <regex>
#include "../utilities/macros.hpp"
#ifdef WIN32
IniAccess::IniAccess(const std::string file_path) : file_path_(file_path) {
// strcpy_s(file_path_char_, (size_t)_countof(file_path_char_), file_path_.c_str());
strncpy(file_path_char_, file_path_.c_str(), kMaxCharLength);
}
#else
IniAccess::IniAccess(const std::string file_path) : file_path_(file_path), ini_reader_(file_path) {
strncpy(file_path_char_, file_path_.c_str(), kMaxCharLength);
std::string ext = ".ini";
if (file_path_.size() < 4 || !std::equal(std::rbegin(ext), std::rend(ext), std::rbegin(file_path_))) {
// this is not ini file(csv)
return;
}
if (ini_reader_.ParseError() != 0) {
std::cerr << "Error reading INI file : " << file_path_ << std::endl;
std::cerr << "\t error code: " << ini_reader_.ParseError() << std::endl;
throw std::runtime_error("Error reading INI file");
}
}
#endif
std::vector<unsigned char> IniAccess::ReadVectorUnsignedChar(const char* section_name, const char* key_name, const size_t num) {
std::vector<unsigned char> data;
for (size_t i = 0; i < num; i++) {
std::stringstream edited_key_name;
edited_key_name << key_name << "(" << i << ")";
data.push_back((unsigned char)ReadInt(section_name, edited_key_name.str().c_str()));
}
return data;
}
double IniAccess::ReadDouble(const char* section_name, const char* key_name) {
#ifdef WIN32
std::stringstream value;
double temp = 0;
GetPrivateProfileStringA(section_name, key_name, 0, text_buffer_, kMaxCharLength, file_path_char_);
value << text_buffer_; // input string
value >> temp; // return as double
// std::cout << text_buffer_;
return temp;
#else
UNUSED(text_buffer_);
return ini_reader_.GetReal(section_name, key_name, 0);
#endif
}
int IniAccess::ReadInt(const char* section_name, const char* key_name) {
#ifdef WIN32
int temp;
temp = GetPrivateProfileIntA(section_name, key_name, 0, file_path_char_);
return temp;
#else
return (int)ini_reader_.GetInteger(section_name, key_name, 0);
#endif
}
std::vector<int> IniAccess::ReadVectorInt(const char* section_name, const char* key_name, const size_t num) {
std::vector<int> data;
for (size_t i = 0; i < num; i++) {
std::stringstream edited_key_name;
edited_key_name << key_name << "(" << i << ")";
data.push_back(ReadInt(section_name, edited_key_name.str().c_str()));
}
return data;
}
bool IniAccess::ReadBoolean(const char* section_name, const char* key_name) {
#ifdef WIN32
int temp;
temp = GetPrivateProfileIntA(section_name, key_name, 0, file_path_char_);
if (temp > 0) {
return true;
}
return false;
#else
return ini_reader_.GetBoolean(section_name, key_name, false);
#endif
}
void IniAccess::ReadDoubleArray(const char* section_name, const char* key_name, const int id, const int num, double* data) {
for (int i = 0; i < num; i++) {
std::stringstream edited_key_name;
edited_key_name << key_name << id << "(" << i << ")";
data[i] = ReadDouble(section_name, edited_key_name.str().c_str());
}
}
std::vector<double> IniAccess::ReadVectorDouble(const char* section_name, const char* key_name, const size_t num) {
std::vector<double> data;
for (size_t i = 0; i < num; i++) {
std::stringstream edited_key_name;
edited_key_name << key_name << "(" << i << ")";
data.push_back(ReadDouble(section_name, edited_key_name.str().c_str()));
}
return data;
}
void IniAccess::ReadQuaternion(const char* section_name, const char* key_name, libra::Quaternion& data) {
libra::Quaternion temp;
double norm = 0.0;
for (int i = 0; i < 4; i++) { // Read Quaternion as new format
std::stringstream edited_key_name;
edited_key_name << key_name << "_(" << i << ")";
temp[i] = ReadDouble(section_name, edited_key_name.str().c_str());
norm += temp[i] * temp[i];
}
if (norm == 0.0) { // If it is not new format, try to read old format
for (int i = 0; i < 4; i++) {
std::stringstream edited_key_name;
edited_key_name << key_name << "(" << i << ")";
data[i] = ReadDouble(section_name, edited_key_name.str().c_str());
}
} else {
data[0] = temp[0];
data[1] = temp[1];
data[2] = temp[2];
data[3] = temp[3];
}
}
void IniAccess::ReadChar(const char* section_name, const char* key_name, const int size, char* data) {
#ifdef WIN32
GetPrivateProfileStringA(section_name, key_name, 0, data, size, file_path_char_);
#else
std::string string_data = ReadString(section_name, key_name);
strncpy(data, string_data.c_str(), size);
#endif
}
std::string IniAccess::ReadString(const char* section_name, const char* key_name) {
std::string value;
#ifdef WIN32
char temp[kMaxCharLength];
ReadChar(section_name, key_name, kMaxCharLength, temp);
value = std::string(temp);
#else
value = ini_reader_.GetString(section_name, key_name, "NULL");
#endif
// Special characters
// INI_FILE_DIR
std::string ini_path = INI_FILE_DIR_FROM_EXE;
value = std::regex_replace(value, std::regex("INI_FILE_DIR_FROM_EXE"), ini_path);
// EXT_LIB_DIR
std::string ext_lib_path = EXT_LIB_DIR_FROM_EXE;
value = std::regex_replace(value, std::regex("EXT_LIB_DIR_FROM_EXE"), ext_lib_path);
// CORE_DIR
std::string s2e_core_path = CORE_DIR_FROM_EXE;
value = std::regex_replace(value, std::regex("CORE_DIR_FROM_EXE"), s2e_core_path);
return value;
}
std::vector<std::string> IniAccess::ReadVectorString(const char* section_name, const char* key_name, const size_t num) {
std::vector<std::string> data;
for (size_t i = 0; i < num; i++) {
std::stringstream edited_key_name;
edited_key_name << key_name << "(" << i << ")";
data.push_back(ReadString(section_name, edited_key_name.str().c_str()));
}
return data;
}
bool IniAccess::ReadEnable(const char* section_name, const char* key_name) {
std::string enable_string = ReadString(section_name, key_name);
if (enable_string.compare("ENABLE") == 0) return true;
if (enable_string.compare("1") == 0) return true;
return false;
}
std::vector<std::string> IniAccess::ReadStrVector(const char* section_name, const char* key_name) {
std::vector<std::string> data;
std::string temp;
unsigned int i = 0;
while (true) {
std::stringstream c_name;
c_name << key_name << "(" << i << ")";
temp = ReadString(section_name, c_name.str().c_str());
#ifdef WIN32
if (temp.c_str()[0] == NULL) {
#else
if (!strcmp(temp.c_str(), "NULL")) {
#endif
break;
} else {
data.push_back(temp);
i++;
}
}
return data;
}
std::vector<std::string> IniAccess::Split(const std::string& input, const char delimiter) {
std::istringstream stream(input);
std::string field;
std::vector<std::string> result;
while (getline(stream, field, delimiter)) {
result.push_back(field);
}
return result;
}
void IniAccess::ReadCsvDouble(std::vector<std::vector<double>>& output_value, const size_t node_num) {
std::ifstream ifs(file_path_char_);
if (!ifs.is_open()) {
std::cerr << "file open error. filename = " << file_path_char_ << std::endl;
}
std::string line;
output_value.reserve(node_num);
while (getline(ifs, line)) {
std::vector<std::string> string_vector = Split(line, ',');
std::vector<double> temp;
temp.reserve(node_num);
for (size_t i = 0; i < string_vector.size(); i++) {
temp.push_back(std::stod(string_vector.at(i)));
}
output_value.push_back(temp);
}
}
void IniAccess::ReadCsvDoubleWithHeader(std::vector<std::vector<double>>& output_value, const size_t node_num, const size_t row_header_num,
const size_t column_header_num) {
std::ifstream ifs(file_path_char_);
if (!ifs.is_open()) {
std::cerr << "file open error. filename = " << file_path_char_ << std::endl;
}
std::string line;
size_t line_num = 0;
output_value.reserve(node_num);
while (getline(ifs, line)) {
if (line_num >= row_header_num) {
std::vector<std::string> string_vector = Split(line, ',');
std::vector<double> temp;
temp.reserve(node_num);
for (size_t i = 0; i < string_vector.size(); i++) {
if (i >= column_header_num) {
temp.push_back(std::stod(string_vector.at(i)));
}
}
output_value.push_back(temp);
}
line_num++;
}
}
void IniAccess::ReadCsvString(std::vector<std::vector<std::string>>& output_value, const size_t node_num) {
std::ifstream ifs(file_path_char_);
if (!ifs.is_open()) {
std::cerr << "file open error. filename = " << file_path_char_ << std::endl;
}
std::string line;
output_value.reserve(node_num);
while (getline(ifs, line)) {
std::vector<std::string> temp = Split(line, ',');
temp.reserve(node_num);
output_value.push_back(temp);
}
}