Debian Codegraph @

src/base/castleutils_delphi_compatibility.inc

raw
1 {%MainUnit castleutils.pas}
2 {
3 Copyright 2017-2023 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16 { Useful things from FPC RTL defined for Delphi. }
17
18 // This whole file is only used for Delphi, FPC already has this.
19 {$ifndef FPC}
20 {$ifndef PASDOC}
21 {$ifdef read_interface}
22 const
23 LineEnding = SLineBreak;
24
25 {$ifdef MSWINDOWS}
26 // https://community.embarcadero.com/article/technical-articles/149-tools/13303-detecting-plugnplay-hardware-changes
27 DBT_DEVICEARRIVAL = $8000;
28 DBT_DEVICEQUERYREMOVE = $8001;
29 DBT_DEVICEQUERYREMOVEFAILED = $8002;
30 DBT_DEVICEREMOVEPENDING = $8003;
31 DBT_DEVICEREMOVECOMPLETE = $8004;
32 DBT_DEVICETYPESPECIFIC = $8005;
33 DBT_CONFIGCHANGED = $0018;
34
35 DBT_DEVTYP_DEVICEINTERFACE = $00000005;
36 {$endif}
37
38 {$ifdef UNIX}
39 StdInputHandle = 0;
40 StdOutputHandle = 1;
41 StdErrorHandle = 2;
42 {$endif}
43
44 type
45 SizeInt = NativeInt;
46 SizeUInt = NativeUInt;
47 QWord = UInt64;
48 PQWord = PUInt64;
49 PtrInt = NativeInt;
50 PtrUInt = NativeUInt;
51 WINBOOL = longbool;
52 PUInt16 = ^UInt16;
53 PInt32 = ^Int32;
54 {$ifndef VER340}
55 { Pointer to UInt32.
56 Known to be necessary to be defined in CGE for Delphi 10.2
57 (see https://github.com/castle-engine/castle-engine/issues/475),
58 not necessary with Delphi >= 10.4. }
59 PUInt32 = ^UInt32;
60 {$endif}
61 TGetVendorNameEvent = function: string;
62 TGetAppNameEvent = function: string;
63 TEventType = (etCustom, etInfo, etWarning, etError, etDebug);
64 TEventTypes = Set of TEventType;
65
66 {$ifndef MSWINDOWS}
67 DWord = UInt32;
68 PDWord = ^DWord;
69 {$endif}
70
71 { TOwnerStream from FPC }
72 TOwnerStream = Class(TStream)
73 protected
74 FOwner : Boolean;
75 FSource : TStream;
76 public
77 constructor Create(ASource : TStream);
78 destructor Destroy; override;
79 property Source : TStream Read FSource;
80 property SourceOwner : Boolean Read Fowner Write FOwner;
81 end;
82
83 {$ifdef MSWINDOWS}
84 DEV_BROADCAST_DEVICEINTERFACE = record
85 dbcc_size: DWORD;
86 dbcc_devicetype: DWORD;
87 dbcc_reserved: DWORD;
88 dbcc_classguid: TGuid;
89 dbcc_name: array [0..0] of Char;
90 end;
91 {$endif}
92
93 var
94 OnGetVendorName : TGetVendorNameEvent;
95 OnGetApplicationName : TGetAppNameEvent;
96
97 function GetMem(const Size: Integer): Pointer;
98 function VendorName: string;
99 function ApplicationName: string;
100 function GetAppConfigDir(Global : Boolean) : String;
101 function StringsReplace(const S: string; OldPattern, NewPattern: array of string; Flags: TReplaceFlags): string;
102 function StrToQWord(const S: String): QWord;
103
104 { More reliable IsWild implementation for Delphi
105 can be found in CastleStringUtils unit.
106
107 For unknown reason, the commented out implementation here (copied from FPC RTL)
108 was completely unstable in Delphi, returning random results in some cases
109 -- testcase e.g.
110
111 ./castle_tester_standalone --console --filter='*testeventloop*'
112
113 The filter was matching randomly ~330 names...
114
115 Weird:
116
117 - The implementation here doesn't seem to depend on 8-bit vs 16-bit
118 strings.
119 - And it returns stable results in simple applications.
120 - Adding Writelns to debug... changed the results to be correct sometimes!
121
122 For now, for Delphi, we use a hacky but simpler IsWild from CastleStringUtils
123 that is based on regexps.
124 Autotested in TTestCastleStringUtils.TestIsWild . }
125 //function IsWild(InputStr, Wilds: string; IgnoreCase: Boolean): Boolean;
126
127 procedure FillByte(var X; Count: SizeInt; Value: Byte);
128 procedure FillDWord(var X; const Count: SizeInt; const Value: DWord);
129 function StrToDWord(const S: String): DWord;
130
131 { Silence warnings, we know it is platform specific and most code shouldn't use Extended,
132 it's also said in our coding convs: https://castle-engine.io/coding_conventions }
133 {$warnings off}
134 const
135 MaxFloat = MaxExtended;
136 {$warnings on}
137
138 {$endif read_interface}
139 {$ifdef read_implementation}
140
141 constructor TOwnerStream.Create(ASource: TStream);
142 begin
143 FSource:=ASource;
144 end;
145
146 destructor TOwnerStream.Destroy;
147 begin
148 If FOwner then
149 FreeAndNil(FSource);
150 inherited Destroy;
151 end;
152
153 function GetMem(const Size: Integer): Pointer;
154 begin
155 System.GetMem(Result, Size);
156 end;
157
158 { Copied from FPC RTL (same license as Castle Game Engine) }
159 function VendorName : String;
160 begin
161 If Assigned(OnGetVendorName) then
162 Result:=OnGetVendorName()
163 else
164 Result:='';
165 end;
166
167 { Copied from FPC RTL (same license as Castle Game Engine) }
168 function ApplicationName : String;
169 begin
170 If Assigned(OnGetApplicationName) then
171 Result:=OnGetApplicationName()
172 else
173 Result:=ChangeFileExt(ExtractFileName(Paramstr(0)),'');
174 end;
175
176 {$ifdef MSWINDOWS}
177 { Copied from FPC RTL (same license as Castle Game Engine) and adjusted }
178 function GetWindowsSpecialDirUnicode(ID: Integer; CreateIfNotExists: Boolean = True): UnicodeString;
179 var
180 Buffer: array [0..MAX_PATH] of WideChar;
181 begin
182 Result := '';
183 if SHGetSpecialFolderPathW(0, @Buffer[0], ID, CreateIfNotExists) then
184 Result := IncludeTrailingPathDelimiter(StrPas(Buffer));
185 end;
186
187 { Copied from FPC RTL (same license as Castle Game Engine) }
188 function GetWindowsSpecialDir(ID: Integer; CreateIfNotExists: Boolean = True): String;
189 begin
190 Result := String(GetWindowsSpecialDirUnicode(ID, CreateIfNotExists));
191 end;
192 {$endif}
193
194 { Copied from FPC RTL (same license as Castle Game Engine) }
195 function GetAppConfigDir(Global : Boolean) : String;
196 {$ifdef MSWINDOWS}
197
198 function DGetAppConfigDir(Global : Boolean) : String;
199 begin
200 Result:=ExcludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0)));
201 end;
202
203 begin
204 If Global then
205 Result:=GetWindowsSpecialDir(CSIDL_COMMON_APPDATA)
206 else
207 Result:=GetWindowsSpecialDir(CSIDL_LOCAL_APPDATA);
208 If (Result<>'') then
209 begin
210 if VendorName<>'' then
211 Result:=IncludeTrailingPathDelimiter(Result+VendorName);
212 Result:=IncludeTrailingPathDelimiter(Result+ApplicationName);
213 end
214 else
215 Result:=IncludeTrailingPathDelimiter(DGetAppConfigDir(Global));
216 {$endif}
217
218 {$ifdef UNIX}
219 begin
220 // TODO: simplest implementation hardcoded now, look how FPC implements it
221 if Global then
222 Result := '/etc/' + ApplicationName + PathDelim
223 else
224 Result := IncludeTrailingPathDelimiter(GetEnvironmentVariable('HOME')) +
225 '.config' + PathDelim +
226 ApplicationName + PathDelim;
227 {$endif}
228 end;
229
230 function StringsReplace(const S: string; OldPattern, NewPattern: array of string; Flags: TReplaceFlags): string;
231 var pc,pcc,lastpc : pchar;
232 strcount : integer;
233 ResStr,
234 CompStr : string;
235 Found : Boolean;
236 sc : sizeint;
237 begin
238 if S = '' then
239 Exit; // early exit, rest of code assumes S is not empty, doing CompStr[1]
240
241 sc := length(OldPattern);
242 if sc <> length(NewPattern) then
243 raise exception.Create('Amount of search and replace strings don''t match');
244 dec(sc);
245 if rfIgnoreCase in Flags then
246 begin
247 CompStr:=AnsiUpperCase(S);
248 for strcount := 0 to sc do
249 OldPattern[strcount] := AnsiUpperCase(OldPattern[strcount]);
250 end
251 else
252 CompStr := s;
253 ResStr := '';
254 pc := @CompStr[1];
255 pcc := @s[1];
256 lastpc := pc+Length(S);
257 while pc < lastpc do
258 begin
259 Found := False;
260 for strcount := 0 to sc do
261 begin
262 if (length(OldPattern[strcount])>0) and
263 (OldPattern[strcount][1]=pc^) and
264 (Length(OldPattern[strcount]) <= (lastpc-pc)) and
265 CompareMem(Pointer(OldPattern[strcount]), pc, Length(OldPattern[strcount]) * SizeOf(Char)) then
266 begin
267 ResStr := ResStr + NewPattern[strcount];
268 pc := pc+Length(OldPattern[strcount]);
269 pcc := pcc+Length(OldPattern[strcount]);
270 Found := true;
271 end
272 end;
273 if not found then
274 begin
275 ResStr := ResStr + pcc^;
276 inc(pc);
277 inc(pcc);
278 end
279 else if not (rfReplaceAll in Flags) then
280 begin
281 ResStr := ResStr + StrPas(pcc);
282 break;
283 end;
284 end;
285 Result := ResStr;
286 end;
287
288 function StrToQWord(const S: String): QWord;
289 begin
290 Result := StrToUInt64(S);
291 end;
292
293 (*
294 Function isMatch(level : integer;inputstr,wilds : string; CWild, CinputWord: SizeInt;MaxInputword,maxwilds : SizeInt; Out EOS : Boolean) : Boolean;
295
296 function WildisQuestionmark : boolean;
297 begin
298 Result:=CWild <= MaxWilds;
299 if Result then
300 Result:= Wilds[CWild]='?';
301 end;
302
303 function WildisStar : boolean;
304 begin
305 Result:=CWild <= MaxWilds;
306 if Result then
307 Result:= Wilds[CWild]='*';
308 end;
309
310 begin
311 EOS:=False;
312 Result:=True;
313
314 //Writeln('IsMatch start: ', inputStr, ' ', Wilds);
315
316 repeat
317 if WildisStar then { handling of '*' }
318 begin
319 inc(CWild);
320 if CWild>MaxWilds then
321 begin
322 EOS:=true;
323 exit;
324 end;
325 while WildisQuestionmark do { equal to '?' }
326 begin
327 { goto next letter }
328 inc(CWild);
329 inc(CinputWord);
330 end;
331 { increase until a match }
332 Repeat
333 while (CinputWord <= MaxinputWord) and (CWild <= MaxWilds) and (inputStr[CinputWord]<>Wilds[CWild]) do
334 inc(CinputWord);
335 Result:=isMatch(Level+1,inputstr,wilds,CWild, CinputWord,MaxInputword,maxwilds,EOS);
336 if not Result then
337 Inc(cInputWord);
338 Until Result or (CinputWord>=MaxinputWord);
339 if Result and EOS then
340 Exit;
341 Continue;
342 end;
343 if WildisQuestionmark then { equal to '?' }
344 begin
345 { goto next letter }
346 inc(CWild);
347 inc(CinputWord);
348 Continue;
349 end;
350 if (CinputWord>MaxinputWord) or (CWild > MaxWilds) or (inputStr[CinputWord] = Wilds[CWild]) then { equal letters }
351 begin
352 { goto next letter }
353 inc(CWild);
354 inc(CinputWord);
355 Continue;
356 end;
357 Result:=false;
358 Exit;
359 until (CinputWord > MaxinputWord) or (CWild > MaxWilds);
360 { no completed evaluation, we need to check what happened }
361 if (CinputWord <= MaxinputWord) or (CWild < MaxWilds) then
362 Result:=false
363 else if (CWild>Maxwilds) then
364 EOS:=False
365 else
366 begin
367 EOS:=Wilds[CWild]='*';
368 if not EOS then
369 Result:=False;
370 end;
371
372 //Writeln('IsMatch: ', inputStr, ' ', Wilds, ' ', Result);
373 end;
374
375 function IsWild(InputStr, Wilds: string; IgnoreCase: Boolean): Boolean;
376 var
377 i: SizeInt;
378 MaxinputWord, MaxWilds: SizeInt; { Length of inputStr and Wilds }
379 eos : Boolean;
380 begin
381 Result:=true;
382 if Wilds = inputStr then
383 Exit;
384 { delete '**', because '**' = '*' }
385 i:=Pos('**', Wilds);
386 while i > 0 do
387 begin
388 Delete(Wilds, i, 1);
389 i:=Pos('**', Wilds);
390 end;
391 if Wilds = '*' then { for fast end, if Wilds only '*' }
392 Exit;
393 MaxinputWord:=Length(inputStr);
394 MaxWilds:=Length(Wilds);
395 if (MaxWilds = 0) or (MaxinputWord = 0) then
396 begin
397 Result:=false;
398 Exit;
399 end;
400 if ignoreCase then { upcase all letters }
401 begin
402 inputStr:=AnsiUpperCase(inputStr);
403 Wilds:=AnsiUpperCase(Wilds);
404 end;
405 //Writeln('IsWild: ',inputStr,' ',Wilds);
406 Result:=isMatch(1,inputStr,wilds,1,1,MaxinputWord, MaxWilds,EOS);
407 end;
408 *)
409
410 procedure FillDWord(var X; const Count: SizeInt; const Value: DWord);
411 var
412 PX, PEnd: PDWord;
413 begin
414 PX := @X;
415 PEnd := Pointer(PtrUInt(PX) + SizeOf(DWord) * Count);
416 while PX <> PEnd do
417 begin
418 PX^ := Value;
419 Inc(PX);
420 end;
421 end;
422
423 procedure FillByte(var X; Count: SizeInt; Value: Byte);
424 begin
425 FillChar(X, Count, Value);
426 end;
427
428 function StrToDWord(const S: String): DWord;
429 begin
430 Result := StrToUInt64(S);
431 end;
432
433 {$endif read_implementation}
434 {$endif not PASDOC}
435 {$endif not FPC}