forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatSource.java
More file actions
203 lines (200 loc) · 5.36 KB
/
Copy pathFormatSource.java
File metadata and controls
203 lines (200 loc) · 5.36 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
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
package javaforce.utils;
/** Format Source code to assist in Java to C# conversion.
*
* @author pquiring
*/
import java.io.*;
public class FormatSource {
public static void main(String[] args) {
new FormatSource().run(args);
}
public void run(String[] args) {
if (args.length == 0) {
System.out.println("usage:FormatSource folder");
System.exit(0);
}
doFolder(args[0]);
}
public boolean isID(byte ch) {
if ((ch >= 'a') && (ch <= 'z')) return true;
if ((ch >= 'A') && (ch <= 'Z')) return true;
if ((ch >= '0') && (ch <= '9')) return true;
if (ch == '_') return true;
return false;
}
public boolean isType(byte[] data, int offset, int length) {
char first = (char)data[offset];
if ((first >= 'A') && (first <= 'Z')) return true; //assume capitalized name is a type
String type = new String(data, offset, length);
switch (type) {
case "byte": return true;
case "short": return true;
case "int": return true;
case "long": return true;
case "float": return true;
case "double": return true;
case "boolean": return true;
case "char": return true;
}
return false;
}
public int doSource(byte[] data) {
//type name[] -> type[] name
int count = 0;
int offset = 0;
int line = 1;
boolean inMultiComment = false, inSingleComment = false;
boolean inDoubleQuote = false, inSingleQuote = false;
while (offset < data.length) {
if (inMultiComment) {
if (data[offset] == '\n') {
line++;
offset++;
continue;
}
if (data[offset] == '*' && data[offset + 1] == '/') {
inMultiComment = false;
offset += 2;
continue;
}
offset++;
continue;
}
if (inSingleComment) {
if (data[offset] == '\n') {
line++;
offset++;
inSingleComment = false;
continue;
}
offset++;
continue;
}
if (inDoubleQuote) {
if (data[offset] == '\\' && data[offset + 1] == '"') {
offset += 2;
continue;
}
if (data[offset] == '"') inDoubleQuote = false;
offset++;
continue;
}
if (inSingleQuote) {
if (data[offset] == '\\' && data[offset + 1] == '\'') {
offset += 2;
continue;
}
if (data[offset] == '\'') inSingleQuote = false;
offset++;
continue;
}
if (data[offset] == '/' && data[offset + 1] == '*') {
inMultiComment = true;
offset += 2;
continue;
}
if (data[offset] == '/' && data[offset + 1] == '/') {
inSingleComment = true;
offset += 2;
continue;
}
if (data[offset] == '"') {
inDoubleQuote = true;
offset++;
continue;
}
if (data[offset] == '\'') {
inSingleQuote = true;
offset++;
continue;
}
int length = 0;
while (data[offset + length] == '[' && data[offset + length + 1] == ']') {
length += 2;
}
if (length > 0) {
int nameStart, nameLength;
//scan back to name id
nameStart = offset - 1;
nameLength = 0;
while (data[nameStart] == ' ') {
nameStart--;
}
while (isID(data[nameStart])) {
nameStart--;
nameLength++;
}
nameStart++;
if (nameLength > 0 && !isType(data, nameStart, nameLength)) {
//scan back to type id
int typeStart, typeLength;
typeStart = nameStart - 1;
typeLength = 0;
while (data[typeStart] == ' ') {
typeStart--;
}
while (isID(data[typeStart])) {
typeStart--;
typeLength++;
}
typeStart++;
if (typeLength > 0) {
//type name[] -> type[] name
int src = typeStart + typeLength;
int dst = src + length;
System.arraycopy(data, src, data, dst, offset - src);
int pairs = length / 2;
for(int a=0;a<pairs;a++) {
data[src++] = '[';
data[src++] = ']';
}
count++;
}
}
offset += length;
} else {
if (data[offset] == '\n') {
line++;
}
offset++;
}
}
return count;
}
public void doFile(String file) {
System.out.println("doFile:" + file);
try {
FileInputStream fis = new FileInputStream(file);
byte[] data = fis.readAllBytes();
fis.close();
int count = doSource(data);
if (count == 0) return;
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.close();
System.out.println("Fixed " + count + " arrays in " + file);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public void doFolder(String folder) {
System.out.println("doFolder:" + folder);
File[] files = new File(folder).listFiles();
for(File file : files) {
if (file.isDirectory()) {
try {
doFolder(file.getCanonicalPath());
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
} else {
String name = file.getAbsolutePath();
if (name.endsWith(".java")) {
doFile(name);
}
}
}
}
}