METARESULT_GEN.cpp
5.14 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
// METARESULT_GEN.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <WINDOWS.H>
using namespace std;
std::string& trim(std::string &s)
{
if (s.empty())
{
return s;
}
int pos = s.find_first_not_of(" ");
if (pos != -1)
{
s.erase(0,pos);
}
pos = s.find_first_of(" ");
if (pos != -1)
{
s.erase(pos, s.length());
}
return s;
}
int main(int argc, char* argv[])
{
char folderPath[] = ".\\";
char filePath[260] = {0};
HANDLE hFind;
WIN32_FIND_DATA FindFileData;
// prescan
//sprintf(find_path, "%s*Pass*.txt", m_log_folder);
sprintf(filePath, "%s*.h", folderPath);
hFind = FindFirstFile(filePath, &FindFileData);
if( hFind != INVALID_HANDLE_VALUE )
{
cout << FindFileData.cFileName << endl;
}
else
{
cout << "Can`t find .h file" << endl;
return 0;
}
FindClose(hFind);
string filename(FindFileData.cFileName); // 输入文件
char *pStartKeyword = "META_SUCCESS";
char *pEndKeyword = "META_LAST_RESULT";
ifstream in(filename.c_str());
if (in)
{
ostringstream os; // 加载文件到sstream流
os << in.rdbuf();
string content(os.str()); // sstream流 转换成 字符串类
// 查找关键字,第一次出现的位置, 在文件的第 X 行
string::size_type pos_start = content.find(pStartKeyword);
string::size_type pos_end = content.find(pEndKeyword);
string::size_type rtLength = 0;
string strResult = content.substr(pos_start, pos_end - pos_start + strlen(pEndKeyword));
rtLength = strResult.length();
//cout << strResult << endl;
//cout << "rtLength = " << rtLength << endl;
std::string::iterator it = strResult.begin(); // 指向字符串开始的迭代器
pos_start = 0;
pos_end = strResult.find(pEndKeyword);
int rtCount = count(it, it + pos_end, ',') + 1; // 计算MetaResult个数
cout << "Meta result Count = " << rtCount << endl;
ofstream fileOut("Gen_MetaResult.txt");
fileOut<<"#define M2S(x) #x" << endl;
char strOut[1024] = {0};
pos_end = strResult.find("\n");
string::size_type curPos = pos_end+1;//remove '\n'
if (strnicmp(FindFileData.cFileName, "meta.h", strlen("meta.h")) == 0)
{
string strTmp = strResult.substr(pos_start, pos_end - pos_start);
sprintf(strOut, "case %s:\n\t return M2S(%s);\n\n", "META_SUCCESS", "META_SUCCESS");
cout << strOut;
fileOut<<strOut;
for (int i = 0; i < rtCount - 1; i++)
{
strResult = strResult.substr(curPos, -1);
pos_start = strResult.find(",");
pos_end = strResult.find("\n");
while (pos_end < pos_start)
{
curPos = pos_end + 1;//remove '\n'
strResult = strResult.substr(curPos, -1);
pos_start = strResult.find(",");
pos_end = strResult.find("\n");
}
strTmp = strResult.substr(pos_start+1, pos_end-pos_start-1);
//去掉首尾空格
strTmp = trim(strTmp);
curPos = pos_end+1;
sprintf(strOut, "case %s:\n\t return M2S(%s);\n\n", strTmp.c_str(), strTmp.c_str());
fileOut<<strOut;
cout << strOut;
}
}
else if (strnicmp(FindFileData.cFileName, "spmeta.h", strlen("spmeta.h")) == 0)
{
string strSPMetadll = "SPMETA_DLL::";
string strTmp = strResult.substr(pos_start, pos_end - pos_start);
sprintf(strOut, "case %s:\n\t return M2S(%s);\n\n", "SPMETA_DLL::META_SUCCESS", "SPMETA_DLL::META_SUCCESS");
cout << strOut;
fileOut<<strOut;
for (int i = 0; i < rtCount - 1; i++)
{
strResult = strResult.substr(curPos, -1);
pos_start = strResult.find(",");
pos_end = strResult.find("\n");
while (pos_end < pos_start)
{
curPos = pos_end + 1;//remove '\n'
strResult = strResult.substr(curPos, -1);
pos_start = strResult.find(",");
pos_end = strResult.find("\n");
}
strTmp = strResult.substr(pos_start+1, pos_end-pos_start-1);
//去掉首尾空格
strTmp = trim(strTmp);
curPos = pos_end+1;
sprintf(strOut, "case SPMETA_DLL::%s:\n\t return M2S(SPMETA_DLL::%s);\n\n", strTmp.c_str(), strTmp.c_str());
fileOut<<strOut;
cout << strOut;
}
}
fileOut.close();
cout << "Gen meta result succss!!" << endl;
}
else
{
cerr << "Open file error!" << endl;
return -1;
}
return 0;
}