-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunch.cpp
263 lines (236 loc) · 7.93 KB
/
launch.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
#include <bits/stdc++.h>
#include <dirent.h>
// #include <iomanip.h>
#include <iostream>
#include <iomanip>
#include <sys/time.h>
#include <unistd.h>
#include "config.h"
#include "log.h"
#include "cal.h"
#define pb push_back
void init(config::Config config); // 初始化全局变量
std::vector<std::string> readFileList(const char *basePath);
cal::Cal calcu;
int main()
{
config::Config config;
std::vector<std::string> fileListAssembler;
std::vector<std::string> fileListParse;
std::vector<std::vector<function::Function> > parseAssemnlerList;
std::set<std::string> S;
std::string filePath;
std::string filePathAssembler;
std::string filePathParse;
std::fstream fileAns;
int fileAssemblerSize;
int fileParseSize;
int cmpCnt;
double averageTime;
// myLog::Log log(myLog::Log::INFO, "log.txt", myLog::Log::OVER);
std::cout << "start" << std::endl;
if(!config.ReadConfig("config.ini")) {
printf("read config.ini error\n");
exit(0);
}
// 初始化
init(config);
averageTime = 0;
cmpCnt = 0;
// 读取配置
filePath = config.ReadString("FILE_PATH", "");
filePathAssembler = config.ReadString("FILE_PATH_ASSEMBLER", "");
filePathParse = config.ReadString("FILE_PATH_PARSE", "");
// 计算
fileListAssembler = readFileList(filePathAssembler.c_str());
fileAssemblerSize = fileListAssembler.size();
for(int i = 0; i < fileAssemblerSize; i ++) {
std::string assemblerFileName;
std::string parseFileName;
assemblerFileName = filePathAssembler + fileListAssembler[i];
parseFileName = filePathParse + fileListAssembler[i];
parseFileName[parseFileName.size() - 1] = 'p'; // 随便拍脑袋想出来的后缀
function::parseAssembler(assemblerFileName.c_str(), parseFileName.c_str());
}
// 狗逼学生提交不能编译的代码,导致 parse 列表和 assembler 列表里数目不匹配
fileListParse = readFileList(filePathParse.c_str());
fileParseSize = fileListParse.size();
// 一次性读取文件到内存里,虽然丑了一点,但是防止多次读文件
for(int i = 0; i < fileParseSize; i ++) {
std::fstream fileIn;
std::string buff;
std::string parseFileName;
std::vector<function::Function> functionList;
parseFileName = filePathParse + fileListParse[i];
// std::cout << fileListParse[i] << std::endl;
fileIn.open(parseFileName.c_str(), std::ios::in);
while(getline(fileIn, buff)) {
function::Function func;
// std::cout << buff << std::endl;
if(buff[buff.size() - 1] == '>') {
S.insert(buff);
func.name = buff;
getline(fileIn, buff); // get len
func.len = std::stoi(buff);
for(int i = 0; i < func.len; i ++) {
getline(fileIn, buff);
func.V.pb(buff);
}
} else {
LogErr("the parse: %s struct err\n", parseFileName.c_str());
exit(1);
}
functionList.pb(func);
}
parseAssemnlerList.pb(functionList);
fileIn.close();
}
fileAns.open("ans", std::ios::out);
// solve porblem
// fileParseSize
for(int i = 0; i < 20; i ++) {
for(int j = i + 1; j < 20; j ++) {
double ansAToB;
double ansBToA;
double costTime;
struct timeval start;
struct timeval end;
gettimeofday(&start, NULL);
// fileAns << "cmp:" << i << " " << fileListParse[i].c_str() << " " << j << " " << fileListParse[j].c_str() << std::endl;
// printf("cmp: (%d %s) (%d %s)\n", i, fileListParse[i].c_str(), j, fileListParse[j].c_str());
// printf("size: %d %d\n", functionListA.size(), functionListB.size());
ansAToB = calcu.Solve(parseAssemnlerList[i], parseAssemnlerList[j]);
ansBToA = calcu.Solve(parseAssemnlerList[j], parseAssemnlerList[i]);
gettimeofday(&end, NULL);
costTime = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
averageTime += costTime / 1000;
cmpCnt ++;
fileAns << "result:"
<< fileListParse[i].c_str()
<< " "
<< fileListParse[j].c_str()
<< " ("
<< ansAToB
<< "%, "
<< ansBToA
<< "%) = "
<< std::max(ansAToB, ansBToA)
<< "% "
<< "cost time: "
<< std::fixed
<< std::setprecision(5)
<< costTime / 1000
<< std::endl;
}
}
// 记录函数列表,偶尔看看是否有遗漏的需删除函数
for(auto it : S) {
// std::cout << it << std::endl;
LogInfo("all function list: %s\n", it.c_str());
}
averageTime /= cmpCnt;
std::cout << "program over\n"
<< "average cost time: "
<< std::fixed
<< std::setprecision(5)
<< averageTime
<< "ms"
<< std::endl;
// for(int i = 0; i < fileSize; i ++) {
// int vSize;
// functionListA = function::parseAssembler(filePath + fileList[i], "wasteA");
// vSize = functionListA.size();
// for(int j = 0; j < vSize; j ++) {
// S.insert(functionListA[j].V[k]);
// }
// }
// fileOut.open("ans", std::ios::out);
// for(int i = 0; i < 20; i ++) {
// for(int j = i + 1; j < 20; j ++) {
// cal::Cal calAToB;
// cal::Cal calBToA;
// double ansAToB;
// double ansBToA;
// fileOut << "cmp:" << i << " " << fileList[i] << " " << j << " " << fileList[j] << std::endl;
// printf("cmp: (%d %s) (%d %s)\n", i, fileList[i].c_str(), j, fileList[j].c_str());
// functionListA = function::parseAssembler(filePath + fileList[i], "wasteA");
// functionListB = function::parseAssembler(filePath + fileList[j], "wasteB");
// printf("size: %d %d\n", functionListA.size(), functionListB.size());
// ansAToB = calAToB.Solve(functionListA, functionListB);
// ansBToA = calBToA.Solve(functionListB, functionListA);
// fileOut << "ans:" << fileList[i] << " " << fileList[j] << " " << ansAToB << " " << ansBToA << std::endl;
// printf("(%s %s) = (%lf %lf)\n", fileList[i].c_str(), fileList[j].c_str(), ansAToB, ansBToA);
// }
// }
// fileAIn = config.ReadString("fileAIn", "");
// fileAOut = config.ReadString("fileAOut", "");
// fileBIn = config.ReadString("fileBIn", "");
// fileBOut = config.ReadString("fileBOut", "");
// LogDebug("begin\n");
// printf("%lf\n", calAToB.Solve(functionListA, functionListB));
// printf("%lf\n", calBToA.Solve(functionListB, functionListA));
/* test log */
// int x;
// x = 3;
// LogInfo("%d\n", x);
// loli.logWrite(__FILE__, __LINE__, myLog::Log::INFO, "%d", x);
// #define LogInfo(M, ...) log.logWrite(__FILE__, __LINE__, Log::INFO, parseMessage(M))
/* test log */
/* test config */
// config::Config config;
// config.ReadConfig("config.ini");
// std::string HostName = config.ReadString("MYSQL", "HostName", "");
// int Port = config.ReadInt("MYSQL", "Port", 0);
// std::string UserName = config.ReadString("MYSQL", "UserName", "");
// std::cout << "HostName=" << HostName << std::endl;
// std::cout << "Port=" << Port << std::endl;
// std::cout << "UserName=" << UserName << std::endl;
/* test config */
return 0;
}
std::vector<std::string> readFileList(const char *basePath)
{
std::vector<std::string> res;
DIR *dir;
struct dirent *ptr;
char base[1000];
dir = opendir(basePath);
if(dir == NULL) {
perror("Open dir error...");
exit(1);
}
while((ptr=readdir(dir)) != NULL) {
if(strcmp(ptr -> d_name,".")==0 || !strcmp(ptr->d_name,"..")) { // current dir OR parrent dir
continue;
}
else if(ptr -> d_type == 8) { // file
res.pb(std::string(ptr -> d_name));
}
else if(ptr -> d_type == 10) { // link file
res.pb(std::string(ptr -> d_name));
}
else if(ptr -> d_type == 4) { // dir
memset(base,'\0',sizeof(base));
strcpy(base,basePath);
strcat(base,"/");
strcat(base, ptr -> d_name);
res.pb(std::string(ptr -> d_name));
readFileList(base);
}
}
closedir(dir);
return res;
}
void init(config::Config config) // 初始化全局变量
{
int logLevel;
int logType;
std::string logFile;
logLevel = config.ReadInt("LOG_LEVEL", -1);
logFile = config.ReadString("LOG_FILE", "");
logType = config.ReadInt("LOG_TYPE", -1);
myLog::loli.Init(myLog::Log::INFO, "log.txt", myLog::Log::OVER);
}
void parseRevealAllTheDetails()
{
}