-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommon.pas
193 lines (168 loc) · 4.07 KB
/
Common.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
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
unit Common;
interface
uses Windows, SysUtils, Classes, SyncObjs;
const
CRLF = 2573; // #13#10
SORT_BLOCK_SIZE = 100000;
MERGE_BLOCK_SIZE = SORT_BLOCK_SIZE; // SORT_BLOCK_SIZE *3 div 2;
MAX_LINE_LENGTH = 500;
STRING_COMPARE_LENGTH = 50;
{$WARNINGS OFF}
{.$IF SORT_BLOCK_SIZE < MAX_LINE_LENGTH}
// Incorrect constants!
{.$IFEND}
{$WARNINGS ON}
var
Debug: boolean = false;
TrackMemoryUsage: boolean = false;
MaxWorkerThreadCount: integer = 1;
MemoryAvailable: integer = 1024;
SortBufferSize: integer = SORT_BLOCK_SIZE;
MergeBufferSize: integer = SORT_BLOCK_SIZE;
MergeWriteBufferSize: integer = SORT_BLOCK_SIZE;
procedure Log(const s: string);
function FindLastCRLF(const BufferStart, BufferEnd: PAnsiChar): PAnsiChar;
function CompareStrings(const S1, S2: PAnsiChar; var S1End, S2End: PAnsiChar): integer; overload;
function CompareStrings(S1, S2: PAnsiChar): integer; overload;
function FileSize(const FileName: string): Int64;
procedure GetConsoleCursorPos(var x, y: integer);
procedure SetConsoleCursorPos(const x, y: integer);
procedure ShowConsoleCursor(const Visible: boolean);
type
TProgressProc = procedure(const Percent: Double);
implementation
var
LogCS: TCriticalSection;
procedure Log(const s: string);
begin
if Debug then
begin
LogCS.Acquire;
try
writeln(s);
finally
LogCS.Release;
end;
end;
end;
function FindLastCRLF(const BufferStart, BufferEnd: PAnsiChar): PAnsiChar;
begin
Result := BufferEnd - 2;
while (PWord(Result)^ <> CRLF) and (Result >= BufferStart) do
Dec(Result);
if Result < BufferStart then
Result := nil;
end;
// -1 ==> S1 < S2
// 0 ==> S1 = S2
// 1 ==> S1 > S2
function CompareStrings(const S1, S2: PAnsiChar; var S1End, S2End: PAnsiChar): integer;
var
CurLen: integer;
begin
Result := 0;
CurLen := 0;
S1End := S1;
S2End := S2;
while (Result = 0) and (CurLen < STRING_COMPARE_LENGTH) do
begin
if PWord(S1End)^ = CRLF then
begin
if PWord(S2End)^ <> CRLF then
Result := -1;
end
else
if PWord(S2End)^ = CRLF then
Result := 1
else
if S1End^ < S2End^ then
Result := -1
else
if S1End^ > S2End^ then
Result := 1
else
begin
Inc(S1End);
Inc(S2End);
end;
Inc(CurLen);
end;
// ñäâèíåì óêàçàòåëè ê íà÷àëàì ñîîòâåòñòâóþùèõ ñëåäóþùèõ ñòðîê â áóôåðàõ
while PWord(S1End)^ <> CRLF do
Inc(S1End);
Inc(S1End, 2);
while PWord(S2End)^ <> CRLF do
Inc(S2End);
Inc(S2End, 2);
end;
function CompareStrings(S1, S2: PAnsiChar): integer;
var
CurLen: integer;
begin
Result := 0;
CurLen := 0;
while (Result = 0) and (CurLen < STRING_COMPARE_LENGTH) do
begin
if PWord(S1)^ = CRLF then
begin
if PWord(S2)^ <> CRLF then
Result := -1;
end
else
if PWord(S2)^ = CRLF then
Result := 1
else
if S1^ < S2^ then
Result := -1
else
if S1^ > S2^ then
Result := 1
else
begin
Inc(S1);
Inc(S2);
end;
Inc(CurLen);
end;
end;
function FileSize(const FileName: string): Int64;
var
AttributeData: TWin32FileAttributeData;
begin
if GetFileAttributesEx(PChar(FileName), GetFileExInfoStandard, @AttributeData) then
begin
Int64Rec(Result).Lo := AttributeData.nFileSizeLow;
Int64Rec(Result).Hi := AttributeData.nFileSizeHigh;
end
else
Result := -1;
end;
procedure GetConsoleCursorPos(var x, y: integer);
var
CBI: TConsoleScreenBufferInfo;
begin
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), CBI);
X := TCoord(CBI.dwCursorPosition).X + 1;
Y := TCoord(CBI.dwCursorPosition).Y + 1;
end;
procedure SetConsoleCursorPos(const x, y: integer);
var
Coord: TCoord;
begin
Coord.X := X - 1;
Coord.Y := Y - 1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Coord);
end;
procedure ShowConsoleCursor(const Visible: boolean);
var
CCI: TConsoleCursorInfo;
begin
GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), CCI);
CCI.bVisible := Visible;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), CCI);
end;
initialization
LogCS := TCriticalSection.Create;
finalization
LogCS.Free;
end.