common.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. {********************[ COMMON UNIT ]***********************}
  2. { }
  3. { System independent COMMON TYPES & DEFINITIONS }
  4. { }
  5. { Parts Copyright (c) 1997 by Balazs Scheidler }
  6. { [email protected] }
  7. { }
  8. { Parts Copyright (c) 1999 by Leon de Boer }
  9. { [email protected] - primary e-mail address }
  10. { [email protected] - backup e-mail address }
  11. { }
  12. {****************[ THIS CODE IS FREEWARE ]*****************}
  13. { }
  14. { This sourcecode is released for the purpose to }
  15. { promote the pascal language on all platforms. You may }
  16. { redistribute it and/or modify with the following }
  17. { DISCLAIMER. }
  18. { }
  19. { This SOURCE CODE is distributed "AS IS" WITHOUT }
  20. { WARRANTIES AS TO PERFORMANCE OF MERCHANTABILITY OR }
  21. { ANY OTHER WARRANTIES WHETHER EXPRESSED OR IMPLIED. }
  22. { }
  23. {*****************[ SUPPORTED PLATFORMS ]******************}
  24. { 16 and 32 Bit compilers }
  25. { DOS - Turbo Pascal 7.0 + (16 Bit) }
  26. { DPMI - Turbo Pascal 7.0 + (16 Bit) }
  27. { - FPC 0.9912+ (GO32V2) (32 Bit) }
  28. { WINDOWS - Turbo Pascal 7.0 + (16 Bit) }
  29. { - Delphi 1.0+ (16 Bit) }
  30. { WIN95/NT - Delphi 2.0+ (32 Bit) }
  31. { - Virtual Pascal 2.0+ (32 Bit) }
  32. { - Speedsoft Sybil 2.0+ (32 Bit) }
  33. { - FPC 0.9912+ (32 Bit) }
  34. { OS2 - Virtual Pascal 1.0+ (32 Bit) }
  35. { - Speed Pascal 1.0+ (32 Bit) }
  36. { - C'T patch to BP (16 Bit) }
  37. { }
  38. {******************[ REVISION HISTORY ]********************}
  39. { Version Date Who Fix }
  40. { ------- -------- --- ---------------------------- }
  41. { 0.1 12 Jul 97 Bazsi Initial implementation }
  42. { 0.2 18 Jul 97 Bazsi Linux specific error codes }
  43. { 0.2.2 28 Jul 97 Bazsi Base error code for Video }
  44. { 0.2.3 29 Jul 97 Bazsi Basic types added (PByte etc) }
  45. { 0.2.5 08 Aug 97 Bazsi Error handling code added }
  46. { 0.2.6 06 Sep 97 Bazsi Base code for keyboard }
  47. { 0.2.7 06 Nov 97 Bazsi Base error code for filectrl }
  48. { 0.2.8 21 Jan 99 LdB Max data sizes added. }
  49. { 0.2.9 22 Jan 99 LdB General array types added. }
  50. { 0.3.0 27 Oct 99 LdB Delphi3+ MaxAvail, MemAvail }
  51. {**********************************************************}
  52. UNIT Common;
  53. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  54. INTERFACE
  55. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  56. {====Include file to sort compiler platform out =====================}
  57. {$I Platform.inc}
  58. {====================================================================}
  59. {***************************************************************************}
  60. { PUBLIC CONSTANTS }
  61. {***************************************************************************}
  62. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  63. { SYSTEM ERROR BASE CONSTANTS }
  64. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  65. {---------------------------------------------------------------------------}
  66. { The following ranges have been defined for error codes: }
  67. {---------------------------------------------------------------------------}
  68. { 0 - 1000 OS dependant error codes }
  69. { 1000 - 10000 API reserved error codes }
  70. { 10000 - Add-On unit error codes }
  71. {---------------------------------------------------------------------------}
  72. { Before anyone adding a unit, contact [email protected] to assign a base }
  73. { error code, to avoid collisions. }
  74. {---------------------------------------------------------------------------}
  75. {---------------------------------------------------------------------------}
  76. { DEFINED BASE ERROR CONSTANTS }
  77. {---------------------------------------------------------------------------}
  78. CONST
  79. errOk = 0; { No error }
  80. errVioBase = 1000; { Video base offset }
  81. errKbdBase = 1010; { Keyboard base offset }
  82. errFileCtrlBase = 1020; { File IO base offset }
  83. errMouseBase = 1030; { Mouse base offset }
  84. {---------------------------------------------------------------------------}
  85. { MAXIUM DATA SIZES }
  86. {---------------------------------------------------------------------------}
  87. CONST
  88. {$IFDEF BIT_16} { 16 BIT DEFINITION }
  89. MaxBytes = 65520; { Maximum data size }
  90. {$ENDIF}
  91. {$IFDEF BIT_32} { 32 BIT DEFINITION }
  92. MaxBytes = 128*1024*1024; { Maximum data size }
  93. {$ENDIF}
  94. MaxWords = MaxBytes DIV SizeOf(Word); { Max words }
  95. MaxInts = MaxBytes DIV SizeOf(Integer); { Max integers }
  96. MaxLongs = MaxBytes DIV SizeOf(LongInt); { Max longints }
  97. MaxPtrs = MaxBytes DIV SizeOf(Pointer); { Max pointers }
  98. MaxReals = MaxBytes DIV SizeOf(Real); { Max reals }
  99. MaxStr = MaxBytes DIV SizeOf(String); { Max strings }
  100. {***************************************************************************}
  101. { PUBLIC TYPE DEFINITIONS }
  102. {***************************************************************************}
  103. {---------------------------------------------------------------------------}
  104. { CPU TYPE DEFINITIONS }
  105. {---------------------------------------------------------------------------}
  106. TYPE
  107. {$IFDEF BIT_32} { 32 BIT CODE }
  108. CPUWord = Longint; { CPUWord is 32 bit }
  109. CPUInt = Longint; { CPUInt is 32 bit }
  110. {$ELSE} { 16 BIT CODE }
  111. CPUWord = Word; { CPUWord is 16 bit }
  112. CPUInt = Integer; { CPUInt is 16 bit }
  113. {$ENDIF}
  114. {---------------------------------------------------------------------------}
  115. { 16/32 BIT SWITCHED TYPE CONSTANTS }
  116. {---------------------------------------------------------------------------}
  117. TYPE
  118. {$IFDEF BIT_16} { 16 BIT DEFINITIONS }
  119. Sw_Word = Word; { Standard word }
  120. Sw_Integer = Integer; { Standard integer }
  121. {$ENDIF}
  122. {$IFDEF BIT_32} { 32 BIT DEFINITIONS }
  123. Sw_Word = LongInt; { Long integer now }
  124. Sw_Integer = LongInt; { Long integer now }
  125. {$ENDIF}
  126. {---------------------------------------------------------------------------}
  127. { POINTERS TO STANDARD DATA TYPES }
  128. {---------------------------------------------------------------------------}
  129. TYPE
  130. PByte = ^Byte; { Pointer to byte }
  131. PWord = ^Word; { Pointer to word }
  132. PLongint = ^Longint; { Pointer to longint }
  133. {---------------------------------------------------------------------------}
  134. { GENERAL ARRAYS }
  135. {---------------------------------------------------------------------------}
  136. TYPE
  137. TByteArray = ARRAY [0..MaxBytes-1] Of Byte; { Byte array }
  138. PByteArray = ^TByteArray; { Byte array pointer }
  139. TWordArray = ARRAY [0..MaxWords-1] Of Word; { Word array }
  140. PWordArray = ^TWordArray; { Word array pointer }
  141. TIntegerArray = ARRAY [0..MaxInts-1] Of Integer; { Integer array }
  142. PIntegerArray = ^TIntegerArray; { Integer array pointer }
  143. TLongIntArray = ARRAY [0..MaxLongs-1] Of LongInt; { LongInt array }
  144. PLongIntArray = ^TLongIntArray; { LongInt array pointer }
  145. TRealArray = Array [0..MaxReals-1] Of Real; { Real array }
  146. PRealarray = ^TRealArray; { Real array pointer }
  147. TPointerArray = Array [0..MaxPtrs-1] Of Pointer; { Pointer array }
  148. PPointerArray = ^TPointerArray; { Pointer array ptr }
  149. TStrArray = Array [0..MaxStr-1] Of String; { String array }
  150. PStrArray = ^TStrArray; { String array ptr }
  151. {***************************************************************************}
  152. { INTERFACE ROUTINES }
  153. {***************************************************************************}
  154. {-GetErrorCode-------------------------------------------------------
  155. Returns the last error code and resets ErrorCode to errOk.
  156. 07/12/97 Bazsi
  157. ---------------------------------------------------------------------}
  158. FUNCTION GetErrorCode: LongInt;
  159. {-GetErrorInfo-------------------------------------------------------
  160. Returns the info assigned to the previous error, doesn't reset the
  161. value to nil. Would usually only be called if ErrorCode <> errOk.
  162. 07/12/97 Bazsi
  163. ---------------------------------------------------------------------}
  164. FUNCTION GetErrorInfo: Pointer;
  165. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  166. { MASK CONTROL ROUTINES }
  167. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  168. {-SetWordMask--------------------------------------------------------
  169. Sets the bits provided in the mask word in the passed word variable.
  170. 20Sep99 LdB
  171. ---------------------------------------------------------------------}
  172. PROCEDURE SetWordMask (Var WordVar: Word; Mask: Word);
  173. {-SetWordMask--------------------------------------------------------
  174. Clears the bits provided in the mask word in the passed word variable.
  175. 20Sep99 LdB
  176. ---------------------------------------------------------------------}
  177. PROCEDURE ClrWordMask (Var WordVar: Word; Mask: Word);
  178. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  179. { MINIMUM AND MAXIMUM ROUTINES }
  180. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  181. {-MinimumOf----------------------------------------------------------
  182. Given two real numbers returns the minimum real of the two.
  183. 04Oct99 LdB
  184. ---------------------------------------------------------------------}
  185. FUNCTION MinimumOf (A, B: Real): Real;
  186. {-MaximumOf----------------------------------------------------------
  187. Given two real numbers returns the maximum real of the two.
  188. 04Oct99 LdB
  189. ---------------------------------------------------------------------}
  190. FUNCTION MaximumOf (A, B: Real): Real;
  191. {-MinIntegerOf-------------------------------------------------------
  192. Given two integer values returns the lowest integer of the two.
  193. 04Oct99 LdB
  194. ---------------------------------------------------------------------}
  195. FUNCTION MinIntegerOf (A, B: Integer): Integer;
  196. {-MaxIntegerof-------------------------------------------------------
  197. Given two integer values returns the biggest integer of the two.
  198. 04Oct99 LdB
  199. ---------------------------------------------------------------------}
  200. FUNCTION MaxIntegerOf (A, B: Integer): Integer;
  201. {-MinLongIntOf-------------------------------------------------------
  202. Given two long integers returns the minimum longint of the two.
  203. 04Oct99 LdB
  204. ---------------------------------------------------------------------}
  205. FUNCTION MinLongIntOf (A, B: LongInt): LongInt;
  206. {-MaxLongIntOf-------------------------------------------------------
  207. Given two long integers returns the maximum longint of the two.
  208. 04Oct99 LdB
  209. ---------------------------------------------------------------------}
  210. FUNCTION MaxLongIntOf (A, B: LongInt): LongInt;
  211. {$IFDEF PPC_DELPHI3} { DELPHI 3+ CODE }
  212. { ******************************* REMARK ****************************** }
  213. { Delphi 3+ does not define these standard routines so I have made }
  214. { some private functions here to complete compatability. }
  215. { ****************************** END REMARK *** Leon de Boer, 14Aug98 * }
  216. {-MemAvail-----------------------------------------------------------
  217. Returns the free memory available under Delphi 3+.
  218. 14Aug98 LdB
  219. ---------------------------------------------------------------------}
  220. FUNCTION MemAvail: LongInt;
  221. {-MaxAvail-----------------------------------------------------------
  222. Returns the max free memory block size available under Delphi 3+.
  223. 14Aug98 LdB
  224. ---------------------------------------------------------------------}
  225. FUNCTION MaxAvail: LongInt;
  226. {$ENDIF}
  227. {***************************************************************************}
  228. { INITIALIZED PUBLIC VARIABLES }
  229. {***************************************************************************}
  230. {---------------------------------------------------------------------------}
  231. { INITIALIZED DOS/DPMI/WIN/NT/OS2 VARIABLES }
  232. {---------------------------------------------------------------------------}
  233. CONST
  234. ErrorCode: Longint = errOk; { Last error code }
  235. ErrorInfo: Pointer = Nil; { Last error info }
  236. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  237. IMPLEMENTATION
  238. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  239. {$IFDEF PPC_DELPHI3} { DELPHI 3+ COMPILER }
  240. USES WinTypes, WinProcs; { Stardard units }
  241. {$ENDIF}
  242. {***************************************************************************}
  243. { INTERFACE ROUTINES }
  244. {***************************************************************************}
  245. {---------------------------------------------------------------------------}
  246. { GetErrorCode -> Platforms ALL - Updated 12Jul97 Bazsi }
  247. {---------------------------------------------------------------------------}
  248. FUNCTION GetErrorCode: LongInt;
  249. BEGIN
  250. GetErrorCode := ErrorCode; { Return last error }
  251. ErrorCode := 0; { Now clear errorcode }
  252. END;
  253. {---------------------------------------------------------------------------}
  254. { GetErrorInfo -> Platforms ALL - Updated 12Jul97 Bazsi }
  255. {---------------------------------------------------------------------------}
  256. FUNCTION GetErrorInfo: Pointer;
  257. BEGIN
  258. GetErrorInfo := ErrorInfo; { Return errorinfo ptr }
  259. END;
  260. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  261. { MASK CONTROL ROUTINES }
  262. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  263. {---------------------------------------------------------------------------}
  264. { SetWordMask -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 20Sep99 LdB }
  265. {---------------------------------------------------------------------------}
  266. PROCEDURE SetWordMask (Var WordVar: Word; Mask: Word);
  267. BEGIN
  268. WordVar := WordVar OR Mask; { Set the mask bits }
  269. END;
  270. {---------------------------------------------------------------------------}
  271. { ClrWordMask -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 20Sep99 LdB }
  272. {---------------------------------------------------------------------------}
  273. PROCEDURE ClrWordMask (Var WordVar: Word; Mask: Word);
  274. BEGIN
  275. WordVar := WordVar AND NOT Mask; { Clr the mask bits }
  276. END;
  277. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  278. { MINIMUM AND MAXIMUM ROUTINES }
  279. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  280. {---------------------------------------------------------------------------}
  281. { MinimumOf -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 04Oct99 LdB }
  282. {---------------------------------------------------------------------------}
  283. FUNCTION MinimumOf (A, B: Real): Real;
  284. BEGIN
  285. If (B < A) Then MinimumOf := B { B smaller take it }
  286. Else MinimumOf := A; { Else take A }
  287. END;
  288. {---------------------------------------------------------------------------}
  289. { MaximumOf -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 04Oct99 LdB }
  290. {---------------------------------------------------------------------------}
  291. FUNCTION MaximumOf (A, B: Real): Real;
  292. BEGIN
  293. If (B > A) Then MaximumOf := B { B bigger take it }
  294. Else MaximumOf := A; { Else take A }
  295. END;
  296. {---------------------------------------------------------------------------}
  297. { MinIntegerOf -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 04Oct99 LdB }
  298. {---------------------------------------------------------------------------}
  299. FUNCTION MinIntegerOf (A, B: Integer): Integer;
  300. BEGIN
  301. If (B < A) Then MinIntegerOf := B { B smaller take it }
  302. Else MinIntegerOf := A; { Else take A }
  303. END;
  304. {---------------------------------------------------------------------------}
  305. { MaxIntegerOf -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 04Oct99 LdB }
  306. {---------------------------------------------------------------------------}
  307. FUNCTION MaxIntegerOf (A, B: Integer): Integer;
  308. BEGIN
  309. If (B > A) Then MaxIntegerOf := B { B bigger take it }
  310. Else MaxIntegerOf := A; { Else take A }
  311. END;
  312. {---------------------------------------------------------------------------}
  313. { MinLongIntOf -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 04Oct99 LdB }
  314. {---------------------------------------------------------------------------}
  315. FUNCTION MinLongIntOf (A, B: LongInt): LongInt;
  316. BEGIN
  317. If (B < A) Then MinLongIntOf := B { B smaller take it }
  318. Else MinLongIntOf := A; { Else take A }
  319. END;
  320. {---------------------------------------------------------------------------}
  321. { MaxLongIntOf -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 04Oct99 LdB }
  322. {---------------------------------------------------------------------------}
  323. FUNCTION MaxLongIntOf (A, B: LongInt): LongInt;
  324. BEGIN
  325. If (B > A) Then MaxLongIntOf := B { B bigger take it }
  326. Else MaxLongIntOf := A; { Else take A }
  327. END;
  328. {$IFDEF PPC_DELPHI3} { DELPHI 3+ CODE }
  329. {---------------------------------------------------------------------------}
  330. { MemAvail -> Platforms WIN/NT - Updated 14Aug98 LdB }
  331. {---------------------------------------------------------------------------}
  332. FUNCTION MemAvail: LongInt;
  333. VAR Ms: TMemoryStatus;
  334. BEGIN
  335. GlobalMemoryStatus(Ms); { Get memory status }
  336. MemAvail := Ms.dwAvailPhys; { Avail physical memory }
  337. END;
  338. {---------------------------------------------------------------------------}
  339. { MaxAvail -> Platforms WIN/NT - Updated 14Aug98 LdB }
  340. {---------------------------------------------------------------------------}
  341. FUNCTION MaxAvail: LongInt;
  342. VAR Ms: TMemoryStatus;
  343. BEGIN
  344. GlobalMemoryStatus(Ms); { Get memory status }
  345. MaxAvail := Ms.dwTotalPhys; { Max physical memory }
  346. END;
  347. {$ENDIF}
  348. END.