forked from Kryuski/AnsiToUtf8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnsiToUtf8.dpr
88 lines (72 loc) · 2.52 KB
/
AnsiToUtf8.dpr
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
program AnsiToUtf8;
// Disable the "new" RTTI
{$WEAKLINKRTTI ON}
{$IF DECLARED(TVisibilityClasses)}
{$RTTI EXPLICIT METHODS([]) PROPERTIES([]) FIELDS([])}
{$IFEND}
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.Classes,
System.IOUtils,
Windows,
Conversion.Processor in 'Sources\Conversion.Processor.pas',
Conversion.Processor.Interfaces in 'Sources\Conversion.Processor.Interfaces.pas';
begin
try
if ParamCount = 0 then begin
Writeln('ANSI to UTF-8 file converter.');
Writeln('Usage: ', ExtractFileName(ParamStr(0)), ' <filename or folder>');
Writeln('Options:');
Writeln(' -cp <codepage>: Define an ANSI codepage for the file (default 1251).');
Writeln(' -subdirs: Include subdirectories when converting a folder.');
Writeln(' -pattern: Search pattern for files (default *.pas).');
Exit;
end;
var FileParam := ParamStr(1);
var CodePage := 1251;
var CodePageParam: string;
if FindCmdLineSwitch('cp', CodePageParam) then
CodePage := StrToInt(CodePageParam);
var TargetEncoding := TEncodingTarget.UTF8;
Writeln(Format('Started ANSI (codepage %d) to %s conversion', [CodePage, TargetEncoding.Name]));
var Processor: IConversionProcessor := TConversionProcessor.Create;
Processor
.UseTargetEncoding(TEncodingTarget.UTF8);
var IsDirectory := False;
if TDirectory.Exists(FileParam) then
IsDirectory := True;
var SearchOption := TSearchOption.soTopDirectoryOnly;
if FindCmdLineSwitch('subdirs') then
SearchOption := TSearchOption.soAllDirectories;
var SearchPattern := '*.pas';
var PatternParam: string;
if FindCmdLineSwitch('pattern', PatternParam) then
SearchPattern := PatternParam;
var Files: TArray<string> := [];
if IsDirectory then
Files := TDirectory.GetFiles(FileParam, SearchPattern, SearchOption)
else
Files := [FileParam];
for var FileName in Files do
begin
try
Processor
.UseTargetEncoding(TargetEncoding)
.Convert(FileName, CodePage,
procedure(ProgressMsg: string)
begin
Writeln(ProgressMsg);
end);
except
on E: Exception do
Writeln(Format('> ERRROR: [%s] %s', [E.ClassName, E.Message]));
end;
end;
Writeln('Conversion finished');
except
on E: Exception do
Writeln(Format('ERRROR: [%s] %s', [E.ClassName, E.Message]));
end;
end.