sysutilh.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. {%MainUnit sysutils.pp}
  2. {
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Florian Klaempfl
  5. member of the Free Pascal development team
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$MACRO ON}
  13. {$IFNDEF FPC_DOTTEDUNITS}
  14. {$DEFINE SUT:=sysutils}
  15. {$DEFINE BU:=baseunix}
  16. {$ELSE}
  17. {$DEFINE SUT:=System.SysUtils}
  18. {$DEFINE BU:=UnixApi.Base}
  19. {$ENDIF}
  20. { Using inlining for small system functions/wrappers }
  21. {$inline on}
  22. {$define SYSUTILSINLINE}
  23. { Read internationalization settings }
  24. {$i sysinth.inc}
  25. { Read date & Time function declarations }
  26. {$i osutilsh.inc}
  27. {$ifndef FPUNONE}
  28. {$i datih.inc}
  29. {$endif}
  30. function GetTickCount: LongWord; deprecated 'Use GetTickCount64 instead';
  31. function GetTickCount64: QWord;
  32. { Read String Handling functions declaration }
  33. {$i sysstrh.inc}
  34. type
  35. { some helpful data types }
  36. THandle = System.THandle;
  37. TProcedure = procedure;
  38. TFileName = type string;
  39. TIntegerSet = Set of 0..SizeOf(Integer)*8-1;
  40. LongRec = packed record
  41. case Integer of
  42. {$ifdef FPC_LITTLE_ENDIAN}
  43. 0 : (Lo,Hi : Word);
  44. {$else FPC_LITTLE_ENDIAN}
  45. 0 : (Hi,Lo : Word);
  46. {$endif FPC_LITTLE_ENDIAN}
  47. 1 : (Bytes : Array[0..3] of Byte);
  48. end;
  49. WordRec = packed record
  50. {$ifdef FPC_LITTLE_ENDIAN}
  51. Lo,Hi : Byte;
  52. {$else FPC_LITTLE_ENDIAN}
  53. Hi,Lo : Byte;
  54. {$endif FPC_LITTLE_ENDIAN}
  55. end;
  56. Int64Rec = packed record
  57. case integer of
  58. {$ifdef FPC_LITTLE_ENDIAN}
  59. 0 : (Lo,Hi : Cardinal);
  60. {$else FPC_LITTLE_ENDIAN}
  61. 0 : (Hi,Lo : Cardinal);
  62. {$endif FPC_LITTLE_ENDIAN}
  63. 1 : (Words : Array[0..3] of Word);
  64. 2 : (Bytes : Array[0..7] of Byte);
  65. end;
  66. Int128Rec = packed record
  67. case integer of
  68. {$ifdef FPC_LITTLE_ENDIAN}
  69. 0 : (Lo,Hi : QWord);
  70. {$else FPC_LITTLE_ENDIAN}
  71. 0 : (Hi,Lo : QWord);
  72. {$endif FPC_LITTLE_ENDIAN}
  73. 1 : (DWords : Array[0..3] of DWord);
  74. 2 : (Words : Array[0..7] of Word);
  75. 3 : (Bytes : Array[0..15] of Byte);
  76. end;
  77. OWordRec = packed record
  78. case integer of
  79. {$ifdef FPC_LITTLE_ENDIAN}
  80. 0 : (Lo,Hi : QWord);
  81. {$else FPC_LITTLE_ENDIAN}
  82. 0 : (Hi,Lo : QWord);
  83. {$endif FPC_LITTLE_ENDIAN}
  84. 1 : (DWords : Array[0..3] of DWord);
  85. 2 : (Words : Array[0..7] of Word);
  86. 3 : (Bytes : Array[0..15] of Byte);
  87. end;
  88. PByteArray = ^TByteArray;
  89. TByteArray = Array[0..{$ifdef CPU16}32766{$else}32767{$endif}] of Byte;
  90. PWordarray = ^TWordArray;
  91. TWordArray = array[0..{$ifdef CPU16}16382{$else}16383{$endif}] of Word;
  92. TBytes = array of Byte;
  93. { exceptions }
  94. Exception = class(TObject)
  95. private
  96. fmessage : string;
  97. fhelpcontext : longint;
  98. public
  99. constructor Create(const msg : string);
  100. constructor CreateFmt(const msg : string; const args : array of const);
  101. constructor CreateRes(ResString: PResStringRec);
  102. constructor CreateResFmt(ResString: PResStringRec; const Args: array of const);
  103. constructor CreateHelp(const Msg: string; AHelpContext: Longint);
  104. constructor CreateFmtHelp(const Msg: string; const Args: array of const;
  105. AHelpContext: Longint);
  106. constructor CreateResHelp(ResString: PResStringRec; AHelpContext: Longint);
  107. constructor CreateResFmtHelp(ResString: PResStringRec; const Args: array of const;
  108. AHelpContext: Longint);
  109. Function ToString : RTLString; override;
  110. function GetBaseException : Exception;virtual;
  111. property HelpContext : longint read fhelpcontext write fhelpcontext;
  112. property Message : string read fmessage write fmessage;
  113. end;
  114. ExceptClass = class of Exception;
  115. EExternal = class(Exception)
  116. {$if defined(win32) or defined(win64) or defined(wince)}
  117. { OS-provided exception record is stored on stack and has very limited lifetime.
  118. Therefore store a complete copy. }
  119. private
  120. FExceptionRecord: TExceptionRecord;
  121. function GetExceptionRecord: PExceptionRecord;
  122. public
  123. property ExceptionRecord : PExceptionRecord read GetExceptionRecord;
  124. {$endif win32 or win64 or wince}
  125. end;
  126. { integer math exceptions }
  127. EIntError = Class(EExternal);
  128. EDivByZero = Class(EIntError);
  129. ERangeError = Class(EIntError);
  130. EIntOverflow = Class(EIntError);
  131. { General math errors }
  132. EMathError = Class(EExternal);
  133. EInvalidOp = Class(EMathError);
  134. EZeroDivide = Class(EMathError);
  135. EOverflow = Class(EMathError);
  136. EUnderflow = Class(EMathError);
  137. { Run-time and I/O Errors }
  138. EInOutError = class(Exception)
  139. public
  140. ErrorCode : Integer;
  141. end;
  142. EHeapMemoryError = class(Exception)
  143. protected
  144. AllowFree : boolean;
  145. public
  146. procedure FreeInstance;override;
  147. end;
  148. EHeapException = EHeapMemoryError;
  149. EExternalException = class(EExternal);
  150. EInvalidPointer = Class(EHeapMemoryError);
  151. EOutOfMemory = Class(EHeapMemoryError);
  152. EInvalidCast = Class(Exception);
  153. EVariantError = Class(Exception)
  154. ErrCode : longint;
  155. Constructor CreateCode(Code : Longint);
  156. end;
  157. EAccessViolation = Class(EExternal);
  158. EBusError = Class(EAccessViolation);
  159. EPrivilege = class(EExternal);
  160. EStackOverflow = class(EExternal);
  161. EControlC = class(EExternal);
  162. { String conversion errors }
  163. EConvertError = class(Exception);
  164. EFormatError = class(Exception);
  165. { Other errors }
  166. EAbort = Class(Exception);
  167. EAbstractError = Class(Exception);
  168. EAssertionFailed = Class(Exception);
  169. EObjectCheck = Class(Exception);
  170. EThreadError = Class(Exception);
  171. ESigQuit = Class(Exception);
  172. EPropReadOnly = class(Exception);
  173. EPropWriteOnly = class(Exception);
  174. EIntfCastError = class(Exception);
  175. EInvalidContainer = class(Exception);
  176. EInvalidInsert = class(Exception);
  177. EPackageError = class(Exception);
  178. ECFError = class(Exception);
  179. EOSError = class(Exception)
  180. public
  181. ErrorCode: Longint;
  182. end;
  183. ESafecallException = class(Exception);
  184. ENoThreadSupport = Class(Exception);
  185. ENoWideStringSupport = Class(Exception);
  186. ENoDynLibsSupport = class(Exception);
  187. EProgrammerNotFound = class(Exception);
  188. EMonitor = class(Exception);
  189. EMonitorLockException = class(EMonitor);
  190. ENoMonitorSupportException = class(EMonitor);
  191. EObjectDisposed = class(Exception);
  192. ENotImplemented = class(Exception);
  193. EArgumentException = class(Exception);
  194. EArgumentOutOfRangeException = class(EArgumentException);
  195. EArgumentNilException = class(EArgumentException);
  196. EPathTooLongException = class(Exception);
  197. ENotSupportedException = class(Exception);
  198. EDirectoryNotFoundException = class(Exception);
  199. EFileNotFoundException = class(Exception);
  200. EPathNotFoundException = class(Exception);
  201. EInOutArgumentException = class(EArgumentException)
  202. public
  203. Path: string;
  204. constructor Create(const aMsg, aPath: string); overload;
  205. constructor CreateRes(ResStringRec: PResStringRec; const aPath: string); overload;
  206. constructor CreateFmt(const fmt : string; const args : array of const; const aPath : String); overload;
  207. end;
  208. EInvalidOpException = class(Exception);
  209. ENoConstructException = class(Exception);
  210. EListError = Class(Exception);
  211. EOperationCancelled = class(Exception);
  212. { Exception handling routines }
  213. function ExceptObject: TObject;
  214. function ExceptAddr: CodePointer;
  215. function ExceptFrameCount: Longint;
  216. function ExceptFrames: PCodePointer;
  217. function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
  218. Buffer: PAnsiChar; Size: Integer): Integer;
  219. procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
  220. procedure Abort;
  221. procedure OutOfMemoryError;
  222. procedure ListIndexError(aIndex,aMax: Integer; aObj: TObject);
  223. Type
  224. TBeepHandler = Procedure;
  225. Var
  226. OnBeep : TBeephandler = Nil;
  227. procedure Beep;
  228. function SysErrorMessage(ErrorCode: Integer): String;
  229. Type
  230. TCreateGUIDFunc = Function(Out GUID : TGUID) : Integer;
  231. Var
  232. OnCreateGUID : TCreateGUIDFunc = Nil;
  233. Function CreateGUID(out GUID : TGUID) : Integer;
  234. type
  235. TTerminateProc = Function: Boolean;
  236. procedure AddTerminateProc(TermProc: TTerminateProc);
  237. function CallTerminateProcs: Boolean;
  238. {$IFNDEF VER3_0}
  239. generic function IfThen<T>(val:boolean;const iftrue:T; const iffalse:T) :T; inline; overload;
  240. generic function Exchange<T>(var target:T; const newvalue:T) :T; inline;
  241. {$ENDIF}
  242. Var
  243. OnShowException : Procedure (Msg : ShortString);
  244. Const
  245. HexDisplayPrefix : string = '$';
  246. const
  247. // commenting is VP fix. These idents are in a different unit there.
  248. PathDelim={System.}DirectorySeparator;
  249. DriveDelim={System.}DriveSeparator;
  250. PathSep={System.}PathSeparator;
  251. MAX_PATH={System.}MaxPathLen;
  252. Type
  253. TFileRec=FileRec;
  254. TTextRec=TextRec;
  255. { Read PAnsiChar handling functions declaration }
  256. {$i syspchh.inc}
  257. { MCBS functions }
  258. {$i sysansih.inc}
  259. {$i syscodepagesh.inc}
  260. { wide string functions }
  261. {$i syswideh.inc}
  262. {$ifdef FPC_HAS_UNICODESTRING}
  263. { unicode string functions }
  264. {$i sysunih.inc}
  265. {$i sysencodingh.inc}
  266. {$endif FPC_HAS_UNICODESTRING}
  267. {$macro on}
  268. {$define PathStr:=UnicodeString}
  269. {$define PathPChar:=PUnicodeChar}
  270. { Read filename handling functions declaration }
  271. {$i finah.inc}
  272. {$define PathStr:=RawByteString}
  273. {$define PathPChar:=PAnsiChar}
  274. { Read filename handling functions declaration }
  275. {$i finah.inc}
  276. {$undef PathStr}
  277. {$undef PathPChar}
  278. { Read other file handling function declarations }
  279. {$i filutilh.inc}
  280. { Read disk function declarations }
  281. {$i diskh.inc}
  282. { read thread handling }
  283. {$i systhrdh.inc}
  284. { Type Helpers}
  285. {$i syshelph.inc}
  286. procedure FreeAndNil(var obj);
  287. procedure FreeMemAndNil(var p);
  288. { interface handling }
  289. {$i intfh.inc}
  290. {$IFDEF FPC_HAS_FEATURE_UNICODESTRINGS}
  291. { strange Delphi thing }
  292. {$i sysmarshalh.inc}
  293. {$ENDIF}
  294. function SafeLoadLibrary(const FileName: AnsiString;
  295. ErrorMode: DWord = {$ifdef windows}SEM_NOOPENFILEERRORBOX{$else windows}0{$endif windows}): HMODULE;
  296. function GetModuleName(Module: HMODULE): string;
  297. { some packages and unit related constants for compatibility }
  298. const
  299. pfExeModule = $00000000;
  300. pfNeverBuild = $00000001;
  301. pfDesignOnly = $00000002;
  302. pfRunOnly = $00000004;
  303. pfIgnoreDupUnits = $00000008;
  304. pfPackageModule = $40000000;
  305. pfModuleTypeMask = $C0000000;
  306. pfV3Produced = $00000000;
  307. pfProducerUndefined = $04000000;
  308. pfBCB4Produced = $08000000;
  309. pfDelphi4Produced = $0C000000;
  310. pfLibraryModule = $80000000;
  311. pfProducerMask = $0C000000;
  312. const
  313. ufMainUnit = $01;
  314. ufPackageUnit = $02;
  315. ufWeakUnit = $04;
  316. ufOrgWeakUnit = $08;
  317. ufImplicitUnit = $10;
  318. ufWeakPackageUnit = ufPackageUnit or ufWeakUnit;
  319. Type
  320. TUTF8StringDynArray = Array of UTF8String;
  321. Function ArrayOfConstToStr(Args: array of const ; aSeparator : Char = ','; aQuoteBegin : Char = '"'; aQuoteEnd : Char = '"') : UTF8String;
  322. Function ArrayOfConstToStrArray(Args: array of const) : TUTF8StringDynArray;
  323. { Delphi compatibility }
  324. Type
  325. { TOSVersion }
  326. TOSVersion = record
  327. public type
  328. TArchitecture = (arIntelX86, arIntelX64, arARM32, arARM64, arIntelX16, arXTensa, arAVR, arM68k, arPowerPC, arPower64,
  329. arMips, arMipsel, arMips64, arMips64el, arJVM, arWasm32, arSparc, arSparc64, arRiscV32, arRiscV64, arZ80,
  330. arLoongArch64, arOther);
  331. TPlatform = (pfWindows, pfMacOS, pfiOS, pfAndroid, pfWinRT, pfLinux,
  332. pfGo32v2, pfOS2, pfFreeBSD, pfBeos, pfNetBSD, pfAmiga,
  333. pfAtari, pfSolaris, pfQNX, pfNetware, pfOpenBSD, pfWDosX,
  334. pfPalmos, pfMacOSClassic, pfDarwin, pfEMX, pfWatcom,
  335. pfMorphos, pfNetwLibC, pfWinCE, pfGBA, pfNDS, pfEmbedded,
  336. pfSymbian, pfHaiku, pfIPhoneSim, pfAIX, pfJava,
  337. pfNativeNT, pfMSDos, pfWII, pfAROS, pfDragonFly, pfWin16,
  338. pfFreeRTOS, pfZXSpectrum, pfMSXDOS, pfAmstradCPC,
  339. pfSinclairQL, pfWasi, pfOther);
  340. TDelphiPlatform = TPlatform.pfWindows..TPlatform.pfLinux;
  341. public const
  342. AllArchitectures = [Low(TArchitecture) .. High(TArchitecture)];
  343. AllPlatforms = [Low(TPlatform) .. High(TPlatform)];
  344. private
  345. class var FFull : string;
  346. class var FArchitecture: TArchitecture;
  347. class var FBuild: Integer;
  348. class var FMajor: Integer;
  349. class var FMinor: Integer;
  350. class var FName: string;
  351. class var FPlatform: TPlatform;
  352. class var FServicePackMajor: Integer;
  353. class var FServicePackMinor: Integer;
  354. {$IFDEF LINUX}
  355. class var FPrettyName: string;
  356. class var FLibCVersionMajor: Integer;
  357. class var FLibCVersionMinor: Integer;
  358. {$ENDIF LINUX}
  359. class constructor Create;
  360. public
  361. class function Check(AMajor: Integer): Boolean; overload; static; inline;
  362. class function Check(AMajor, AMinor: Integer): Boolean; overload; static; inline;
  363. class function Check(AMajor, AMinor, AServicePackMajor: Integer): Boolean; overload; static; inline;
  364. class function ToString: string; static;
  365. class property Architecture: TArchitecture read FArchitecture;
  366. class property Build: Integer read FBuild;
  367. class property Major: Integer read FMajor;
  368. class property Minor: Integer read FMinor;
  369. class property Name: string read FName;
  370. class property Platform: TPlatform read FPlatform;
  371. class property ServicePackMajor: Integer read FServicePackMajor;
  372. class property ServicePackMinor: Integer read FServicePackMinor;
  373. {$IFDEF LINUX}
  374. class property PrettyName: string read FPrettyName;
  375. class property LibCVersionMajor: Integer read FLibCVersionMajor;
  376. class property LibCVersionMinor: Integer read FLibCVersionMinor;
  377. {$ENDIF LINUX}
  378. end;
  379. TArchitectures = set of TOSVersion.TArchitecture;
  380. TPlatforms = set of TOSVersion.TPlatform;
  381. TDelphiPlatforms = set of TOSVersion.TDelphiPlatform;
  382. Function GetCompiledArchitecture : TOSVersion.TArchitecture;
  383. Function GetCompiledPlatform : TOSVersion.TPlatform;
  384. {$IFNDEF HAS_INVALIDHANDLE}
  385. const
  386. INVALID_HANDLE_VALUE = DWORD(-1);
  387. {$ENDIF}