forked from Alexey-T/CudaLister
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_proc.pas
163 lines (135 loc) · 3.5 KB
/
file_proc.pas
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
unit file_proc;
{$mode objfpc}{$H+}
interface
uses
Windows,
Classes, SysUtils, FileUtil,
ATStrings,
ATStringProc_UTF8Detect;
function IsFileTooBig(const fn: string): boolean;
function IsFileText(const fn: string): boolean;
function _GetDllFilename: string;
var
OptMaxFileSizeMb: integer = 2;
implementation
function IsFileTooBig(const fn: string): boolean;
begin
Result:= FileSize(fn) >= OptMaxFileSizeMb*(1024*1024);
end;
type
TFreqTable = array[$80 .. $FF] of Integer;
function IsAsciiControlChar(n: integer): boolean; inline;
const
cAllowedControlChars: set of byte = [
7, //Bell
8, //Backspace
9, //Tab
10, //LF
13, //CR
12, //FormFeed
26, //EOF
27 //Escape
];
begin
Result:= (n < 32) and not (byte(n) in cAllowedControlChars);
end;
function AppIsFileContentText(const fn: string; BufSizeKb: integer;
BufSizeWords: integer;
DetectOEM: boolean): Boolean;
const
cBadBytesAtEndAllowed = 2;
var
Buffer: PAnsiChar;
BufSize, BytesRead, i: DWORD;
n: Integer;
Table: TFreqTable;
TableSize: Integer;
Str: TFileStream;
IsLE: boolean;
bReadAllFile: boolean;
begin
Result:= False;
//IsOEM:= False;
Str:= nil;
Buffer:= nil;
if BufSizeKb<=0 then Exit;
BufSize:= BufSizeKb*1024;
//Init freq table
TableSize:= 0;
FillChar(Table, SizeOf(Table), 0);
try
try
Str:= TFileStream.Create(fn, fmOpenRead or fmShareDenyNone);
except
exit(false);
end;
if Str.Size<=2 then exit(true);
if DetectStreamUtf8NoBom(Str, BufSizeKb)=TATBufferUTF8State.Yes then exit(true);
if DetectStreamUtf16NoBom(Str, BufSizeWords, IsLE) then exit(true);
Str.Position:= 0;
GetMem(Buffer, BufSize);
FillChar(Buffer^, BufSize, 0);
BytesRead:= Str.Read(Buffer^, BufSize);
if BytesRead > 0 then
begin
bReadAllFile:= BytesRead=Str.Size;
//Test UTF16 signature
if ((Buffer[0]=#$ff) and (Buffer[1]=#$fe)) or
((Buffer[0]=#$fe) and (Buffer[1]=#$ff)) then
Exit(True);
Result:= True;
for i:= 0 to BytesRead - 1 do
begin
n:= Ord(Buffer[i]);
//If control chars present, then non-text
if IsAsciiControlChar(n) then
//ignore bad bytes at the end, https://github.com/Alexey-T/CudaText/issues/2959
if not (bReadAllFile and (i>=BytesRead-cBadBytesAtEndAllowed)) then
begin
Result:= False;
Break
end;
//Calculate freq table
if DetectOEM then
if (n >= Low(Table)) and (n <= High(Table)) then
begin
Inc(TableSize);
Inc(Table[n]);
end;
end;
end;
//Analize table
if DetectOEM then
if Result and (TableSize > 0) then
for i:= Low(Table) to High(Table) do
begin
Table[i]:= Table[i] * 100 div TableSize;
if ((i >= $B0) and (i <= $DF)) or (i = $FF) or (i = $A9) then
if Table[i] >= 18 then
begin
//IsOEM:= True;
Break
end;
end;
finally
if Assigned(Buffer) then
FreeMem(Buffer);
if Assigned(Str) then
FreeAndNil(Str);
end;
end;
function IsFileText(const fn: string): boolean;
var
bOem: boolean;
begin
Result:= AppIsFileContentText(fn, 64, 2*1024, bOem);
end;
function _GetDllFilename: string;
var
S: UnicodeString;
begin
SetLength(S, MAX_PATH);
SetLength(S, GetModuleFileNameW(HINSTANCE, PWChar(S), Length(S)));
Result:= UTF8Encode(S);
end;
end.