-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainUnit.cpp
More file actions
432 lines (325 loc) · 12.1 KB
/
MainUnit.cpp
File metadata and controls
432 lines (325 loc) · 12.1 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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include<hgl/type/StringList.h>
#include<hgl/filesystem/FileSystem.h>
#include<hgl/filesystem/EnumFile.h>
#include<hgl/io/FileOutputStream.h>
#include<hgl/io/TextOutputStream.h>
#if HGL_OS == HGL_OS_Windows
#include<wchar.h>
#define cmd_out std::wcout
#else
#define cmd_out std::cout
#endif//
using namespace hgl;
using namespace hgl::io;
using namespace hgl::filesystem;
static OSStringList convert_ext_list; //要转换的文件
static OSStringList discard_file_list; //要抛弃的文件
static OSStringList discard_folder_list; //要抛弃的目录
static OSString in_folder_base;
static OSString out_folder_base;
static bool fn_japanese_hw2fw=false;
static bool fn_english_fw2hw=false;
static bool japanese_hw2fw=false;
static bool english_fw2hw=false;
static bool sub_folder=false;
static bool include_bom=true;
static CharSet in_charset;
static CharSet out_charset;
void ConvertQtoB(u16char *str,int len)
{
constexpr u16char QC[]=U16_TEXT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890");
constexpr u16char BC[]=U16_TEXT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890");
const int tl=sizeof(QC)/sizeof(u16char)-1;
u16char *p=str;
while(len--)
{
for(int i=0;i<tl;i++)
{
if(*p==QC[i])
{
*p=BC[i];
break;
}
}
p++;
}
}
bool ExtNameCheck(const os_char *ext_name)
{
const int len=strlen(ext_name);
for(int i=0;i<convert_ext_list.GetCount();i++)
{
const OSString &fn=convert_ext_list[i];
if(fn.Length()!=len)continue;
if(fn.CaseComp(ext_name)==0)
return(true);
}
return(false);
}
//未来优化内容:
// 1.建立永久性的文件缓冲区,避每个文件都分配、释放内存。可提升性能.
// 评估:现在已经很快了,不想做了。留给有心人做吧!
template<typename TOS> void Convert(const OSString &in_filename,const OSString &out_filename)
{
char *old_text;
union
{
char *u8_text;
u16char *u16_text;
}in_text;
const int64 old_size=LoadFileToMemory(in_filename,(void **)&old_text);
int64 in_size;
if(old_size<=0)return;
const BOMFileHeader *bom=ParseBOM(old_text);
CharSet in_cs;
if(bom) //有BOM的
{
BOM2CharSet(&in_cs,bom);
if(in_cs==out_charset) //完全一样
{
cmd_out<<OS_TEXT("[OK][COPY] filename: ")<<out_filename.c_str()<<std::endl;
SaveMemoryToFile(out_filename,old_text,old_size); //直接保存
delete[] old_text;
return;
}
in_text.u8_text=old_text+bom->size;
in_size=old_size-bom->size;
}
else
{
in_cs=in_charset;
in_text.u8_text=old_text;
in_size=old_size;
}
FileOutputStream fos;
if(!fos.CreateTrunc(out_filename))
{
delete[] old_text;
cmd_out<<OS_TEXT("[ERR] Create file failed: ")<<out_filename.c_str()<<std::endl;
return;
}
bool result=false;
TOS tos(&fos);
if(include_bom)
tos.WriteBOM();
if(in_cs==out_charset) // in/out一样,但走这里,代表它原本是没有BOM头的
{
result=tos.WriteChars(old_text,old_size); //直接写入原始数据
}
else
if(in_cs==utf8_charset)
{
result=tos.WriteChars(in_text.u8_text,in_size);
}
else
if((in_cs==utf16be_charset&&out_charset==utf16le_charset)
||(in_cs==utf16le_charset&&out_charset==utf16be_charset)) //16位互转
{
in_size>>=1;
EndianSwap<u16char>(in_text.u16_text,in_size);
result=tos.WriteChars(in_text.u16_text,in_size);
}
else
if(out_charset==utf8_charset)
{
char *new_text;
int new_size;
new_size=to_utf8(in_cs,&new_text,in_text.u8_text,in_size);
result=tos.WriteChars(new_text,new_size);
delete[] new_text;
}
else
if(out_charset==utf8_charset)
{
u16char *new_text;
int new_size;
new_size=to_utf16(in_cs,&new_text,in_text.u8_text,in_size);
result=tos.WriteChars(new_text,new_size);
delete[] new_text;
}
if(!result)
cmd_out<<OS_TEXT("[ERR] Write Text failed")<<std::endl;
else
cmd_out<<OS_TEXT("[OK] filename: ")<<out_filename.c_str()<<std::endl;
delete[] old_text;
};
class EnumTextFile :public EnumFile
{
public:
using EnumFile::EnumFile;
virtual void ProcFolderBegin(struct EnumFileConfig*, struct EnumFileConfig* cur_efc, FileInfo& fi) override
{
const OSString sub_folder_name = fi.name;
if (sub_folder_name.GetFirstChar() == '.')return; //跳过第一个字符为.的目录
for (int i = 0; i < discard_folder_list.GetCount(); i++)
{
if (discard_folder_list[i].Comp(sub_folder_name) == 0)
return;
}
OSString new_path;
new_path = MergeFilename(out_folder_base, cur_efc->folder_name.c_str() + in_folder_base.Length());
#if HGL_OS != HGL_OS_Windows
UTF16String u16_path = to_u16(new_path);
if (fn_english_fw2hw)
{
ConvertQtoB(u16_path.c_str(), u16_path.Length());
new_path = to_u8(u16_path);
}
#else
ConvertQtoB(new_path.c_str(), new_path.Length());
#endif//
if (!MakePath(new_path))
{
cmd_out << OS_TEXT("[ERR] Create Directory failed: ") << new_path.c_str() << std::endl;
return;
}
else
{
cmd_out << OS_TEXT("[DIR] Create Directory: ") << new_path.c_str() << std::endl;
}
}
virtual void ProcFile(struct EnumFileConfig *efc, FileInfo &fi) override
{
if (fi.name[0] == '.')return;
for (int i = 0; i < discard_file_list.GetCount(); i++)
{
if (discard_file_list[i].Comp(fi.name) == 0)
return;
}
const os_char* ext_name = strrchr<os_char>(fi.name,sizeof(fi.name)/sizeof(os_char),'.');
if (!ext_name)return;
if (!ExtNameCheck(ext_name))return;
cmd_out << OS_TEXT("[IN] filename: ") << fi.fullname << std::endl;
//生成新文件名
OSString new_filename = out_folder_base + (fi.fullname + in_folder_base.Length());
#if HGL_OS != HGL_OS_Windows
UTF16String u16_filename = to_u16(new_filename);
if (fn_english_fw2hw)
{
ConvertQtoB(u16_filename.c_str(), u16_filename.Length());
new_filename = to_u8(u16_filename);
}
#else
ConvertQtoB(new_filename.c_str(), new_filename.Length());
#endif//
if (out_charset==UTF8CharSet )Convert<UTF8TextOutputStream >(fi.fullname, new_filename); else
if (out_charset==UTF16LECharSet )Convert<UTF16LETextOutputStream>(fi.fullname, new_filename); else
if (out_charset==UTF16BECharSet )Convert<UTF16BETextOutputStream>(fi.fullname, new_filename);
}
};//
void out_string_list(const OSStringList &sl,const os_char *name)
{
cmd_out<<OS_TEXT(" ")<<name<<OS_TEXT(": ")<<std::endl;
for(int i=0;i<sl.GetCount();i++)
{
if(sl[i].Length()<=0)continue;
cmd_out<<OS_TEXT("\t")<<i<<OS_TEXT(": ")<<sl[i].c_str()<<std::endl;
}
std::cout<<std::endl;
}
int os_main(int argc,const os_char **argv)
{
OSStringList args;
for (int i = 1; i < argc; i++)
args.Add(argv[i]);
std::cout<<std::endl;
std::cout<<"CMGDK Tools << Text Encoding Convert v2.0 >>"<<std::endl;
std::cout<<"(C)1997-2017 Copyright. Offical Web:www.hyzgame.com.cn"<<std::endl;
std::cout<<std::endl;
LoadStringListFromTextFile(convert_ext_list,OS_TEXT("TextEncodingConvert.filter"),UTF8CharSet);
LoadStringListFromTextFile(discard_file_list,OS_TEXT("TextEncodingConvert.discard_file"),UTF8CharSet);
LoadStringListFromTextFile(discard_folder_list,OS_TEXT("TextEncodingConvert.discard_folder"),UTF8CharSet);
if(args.GetCount()<5)
{
std::cout<<" option:"<<std::endl;
std::cout<<"\t-fjh\tconvert shift-jis half-width japanese filename to full-width"<<std::endl; //转换日文半角文件名到全角
std::cout<<"\t-jh\tconvert shift-jis half-width japanese text to full-width"<<std::endl; //转换日文半角文本到全角
std::cout<<"\t-fbl\tconvert full-width english and number filename to half-width"<<std::endl; //转换全角英文和数字文件名到半角
std::cout<<"\t-bl\tconvert full-width english and number text to half-width"<<std::endl; //转换全角英文和数字文本到半角
std::cout<<"\t-no_bom\ttarget file add BOM header"<<std::endl; //新文件不需要BOM头
std::cout<<"\t-sf\tproc sub-folder"<<std::endl; //处理子目录
std::cout<<std::endl;
std::cout<<" Format: TextEncodingConvert [-fjh|-jh|-fbl|-bl|-no_bom|-sf] -in <us-ascii|shift_jis|big5|gbk|...> <input path> -out <utf8|utf16le> <output path>"<<std::endl;
std::cout<<" Example: TextEncodingConvert -in us-ascii -no_bom /home/hyzboy/input/ -out utf8 /home/hyzboy/output/"<<std::endl;
std::cout<<std::endl;
out_string_list(convert_ext_list,OS_TEXT("filter"));
out_string_list(discard_file_list,OS_TEXT("discard file"));
out_string_list(discard_folder_list,OS_TEXT("discard folder"));
std::cout<<std::endl;
return(0);
}
if(args.CaseFind(OS_TEXT("-fjh"))!=-1)
{
fn_japanese_hw2fw=true;
std::cout<<"option[-fjh]: conevrt shift-jis half-width japanese filename to full-width"<<std::endl;
}
if(args.CaseFind(OS_TEXT("-jh"))!=-1)
{
japanese_hw2fw=true;
std::cout<<"option[-jh]: conevrt shift-jis half-width japanese text to full-width"<<std::endl;
}
if(args.CaseFind(OS_TEXT("-fbl"))!=-1)
{
fn_english_fw2hw=true;
std::cout<<"option[-fbl]: conevrt full-width english and number filename to half-width"<<std::endl;
}
if(args.CaseFind(OS_TEXT("-bl"))!=-1)
{
english_fw2hw=true;
std::cout<<"option[-bl]: conevrt full-width english and number text to half-width"<<std::endl;
}
if(args.CaseFind(OS_TEXT("-no_bom"))!=-1)
{
include_bom=false;
std::cout<<"option[-no_bom]: don't include BOM Header"<<std::endl;
}
if(args.CaseFind(OS_TEXT("-sf"))!=-1)
{
sub_folder=true;
std::cout<<"option[-sf]: proc sub-folder"<<std::endl;
}
int in_off=args.CaseFind(OS_TEXT("-in"));
if(in_off!=-1)
{
#if HGL_OS == HGL_OS_Windows
const U8String in_cs_str=to_u8(args[in_off+1]);
#else
const OSString &in_cs_str=args[in_off+1];
#endif//
in_charset=CharSet(in_cs_str);
std::cout<<"in CharSet: "<<in_charset.charset<<std::endl;
}
int out_off=args.CaseFind(OS_TEXT("-out"));
if(out_off!=-1)
{
#if HGL_OS == HGL_OS_Windows
const U8String out_cs_str=to_u8(args[out_off+1]);
#else
const OSString &out_cs_str=args[out_off+1];
#endif//
if(charset_cmp(out_cs_str.c_str(),"utf8")==0)out_charset=UTF8CharSet;else
if(charset_cmp(out_cs_str.c_str(),"utf16")==0
||charset_cmp(out_cs_str.c_str(),"ucs2le")==0
)out_charset=UTF16LECharSet;else
{
std::cout<<"out charset error,only support utf8 or utf16le"<<std::endl;
return(-2);
}
std::cout<<"out CharSet: "<<out_charset.charset<<std::endl;
}
in_folder_base=args[in_off+2];
out_folder_base=args[out_off+2];
cmd_out<<OS_TEXT("in folder: ")<<in_folder_base.c_str()<<std::endl;
cmd_out<<OS_TEXT("out folder: ")<<out_folder_base.c_str()<<std::endl;
if(!MakePath(out_folder_base))
{
std::cout<<"[ERR] Create Directory failed: "<<out_folder_base.c_str()<<std::endl;
return(-3);
}
std::cout<<std::endl;
EnumFileConfig efc(in_folder_base);
efc.sub_folder=sub_folder;
EnumTextFile ef;
ef.Enum(&efc);
return 0;
}