-
Notifications
You must be signed in to change notification settings - Fork 1
/
Transformer.cpp
185 lines (175 loc) · 4.55 KB
/
Transformer.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
#include "Transformer.h"
#include "File.h"
using namespace bluemeiIO;
#include "WinFile.h"
#include "Time.h"
#include "windows.h"
//long GetCurrentDirectory(long nBufferLength,char* lpBuffer);
void Transformer::transformDirect(String dir)
{
String name;
long costTime=0;
if(dir==".")
{
char buffer[MAX_PATH];
GetCurrentDirectory(MAX_PATH,buffer);
dir=buffer;
}
WinFile reader(dir);
cout<<"即将开始转换目录["<<dir<<"]下源文件,请稍后..."<<endl;
while(reader.getNextFileName(name))
{
costTime+=transform(name,dir);
}
char time[20];
sprintf(time,"%d小时%d分%d秒",costTime/60/60,costTime/60%60,costTime%60);
cout<<endl<<"-------------------转换完毕(耗时:"<<time<<")-------------------"<<endl<<endl;//*/
}
long Transformer::transform(String& name,const String& dir)
{
String line,strTransformed,className;
int i,tmp;
bool bTranCpp=false,bExistNamespace=false,bToDeleteParentheses=false;
bool bAppendPublic=false,bAppendPrivate=false,bAppendProtected=false;
//name="WinFile.h";
name.trim();
if(!name.endOf(".h"))
return 0;
File file(dir+"\\"+name);
className=name;
className.replaceString(".h", "");
cout<<"即将转换:"<<className<<endl;
float percent,length=0;
long total=file.getSize();
time_t oldTime,currentTime,costTime;
time(&oldTime);
while((tmp=file.readLine(line))>0)
{
length+=tmp;//加上换行符?
//cout<<"正在转换: (done:"<<percent<<"%)"<<endl<<line<<" in "<<name<<endl;
printf("\r");
cout<<"正在转换: "<<name<<" (done:"<<percent<<"%)";
line.replaceString("#", "\n", "");
line.replaceString("using", "\n", "");
line.replaceString("#endif", "");
line.replaceString("\t", "");
line.trim();
line.replaceString(className+"::","");
line.replaceString("::","");
line.replaceString("->",".");
line.replaceString("class","public class");
line.replaceString("unsigned","");
line.replaceString("NULL","null");
line.replaceString("&","");
line.replaceString("bool","boolean");
line.replaceString("struct","class");
line.replaceString("delete","//delete");
line.replaceString("*p","p");
line.replaceString("*m_p","m_p");
line.replaceString("string","String");
line.replaceString("vector","ArrayList");
line.replaceString("*","");
line.replaceString("printf(","System.out.print(String.format(");
//删除Namespace
if(bToDeleteParentheses)
{
tmp=line.find("{");
if(tmp>=0)
line.erase(tmp,1);
}
bToDeleteParentheses=false;
if(!bExistNamespace)
{
bExistNamespace=line.replaceString("namespace", "\n", "");
bToDeleteParentheses=bExistNamespace;
}
//添加 public private protected
if((tmp=line.find("public:"))>=0)
{
bAppendPublic=bAppendPrivate=bAppendProtected=false;
bAppendPublic=true;
line.erase(tmp,strlen("public:"));
}
else if((tmp=line.find("private:"))>=0)
{
bAppendPublic=bAppendPrivate=bAppendProtected=false;
bAppendPrivate=true;
line.erase(tmp,strlen("private:"));
}
else if((tmp=line.find("protected:"))>=0)
{
bAppendPublic=bAppendPrivate=bAppendProtected=false;
bAppendProtected=true;
line.erase(tmp,strlen("protected:"));
}
line.trim();
tmp=line.length();
if(!bTranCpp && tmp>5)//int a;
{
tmp=line.find("//");
if(tmp!=0)
{
if(bAppendPublic)
line.insert(0,"public ");
else if(bAppendPrivate)
line.insert(0,"private ");
else if(bAppendProtected)
line.insert(0,"protected ");
}
}
//注释掉方法声明
tmp=line.rfind(");",line.length());
if(!bTranCpp && tmp>0)
{
tmp=line.find("//");
if(tmp!=0)
line.insert(0,"//");
}
strTransformed.append(line);
percent=100*length/total;
//转换cpp
if(percent==100 && !bTranCpp)
{
file.close();
bTranCpp=true;
//去除namespace 存在
if(bExistNamespace)
{
tmp=strTransformed.find_last_of('}');
if(tmp>0)
strTransformed.erase(tmp,1);
}
tmp=strTransformed.rfind("};", strTransformed.length());
if(tmp>0)
{
strTransformed.erase(tmp,strlen("};"));
}
//打开cpp文件
name.replaceString(".h",".cpp");
try
{
file.openFile(dir+"\\"+name, "r");//file=File(dir+"\\"+name)为何出错?
length=0;
total=file.getSize();
}catch(Exception e)
{
e.printException();
break;
}
}
}
strTransformed.append("}");
printf("\r");
for(i=0; i<50;i++)
printf(" ");
printf("\r");
cout<<"转换完成: (done:"<<percent<<"%)"<<endl;
cout<<"正在保存..."<<endl;
className+=".java";
File fileSave(dir+"\\java\\"+className,"w+");
fileSave.writeBytes(strTransformed.c_str(),strTransformed.length());
time(¤tTime);
costTime=currentTime-oldTime;
//difftime(currentTime,oldTime)
return costTime;
}