-
Notifications
You must be signed in to change notification settings - Fork 1
/
Functions.pas
378 lines (336 loc) · 9.33 KB
/
Functions.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
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
unit Functions;
interface
uses
Windows, Messages, SysUtils, StrUtils, IniFiles, Classes, Forms, Variants, MsHTML,
StdCtrls, SynEdit, ActiveX;
type
TContentCallBack = procedure(Content: string) of object;
function GetDosOutput(CommandLine: string; Work: string = ''; ContentCallBack: TContentCallBack=nil): String;
function StrIPos(const SubStr, S: string): Integer;
function LoadFileToStr(const FileName: TFileName): AnsiString;
function LastPos(const SubStr, S: string): integer;
function IsTextHTML(s: string): boolean;
function GetWordUnderPos(AMemo: TSynEdit; Line, Column: integer): string;
function GetWordUnderCaret(AMemo: TSynEdit): string;
function MyVarToStr(v: Variant): string;
function FileSystemCaseSensitive: boolean;
function HighColorWindows: boolean;
function GetTempDir: string;
function GetSpecialFolder(const aCSIDL: Integer): string;
function GetMyDocumentsFolder: string;
function MD5(const str: string): string;
function MD5Stream(const s: TStream): string;
implementation
uses
ShlObj, // Needed for the CSIDL constants
IdGlobal, IdHash, IdHashMessageDigest; // used for MD5 calculation
function GetDosOutput(CommandLine: string; Work: string = ''; ContentCallBack: TContentCallBack=nil): String;
var
SA: TSecurityAttributes;
SI: TStartupInfo;
PI: TProcessInformation;
StdOutPipeRead, StdOutPipeWrite: THandle;
WasOK: Boolean;
Buffer: array[0..255] of AnsiChar;
BytesRead: Cardinal;
WorkDir: string;
Handle: Boolean;
begin
if Work = '' then Work := ExtractFilePath(ParamStr(0));
Result := '';
with SA do begin
nLength := SizeOf(SA);
bInheritHandle := True;
lpSecurityDescriptor := nil;
end;
CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
try
with SI do
begin
FillChar(SI, SizeOf(SI), 0);
cb := SizeOf(SI);
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
wShowWindow := SW_HIDE;
hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
hStdOutput := StdOutPipeWrite;
hStdError := StdOutPipeWrite;
end;
WorkDir := Work;
Handle := CreateProcess(nil, PChar('cmd.exe /C "' + CommandLine + '"'),
nil, nil, True, 0, nil,
PChar(WorkDir), SI, PI);
CloseHandle(StdOutPipeWrite);
if Handle then
try
repeat
WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
if BytesRead > 0 then
begin
Buffer[BytesRead] := #0;
Result := Result + string(Buffer);
if Assigned(ContentCallBack) then ContentCallBack(string(Buffer));
end;
until not WasOK or (BytesRead = 0);
WaitForSingleObject(PI.hProcess, INFINITE);
finally
CloseHandle(PI.hThread);
CloseHandle(PI.hProcess);
end;
finally
CloseHandle(StdOutPipeRead);
end;
end;
function StrIPos(const SubStr, S: string): Integer;
begin
Result := Pos(UpperCase(SubStr), UpperCase(S));
end;
function LoadFileToStr(const FileName: TFileName): AnsiString;
var
FileStream : TFileStream;
begin
Result := '';
FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
if FileStream.Size>0 then begin
SetLength(Result, FileStream.Size);
FileStream.Read(Result[1], FileStream.Size);
end;
finally
FileStream.Free;
end;
end;
function LastPos(const SubStr, S: string): integer;
var
I, J, K: integer;
begin
Result := 0;
I := Length(S);
K := Length(SubStr);
if (K = 0) or (K > I) then
Exit;
while (Result = 0) and (I >= K) do
begin
J := K;
if S[I] = SubStr[J] then
begin
while (J > 1) and (S[I + J - K - 1] = SubStr[J - 1]) do
Dec(J);
if J = 1 then
Result := I - K + 1;
end;
Dec(I);
end;
end;
function IsTextHTML(s: string): boolean;
function _Tag(const tag: string): integer;
begin
result := 0;
if (StrIPos('<'+tag+'>', s) > 0) then Inc(result);
if (StrIPos('</'+tag+'>', s) > 0) then Inc(result);
if (StrIPos('<'+tag+' />', s) > 0) then Inc(result);
if (StrIPos('<'+tag+' ', s) > 0) then Inc(result);
end;
procedure _Check(const tag: string; pair: boolean);
begin
if (pair and (_Tag(tag) >= 2)) or (not pair and (_Tag(tag) >= 1)) then result := true;
end;
begin
result := false;
_Check('html', true);
_Check('body', true);
_Check('p', false{end tag optional});
_Check('a', true);
_Check('b', true);
_Check('i', true);
_Check('u', true);
_Check('li', false{end tag optional});
_Check('ol', true);
_Check('ul', true);
_Check('img', false);
_Check('div', false);
_Check('hr', false);
_Check('code', true);
_Check('pre', true);
_Check('blockquote', true);
_Check('span', true);
_Check('br', false);
end;
// Template: http://stackoverflow.com/questions/6339446/delphi-get-the-whole-word-where-the-caret-is-in-a-memo
function GetWordUnderPos(AMemo: TSynEdit; Line, Column: integer): string;
function ValidChar(c: char): boolean;
begin
{$IFDEF UNICODE}
result := CharInSet(c, ['a'..'z', 'A'..'Z', '0'..'9', '_']);
{$ELSE}
result := c in ['a'..'z', 'A'..'Z', '0'..'9', '_'];
{$ENDIF}
end;
var
LineText: string;
InitPos : Integer;
EndPos : Integer;
begin
//Validate the line number
if AMemo.Lines.Count-1 < Line then Exit;
//Get the text of the line
LineText := AMemo.Lines[Line];
if LineText = '' then
begin
result := '';
exit;
end;
// Column zeigt auf das Zeichen LINKS vom Cursor!
InitPos := Column;
if not ValidChar(LineText[InitPos]) then Inc(InitPos);
while (InitPos-1 >= 1) and ValidChar(LineText[InitPos-1]) do Dec(InitPos);
EndPos := Column;
while (EndPos+1 <= Length(LineText)) and ValidChar(LineText[EndPos+1]) do Inc(EndPos);
//Get the text
Result := Copy(LineText, InitPos, EndPos - InitPos + 1);
end;
function GetWordUnderCaret(AMemo: TSynEdit): string;
var
Line : Integer;
Column : Integer;
begin
//Get the caret position
(*
if AMemo is TMemo then
begin
Line := AMemo.Perform(EM_LINEFROMCHAR,AMemo.SelStart, 0);
Column := AMemo.SelStart - AMemo.Perform(EM_LINEINDEX, Line, 0);
end;
if AMemo is TSynEdit then
begin
*)
Line := AMemo.CaretY-1;
Column := AMemo.CaretX-1;
(*
end;
*)
result := GetWordUnderPos(AMemo, Line, Column);
end;
function MyVarToStr(v: Variant): string;
var
_Lo, _Hi, i: integer;
begin
if VarIsNull(v) then
begin
result := '';
end
else if VarIsArray(v) then
begin
_Lo := VarArrayLowBound(v, 1);
_Hi := VarArrayHighBound(v, 1);
result := '';
for i := _Lo to _Hi do
begin
if v[i] = 0 then break;
result := result + chr(integer(v[i]));
end;
end
else
begin
// At least try it...
result := VarToStr(v);
end;
end;
function FileSystemCaseSensitive: boolean;
begin
// TODO: This code is not very reliable. At MAC OSX, the file system HFS can be switched
// between case sensitivity and insensitivity.
{$IFDEF LINUX}
exit(true);
{$ELSE}
result := false;
exit;
{$ENDIF}
end;
function HighColorWindows: boolean;
var
ver: Cardinal;
dwMajorVersion, dwMinorVersion: integer;
begin
ver := GetVersion();
dwMajorVersion := Lo(ver);
dwMinorVersion := Hi(ver);
// Gradient fitting in:
// 5.1 = XP
// 5.2 = Windows Server 2003
// 6.0 = Vista
// 6.1 = Win7
result := ((dwMajorVersion = 5) and (dwMinorVersion >= 1)) or
((dwMajorVersion = 6) and (dwMinorVersion <= 1));
end;
function GetTempDir: string;
var
Dir: string;
Len: DWord;
begin
SetLength(Dir,MAX_PATH);
Len:=GetTempPath(MAX_PATH, PChar(Dir));
if Len>0 then
begin
SetLength(Dir,Len);
Result:=Dir;
end
else
RaiseLastOSError;
end;
function SHGetFolderPath(hwnd: HWND; csidl: Integer; hToken: THandle;
dwFlags: DWord; pszPath: LPWSTR): HRESULT; stdcall;
external 'SHFolder.dll' name 'SHGetFolderPathW';
function GetSpecialFolder(const aCSIDL: Integer): string;
var
FolderPath: array[0 .. MAX_PATH] of Char;
begin
SetLastError(ERROR_SUCCESS);
if SHGetFolderPath(0, aCSIDL, 0, 0, @FolderPath) = S_OK then
Result := FolderPath;
end;
function GetMyDocumentsFolder: string;
begin
Result := GetSpecialFolder(CSIDL_PERSONAL);
end;
{$IF gsIdVersion <> '10.1.5'} // Delphi 2007 built-in Indy10; gsIdVersion requires idGlobal.pas
{$DEFINE NewIndy}
{$IFEND}
function MD5Stream(const s: TStream): string;
var
idmd5: TIdHashMessageDigest5;
begin
idmd5 := TIdHashMessageDigest5.Create;
try
{$IFDEF NewIndy}
result := idmd5.HashStreamAsHex(s);
{$ELSE}
result := idmd5.AsHex(idmd5.HashValue(s));
{$ENDIF}
finally
idmd5.Free;
end;
end;
function MD5(const str: string): string;
{$IFDEF NewIndy}
var
idmd5: TIdHashMessageDigest5;
begin
idmd5 := TIdHashMessageDigest5.Create;
try
result := idmd5.HashStringAsHex(str,IndyTextEncoding_OSDefault);
finally
idmd5.Free;
end;
{$ELSE}
var
ss: TStringStream;
begin
ss := TStringStream.Create(str);
try
result := MD5Stream(ss);
finally
ss.Free;
end;
{$ENDIF}
end;
end.