common.pas 21 KB

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