-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkrkr2local.cpp
369 lines (349 loc) · 9.7 KB
/
krkr2local.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const char CR = 0x0D;
const char LF = 0x0A;
const char SPACE = 0x20;
const int MAX_FILE_LENGTH = 1000000;
char file1[MAX_FILE_LENGTH], file2[MAX_FILE_LENGTH];
bool mark[MAX_FILE_LENGTH];
void print_help()
{
cout << "usage: krkr2local [options] [files]" << endl;
cout << endl;
cout << "options:" << endl;
cout << "-h or -help" << "\n\t" << "Print help." << endl;
cout << "-ph or -printhex [file]" << "\n\t" << "Print target file in hex." << endl;
cout << "-dr or -deleteruby [input] [output]" << "\n\t" << "Delete ruby texts in [input]'s scripts." << endl;
cout << "-ef or -eraseformat [input] [output]" << "\n\t" << "Erase all formatting marks in [input], generate [output] containing only plain text." << endl;
cout << "-ds or -deletespace [input] [output]" << "\n\t" << "Delete spaces and empty lines in [input]." << endl;
cout << "-r or -replace [format] [newtext] [output]" << "\n\t" << "Replace all plain text in [format] with the corresponding text in [newtext]." << endl;
return;
}
void print_char_in_hex(char c)
{
int u = (unsigned char)c;
int first = u / 16,second = u % 16;
if (first < 10) cout << first;
else cout << (char)('A' + first - 10);
if (second < 10) cout << second;
else cout << (char)('A' + second - 10);
return;
}
void print_file_in_hex(const char file_name[])
{
FILE *fp = fopen(file_name,"rb");
if (fp == NULL) {cout << "Unable to open "<< file_name << endl; return;}
else cout << file_name << " opened successfully." << endl;
char row[16];
int cnt = 0;
while (fread(row,1,16,fp) == 16)
{
cnt++;
for (int i = 0;i < 16;i++)
{
print_char_in_hex(row[i]);
putchar(' ');
}
for (int i = 0;i < 16;i++)
{
if (row[i] != '\r' && row[i] != '\n' && row[i] >= 0x20 && row[i] <= 0x7E) printf("%c ",row[i]);
else if (row[i] == '\r') printf("\\r ");
else if (row[i] == '\n') printf("\\n ");
else if (row[i] < 0x20 || row[i] > 0x7E) printf("?? ");
else return;
}
cout << endl;
}
cout << "total char: " << cnt * 16 << endl;
fclose(fp);
return;
}
void delete_ruby(const char jp_file_name[], const char jp_noruby_file_name[], char jpfile[], bool deleted[])
{
const char ruby[11] = "ruby text=";
FILE *jp_fp = fopen(jp_file_name,"rb");
if (jp_fp == NULL) {cout << "Unable to open "<< jp_file_name << endl; return;}
else cout << jp_file_name << " opened successfully." << endl;
FILE *jp_noruby_fp = fopen(jp_noruby_file_name,"wb");
if (jp_noruby_fp == NULL) {cout << "Unable to open " << jp_noruby_file_name << endl; return;}
else cout << jp_noruby_file_name << " opened successfully." << endl;
int cnt = 0;
memset(jpfile,0,sizeof(*jpfile));
memset(deleted,0,sizeof(*deleted));
while (fread(&jpfile[cnt],1,1,jp_fp) == 1) cnt++;
cout << "Read complete, total text = " << cnt << endl;
int ruby_cnt = 0;
for (int i = 0;i < cnt;i++)
{
bool flag = true;
for (int j = 0;j < 10;j++)
{
if ((int)jpfile[i + j] != (int)ruby[j])
{
flag = false;
break;
}
}
if (flag)
{
ruby_cnt++;
int del_l = i,del_r = i;
while (jpfile[del_l] != '[') del_l--;
while (jpfile[del_r] != ']') del_r++;
for (int k = del_l;k <= del_r;k++)
{
deleted[k] = true;
}
}
}
cout << "Ruby text count = " << ruby_cnt << endl;
int noruby_cnt = 0;
for (int i = 0;i < cnt;i++)
{
if (!deleted[i])
{
fputc(jpfile[i],jp_noruby_fp);
noruby_cnt++;
}
}
cout << "Total noruby text = " << noruby_cnt << endl;
fclose(jp_fp);
fclose(jp_noruby_fp);
return;
}
void erase_format(const char jp_file_name[], const char jp_plain_file_name[], char jpfile[])
{
FILE *jp_fp = fopen(jp_file_name,"rb");
if (jp_fp == NULL) {cout << "Unable to open " << jp_file_name << endl; return;}
else cout << jp_file_name << " opened successfully." << endl;
FILE *jp_plain_fp = fopen(jp_plain_file_name,"wb");
if (jp_plain_fp == NULL) {cout << "Unable to open " << jp_plain_file_name << endl; return;}
else cout << jp_plain_file_name << " opened successfully." << endl;
int cnt = 0;
int plain_cnt = 0;
memset(jpfile,0,sizeof(*jpfile));
while (fread(&jpfile[cnt],1,1,jp_fp) == 1) cnt++;
cout << "Read complete, total formatted text = " << cnt << endl;
bool in_plain = false;
for (int i = 0;i < cnt;)
{
if (jpfile[i] == '[')
{
if (in_plain)
{
in_plain = false;
fputc(CR,jp_plain_fp);
fputc(LF,jp_plain_fp);
}
while (jpfile[i] != ']') i++;
i++;
}
else if (jpfile[i] == ';' || jpfile[i] == '*')
{
if (in_plain)
{
in_plain = false;
fputc(CR,jp_plain_fp);
fputc(LF,jp_plain_fp);
}
while (jpfile[i] != '\r') i++;
}
else if (jpfile[i] == '\r' || jpfile[i] == '\n')
{
if (in_plain)
{
in_plain = false;
fputc(CR,jp_plain_fp);
fputc(LF,jp_plain_fp);
}
i++;
}
else
{
in_plain = true;
plain_cnt++;
fputc(jpfile[i],jp_plain_fp);
i++;
}
}
cout << "Total plain text = " << plain_cnt << endl;
fclose(jp_fp);
fclose(jp_plain_fp);
return;
}
void delete_space(const char cn_file_name[], const char cn_nospace_file_name[], char cnfile[])
{
FILE *cn_fp = fopen(cn_file_name,"rb");
if (cn_fp == NULL) {cout << "Unable to open " << cn_file_name << endl; return;}
else cout << cn_file_name << " opened successfully." << endl;
FILE *cn_nospace_fp =fopen(cn_nospace_file_name,"wb");
if (cn_nospace_fp == NULL) {cout << "Unable to open " << cn_nospace_file_name << endl; return;}
else cout << cn_nospace_file_name << " opened successfully." << endl;
int cnt = 0, nospace_cnt = 0;
while (fread(&cnfile[cnt],1,1,cn_fp) == 1) cnt++;
cout << "Read complete, total cn text = " << cnt << endl;
for (int i = 0;i < cnt;i++)
{
if (cnfile[i] == SPACE)
{
continue;
}
if (cnfile[i] == CR || cnfile[i] == LF)
{
int j = i;
bool allCRLFSPACE = true;
while (j >= 0)
{
j--;
if (cnfile[j] == cnfile[i]) break;
if (cnfile[j] == CR || cnfile[j] == LF || cnfile[j] == SPACE) continue;
else
{
allCRLFSPACE = false;
break;
}
}
if (allCRLFSPACE) continue;
else
{
nospace_cnt++;
fputc(cnfile[i],cn_nospace_fp);
continue;
}
}
else
{
nospace_cnt++;
fputc(cnfile[i],cn_nospace_fp);
continue;
}
}
cout << "Total nospace text = " << nospace_cnt << endl;
fclose(cn_fp);
fclose(cn_nospace_fp);
}
void replace(const char jp_file_name[], const char cn_plain_file_name[], const char cn_file_name[], char jpfile[], char cnfile[])
{
FILE *jp_fp,*cn_plain_fp,*cn_fp;
jp_fp = fopen(jp_file_name,"rb");
if (jp_fp == NULL) cout << "Unable to open " << jp_file_name << endl;
else cout << jp_file_name << " opened successfully." << endl;
cn_plain_fp = fopen(cn_plain_file_name,"rb");
if (cn_plain_fp == NULL) cout << "Unable to open " << cn_plain_file_name << endl;
else cout << cn_plain_file_name << " opened successfully." << endl;
cn_fp = fopen(cn_file_name,"wb");
if (cn_fp == NULL) cout << "Unable to open " << cn_file_name << endl;
else cout << cn_file_name << " opened successfully." << endl;
int jp_cnt = 0;
int cn_cnt = 0;
while (fread(&jpfile[jp_cnt],1,1,jp_fp) == 1) jp_cnt++;
cout << "Read complete, total jp text = " << jp_cnt << endl;
while (fread(&cnfile[cn_cnt],1,1,cn_plain_fp) == 1) cn_cnt++;
cout << "Read complete, total cn text = " << cn_cnt << endl;
int j = 0;
int cn_fp_cnt = 0;
for (int i = 0;i < jp_cnt;)
{
if (jpfile[i] == '[')
{
while (jpfile[i] != ']')
{
fputc(jpfile[i],cn_fp);
cn_fp_cnt++;
i++;
}
fputc(jpfile[i],cn_fp);
cn_fp_cnt++;
i++;
}
else if (jpfile[i] == ';' || jpfile[i] == '*')
{
while (jpfile[i] != '\r')
{
fputc(jpfile[i],cn_fp);
cn_fp_cnt++;
i++;
}
}
else if (jpfile[i] == '\r' || jpfile[i] == '\n')
{
fputc(jpfile[i],cn_fp);
cn_fp_cnt++;
i++;
}
else
{
while (jpfile[i] != '[' && jpfile[i] != ';' && jpfile[i] != '*' && jpfile[i] != '\r' && jpfile[i] != '\n')
{
i++;
}
while (cnfile[j] != '\r' && cnfile[j] != '\n')
{
fputc(cnfile[j],cn_fp);
cn_fp_cnt++;
j++;
}
j++; //CR
j++; //LF
}
}
cout << "Total cn text in format = " << cn_fp_cnt << endl;
fclose(jp_fp);
fclose(cn_plain_fp);
fclose(cn_fp);
}
int main(int argc, char* argv[])
{
//system("chcp 65001");
if (argc <= 1) print_help();
else if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0) print_help();
else if (strcmp(argv[1], "-ph") == 0 || strcmp(argv[1], "-printhex") == 0)
{
if (argc != 3) print_help();
else
{
const char* file = argv[2];
print_file_in_hex(file);
}
}
else if (strcmp(argv[1], "-dr") == 0 || strcmp(argv[1], "-deleteruby") == 0)
{
if (argc != 4) print_help();
else
{
const char *input = argv[2], *output = argv[3];
delete_ruby(input,output,file1,mark);
}
}
else if (strcmp(argv[1], "-ef") == 0 || strcmp(argv[1], "-eraseformat") == 0)
{
if (argc != 4) print_help();
else
{
const char *input = argv[2], *output = argv[3];
erase_format(input,output,file1);
}
}
else if (strcmp(argv[1], "-ds") == 0 || strcmp(argv[1], "-deletespace") == 0)
{
if (argc != 4) print_help();
else
{
const char *input = argv[2], *output = argv[3];
delete_space(input,output,file1);
}
}
else if (strcmp(argv[1], "-r") == 0 || strcmp(argv[1], "-replace") == 0)
{
if (argc != 5) print_help();
else
{
const char *format = argv[2], *new_text = argv[3], *output = argv[4];
replace(format,new_text,output,file1,file2);
}
}
return 0;
}