-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
335 lines (278 loc) · 9.2 KB
/
main.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// ExcelModule.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
#include "excelmodule.h"
#include <basicexcel.h>
#include <model/peakoutput.h>
#include <boost/format.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/shared_ptr.hpp>
typedef boost::shared_ptr<PeakOutput> PeakOutputPtr;
namespace
{
const int START_ROW = 0;
const char* SHEET_NAME = "TIR_Results";
std::string cell(int row, int col)
{
if (row < 0 || col < 0)
{
return "a1";
}
std::string value = boost::str(boost::format("%s%d") % (char)(97 + col) % (row + 1));
return value;
}
std::string narrow_string1(const std::wstring& str)
{
std::string ret;
if (!str.empty())
{
ret.resize(str.length());
typedef std::ctype<wchar_t> CT;
CT const& ct = std::_USE(std::locale(), CT);
ct.narrow(&str[0], &*str.begin() + str.size(), '?', &ret[0]);
}
return ret;
}
std::wstring widen_string(const std::string& str)
{
std::wstring ret;
if (!str.empty())
{
ret.resize(str.length());
typedef std::ctype<wchar_t> CT;
CT const& ct = std::_USE(std::locale(), CT);
ct.widen(&str[0], &*str.begin() + str.size(), &ret[0]);
}
return ret;
}
}
void displayHelp();
bool readFromFile(const _TCHAR* fileName, std::vector<PeakOutput>& pks, std::vector<std::vector<PeakOutputPtr> >& peaks)
{
ifstream fin(fileName, std::ios_base::in);
if (fin.fail()) return false;
// read the number of peak
std::string strNumPeak;
std::string strNumRawData;
fin >> strNumPeak;
fin >> strNumRawData;
int numPeak = boost::lexical_cast<int>(strNumPeak);
int numRawData = boost::lexical_cast<int>(strNumRawData);
if (numPeak <= 0 || numPeak >= 1e4 || numRawData <= 0 || numRawData >= 1e4)
{
return false;
}
std::string m_peak;
std::string m_peakToPeak;
std::string m_effectivePeak;
std::string m_averagePeak;
std::string m_minPeak;
for(int i = 0; i<numPeak; ++i)
{
PeakOutput data;
fin >> m_peak >> m_peakToPeak >> m_effectivePeak >> m_averagePeak >> m_minPeak;
try
{
data.m_peak = boost::lexical_cast<double>(m_peak);
data.m_peakToPeak = boost::lexical_cast<double>(m_peakToPeak);
data.m_effectivePeak = boost::lexical_cast<double>(m_effectivePeak);
data.m_averagePeak = boost::lexical_cast<double>(m_averagePeak);
data.m_minPeak = boost::lexical_cast<double>(m_minPeak);
}
catch(boost::bad_lexical_cast& e)
{
return false;
}
catch(...)
{
return false;
}
pks.push_back(data);
}
for(int i = 0; i<numPeak; ++i)
{
std::vector<PeakOutputPtr> pk2;
for(int j = 0; j<numRawData; ++j)
{
PeakOutputPtr data = boost::make_shared<PeakOutput>();
fin >> m_peak >> m_peakToPeak >> m_effectivePeak >> m_averagePeak >> m_minPeak;
try
{
data->m_peak = boost::lexical_cast<double>(m_peak);
data->m_peakToPeak = boost::lexical_cast<double>(m_peakToPeak);
data->m_effectivePeak = boost::lexical_cast<double>(m_effectivePeak);
data->m_averagePeak = boost::lexical_cast<double>(m_averagePeak);
data->m_minPeak = boost::lexical_cast<double>(m_minPeak);
}
catch(boost::bad_lexical_cast& e)
{
return false;
}
catch(...)
{
return false;
}
pk2.push_back(data);
}
peaks.push_back(pk2);
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
if (argc < 5)
{
return 0;
}
bool toAppend = (std::wstring(argv[3]) == L"true");
bool isExist = (std::wstring(argv[4]) == L"true");
std::wstring tmpFile3(argv[1]);
std::string tmpFile(narrow_string1(tmpFile3));
std::vector<PeakOutput> pks;
std::vector<std::vector<PeakOutputPtr> > peaks;
bool status = readFromFile(argv[2], pks, peaks);
if (!status)
{
return 0;
}
ExcelModule excel;
int sheetIndex = 1;
int startRow = 0;
if (toAppend)
{
bool hasSheet = false;
{
YExcel::BasicExcel workbook;
workbook.Load(tmpFile.c_str());
YExcel::BasicExcelWorksheet* ws = workbook.GetWorksheet(SHEET_NAME);
excel.getExcelSheetIndex(SHEET_NAME, sheetIndex);
if (sheetIndex >= 1)
{
hasSheet = true;
}
else
{
sheetIndex = 1;
}
if (ws != 0) // find the starting row to append the new content
{
for (size_t row = 0; row < 5e4; ++row)
{
int v = ws->Cell(START_ROW + 1 + row, 0)->GetInteger();
if (v < 1)
{
break;
}
startRow = v;
}
}
}
excel.openExcelBook(tmpFile);
if (!hasSheet)
{
excel.setExcelSheetName(sheetIndex, SHEET_NAME);
excel.setExcelValue(cell(START_ROW, 1), "Avg.TIR", false, 1);
excel.setExcelValue(cell(START_ROW, 2), "Min.TIR", false, 1);
excel.setExcelValue(cell(START_ROW, 3), "Max.TIR", false, 1);
excel.setExcelValue(cell(START_ROW, 4), "RPM", false, 1);
}
}
else
{
if (!isExist)
{
excel.openExcelBook(tmpFile);
}
else
{
int totalrows = -1, totalcolumns = -1;
{
YExcel::BasicExcel workbook;
workbook.Load(tmpFile.c_str());
YExcel::BasicExcelWorksheet* ws = workbook.GetWorksheet(SHEET_NAME);
if (ws != 0)
{
totalrows = ws->GetTotalRows();
totalcolumns = max(15, ws->GetTotalCols());
}
}
excel.openExcelBook(tmpFile);
if (totalcolumns > 0 && totalrows > 0)
{
for (int row = 0; row < totalrows; ++row)
{
for (int col = 0; col < totalcolumns; ++col)
{
excel.setExcelValue(cell(row, col), "", false, 1);
}
}
}
}
excel.setExcelSheetName(sheetIndex, SHEET_NAME);
excel.setExcelValue(cell(START_ROW, 1), "Avg.TIR", false, 1);
excel.setExcelValue(cell(START_ROW, 2), "Min.TIR", false, 1);
excel.setExcelValue(cell(START_ROW, 3), "Max.TIR", false, 1);
excel.setExcelValue(cell(START_ROW, 4), "RPM", false, 1);
}
if (peaks.size() != pks.size())
{
return 0;
}
int mxcell = 0;
for (size_t row = 0; row < pks.size(); ++row)
{
int rows = startRow + START_ROW + 1 + row;
//ws->Cell(rows,0)->SetDouble(rows);
excel.setExcelValue(sheetIndex, cell(rows, 0), boost::str(boost::format("%d") % rows), true, 1);
// TODO this is a hack on pk side -- should use a separate result model..
//ws->Cell(rows, 1)->SetDouble(pks[row].m_peakToPeak);
excel.setExcelValue(sheetIndex, cell(rows, 1), boost::str(boost::format("%.4f") % pks[row].m_peakToPeak), true, 1);
//ws->Cell(rows, 2)->SetDouble(pks[row].m_averagePeak);
excel.setExcelValue(sheetIndex, cell(rows, 2), boost::str(boost::format("%.4f") % pks[row].m_averagePeak), true, 1);
//ws->Cell(rows, 3)->SetDouble(pks[row].m_effectivePeak);
excel.setExcelValue(sheetIndex, cell(rows, 3), boost::str(boost::format("%.4f") % pks[row].m_effectivePeak), true, 1);
//ws->Cell(rows, 4)->SetDouble(pks[row].m_peak);
excel.setExcelValue(sheetIndex, cell(rows, 4), boost::str(boost::format("%.4f") % pks[row].m_peak), true, 1);
std::vector<PeakOutputPtr> raw = peaks.at(row);
size_t rs = raw.size();
for (size_t column = 0; column < rs; ++column)
{
if (column < rs)
{
//ws->Cell(rows, 5+column)->SetDouble(raw[column]->m_peakToPeak);
excel.setExcelValue(sheetIndex, cell(rows, 5 + column),
boost::str(boost::format("%.4f") % raw[column]->m_peakToPeak), true, 1);
}
else
{
//ws->Cell(rows, 5+column)->EraseContents(); // clear the contents.
excel.setExcelValue(sheetIndex, cell(rows, 5 + column), "", false, 1);
}
if (mxcell < column)
{
mxcell = column;
}
}
}
for (int j = 0; j < mxcell + 1; ++j)
{
//ws->Cell(START_ROW, 5+j)->SetString("TIR");
excel.setExcelValue(sheetIndex, cell(START_ROW, 5 + j), "TIR", false, 1);
}
if (isExist)
{
excel.save();
}
else
{
excel.saveAs(tmpFile.c_str());
}
DeleteFile(argv[2]);
return 0;
}