fvcommon.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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, 2000 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. { 0.4.0 14 Nov 00 LdB Revamp of whole unit }
  52. {**********************************************************}
  53. {$IFNDEF FPC_DOTTEDUNITS}
  54. {$ifdef FV_UNICODE}
  55. UNIT UFVCommon;
  56. {$else FV_UNICODE}
  57. UNIT FVCommon;
  58. {$endif FV_UNICODE}
  59. {$ENDIF FPC_DOTTEDUNITS}
  60. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  61. INTERFACE
  62. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  63. {====Include file to sort compiler platform out =====================}
  64. {$I platform.inc}
  65. {====================================================================}
  66. {$IFDEF FPC_DOTTEDUNITS}
  67. {$ifdef OS_WINDOWS}
  68. uses
  69. WinApi.Windows;
  70. {$endif}
  71. {$ELSE FPC_DOTTEDUNITS}
  72. {$ifdef OS_WINDOWS}
  73. uses
  74. Windows;
  75. {$endif}
  76. {$ENDIF FPC_DOTTEDUNITS}
  77. {***************************************************************************}
  78. { PUBLIC CONSTANTS }
  79. {***************************************************************************}
  80. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  81. { SYSTEM ERROR BASE CONSTANTS }
  82. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  83. {---------------------------------------------------------------------------}
  84. { The following ranges have been defined for error codes: }
  85. {---------------------------------------------------------------------------}
  86. { 0 - 1000 OS dependant error codes }
  87. { 1000 - 10000 API reserved error codes }
  88. { 10000 - Add-On unit error codes }
  89. {---------------------------------------------------------------------------}
  90. {---------------------------------------------------------------------------}
  91. { DEFINED BASE ERROR CONSTANTS }
  92. {---------------------------------------------------------------------------}
  93. CONST
  94. errOk = 0; { No error }
  95. errVioBase = 1000; { Video base offset }
  96. errKbdBase = 1010; { Keyboard base offset }
  97. errFileCtrlBase = 1020; { File IO base offset }
  98. errMouseBase = 1030; { Mouse base offset }
  99. {---------------------------------------------------------------------------}
  100. { MAXIUM DATA SIZES }
  101. {---------------------------------------------------------------------------}
  102. CONST
  103. {$IFDEF BIT_16} { 16 BIT DEFINITION }
  104. MaxBytes = 65520; { Maximum data size }
  105. {$ENDIF}
  106. {$IFDEF BIT_32_OR_MORE} { 32 BIT DEFINITION }
  107. MaxBytes = 128*1024*1024; { Maximum data size }
  108. {$ENDIF}
  109. MaxWords = MaxBytes DIV SizeOf(Word); { Max words }
  110. MaxInts = MaxBytes DIV SizeOf(SmallInt); { Max integers }
  111. MaxLongs = MaxBytes DIV SizeOf(LongInt); { Max longints }
  112. MaxPtrs = MaxBytes DIV SizeOf(Pointer); { Max pointers }
  113. MaxReals = MaxBytes DIV SizeOf(Real); { Max reals }
  114. MaxStr = MaxBytes DIV SizeOf(String); { Max strings }
  115. {***************************************************************************}
  116. { PUBLIC TYPE DEFINITIONS }
  117. {***************************************************************************}
  118. {---------------------------------------------------------------------------}
  119. { CPU TYPE DEFINITIONS }
  120. {---------------------------------------------------------------------------}
  121. TYPE
  122. {$IFDEF BIT_32_OR_MORE} { 32 BIT CODE }
  123. CPUWord = Longint; { CPUWord is 32 bit }
  124. CPUInt = Longint; { CPUInt is 32 bit }
  125. {$ELSE} { 16 BIT CODE }
  126. CPUWord = Word; { CPUWord is 16 bit }
  127. CPUInt = SmallInt; { CPUInt is 16 bit }
  128. {$ENDIF}
  129. {---------------------------------------------------------------------------}
  130. { 16/32 BIT SWITCHED TYPE CONSTANTS }
  131. {---------------------------------------------------------------------------}
  132. TYPE
  133. {$IFDEF BIT_16} { 16 BIT DEFINITIONS }
  134. Sw_Word = Word; { Standard word }
  135. Sw_Integer = SmallInt; { Standard SmallInt }
  136. {$ENDIF}
  137. {$IFDEF BIT_32_OR_MORE} { 32 BIT DEFINITIONS }
  138. Sw_Word = Cardinal; { Long integer now }
  139. Sw_Integer = LongInt; { Long integer now }
  140. {$ENDIF}
  141. {---------------------------------------------------------------------------}
  142. { SHORT/ANSI/UNICODE SWITCHED STRING TYPE }
  143. {---------------------------------------------------------------------------}
  144. {$IFDEF FV_UNICODE}
  145. TYPE
  146. Sw_String = UnicodeString;
  147. Sw_Char = WideChar;
  148. Sw_PString = UnicodeString;
  149. Sw_ExtendedGraphemeCluster = UnicodeString;
  150. CONST
  151. Sw_PString_Empty = '';
  152. {$ELSE FV_UNICODE}
  153. TYPE
  154. Sw_String = ShortString;
  155. Sw_Char = AnsiChar;
  156. Sw_PString = PShortString;
  157. Sw_ExtendedGraphemeCluster = AnsiChar;
  158. CONST
  159. Sw_PString_Empty = Nil;
  160. {$ENDIF FV_UNICODE}
  161. {---------------------------------------------------------------------------}
  162. { GENERAL ARRAYS }
  163. {---------------------------------------------------------------------------}
  164. TYPE
  165. TByteArray = ARRAY [0..MaxBytes-1] Of Byte; { Byte array }
  166. PByteArray = ^TByteArray; { Byte array pointer }
  167. TWordArray = ARRAY [0..MaxWords-1] Of Word; { Word array }
  168. PWordArray = ^TWordArray; { Word array pointer }
  169. TIntegerArray = ARRAY [0..MaxInts-1] Of SmallInt; { SmallInt array }
  170. PIntegerArray = ^TIntegerArray; { SmallInt array pointer }
  171. TLongIntArray = ARRAY [0..MaxLongs-1] Of LongInt; { LongInt array }
  172. PLongIntArray = ^TLongIntArray; { LongInt array pointer }
  173. TRealArray = Array [0..MaxReals-1] Of Real; { Real array }
  174. PRealarray = ^TRealArray; { Real array pointer }
  175. TPointerArray = Array [0..MaxPtrs-1] Of Pointer; { Pointer array }
  176. PPointerArray = ^TPointerArray; { Pointer array ptr }
  177. TStrArray = Array [0..MaxStr-1] Of String; { String array }
  178. PStrArray = ^TStrArray; { String array ptr }
  179. {***************************************************************************}
  180. { INTERFACE ROUTINES }
  181. {***************************************************************************}
  182. {-GetErrorCode-------------------------------------------------------
  183. Returns the last error code and resets ErrorCode to errOk.
  184. 07/12/97 Bazsi
  185. ---------------------------------------------------------------------}
  186. FUNCTION GetErrorCode: LongInt;
  187. {-GetErrorInfo-------------------------------------------------------
  188. Returns the info assigned to the previous error, doesn't reset the
  189. value to nil. Would usually only be called if ErrorCode <> errOk.
  190. 07/12/97 Bazsi
  191. ---------------------------------------------------------------------}
  192. FUNCTION GetErrorInfo: Pointer;
  193. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  194. { MINIMUM AND MAXIMUM ROUTINES }
  195. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  196. FUNCTION Min (I, J: Sw_Integer): Sw_Integer;
  197. FUNCTION Max (I, J: Sw_Integer): Sw_Integer;
  198. {-MinimumOf----------------------------------------------------------
  199. Given two real numbers returns the minimum real of the two.
  200. 04Oct99 LdB
  201. ---------------------------------------------------------------------}
  202. FUNCTION MinimumOf (A, B: Real): Real;
  203. {-MaximumOf----------------------------------------------------------
  204. Given two real numbers returns the maximum real of the two.
  205. 04Oct99 LdB
  206. ---------------------------------------------------------------------}
  207. FUNCTION MaximumOf (A, B: Real): Real;
  208. {-MinIntegerOf-------------------------------------------------------
  209. Given two SmallInt values returns the lowest SmallInt of the two.
  210. 04Oct99 LdB
  211. ---------------------------------------------------------------------}
  212. FUNCTION MinIntegerOf (A, B: SmallInt): SmallInt;
  213. {-MaxIntegerof-------------------------------------------------------
  214. Given two SmallInt values returns the biggest SmallInt of the two.
  215. 04Oct99 LdB
  216. ---------------------------------------------------------------------}
  217. FUNCTION MaxIntegerOf (A, B: SmallInt): SmallInt;
  218. {-MinLongIntOf-------------------------------------------------------
  219. Given two long integers returns the minimum longint of the two.
  220. 04Oct99 LdB
  221. ---------------------------------------------------------------------}
  222. FUNCTION MinLongIntOf (A, B: LongInt): LongInt;
  223. {-MaxLongIntOf-------------------------------------------------------
  224. Given two long integers returns the maximum longint of the two.
  225. 04Oct99 LdB
  226. ---------------------------------------------------------------------}
  227. FUNCTION MaxLongIntOf (A, B: LongInt): LongInt;
  228. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  229. { MISSING DELPHI3 ROUTINES }
  230. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  231. { ******************************* REMARK ****************************** }
  232. { Delphi 3+ does not define these standard routines so I have made }
  233. { some public functions here to complete compatability. }
  234. { ****************************** END REMARK *** Leon de Boer, 14Aug98 * }
  235. {-MemAvail-----------------------------------------------------------
  236. Returns the free memory available under Delphi 3+.
  237. 14Aug98 LdB
  238. ---------------------------------------------------------------------}
  239. FUNCTION MemAvail: LongInt;
  240. {-MaxAvail-----------------------------------------------------------
  241. Returns the max free memory block size available under Delphi 3+.
  242. 14Aug98 LdB
  243. ---------------------------------------------------------------------}
  244. FUNCTION MaxAvail: LongInt;
  245. {***************************************************************************}
  246. { INITIALIZED PUBLIC VARIABLES }
  247. {***************************************************************************}
  248. {---------------------------------------------------------------------------}
  249. { INITIALIZED DOS/DPMI/WIN/NT/OS2 VARIABLES }
  250. {---------------------------------------------------------------------------}
  251. CONST
  252. ErrorCode: Longint = errOk; { Last error code }
  253. ErrorInfo: Pointer = Nil; { Last error info }
  254. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  255. IMPLEMENTATION
  256. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  257. {$IFDEF PPC_DELPHI3} { DELPHI 3+ COMPILER }
  258. USES WinTypes, WinProcs; { Stardard units }
  259. {$ENDIF}
  260. {***************************************************************************}
  261. { INTERFACE ROUTINES }
  262. {***************************************************************************}
  263. {---------------------------------------------------------------------------}
  264. { GetErrorCode -> Platforms ALL - Updated 12Jul97 Bazsi }
  265. {---------------------------------------------------------------------------}
  266. FUNCTION GetErrorCode: LongInt;
  267. BEGIN
  268. GetErrorCode := ErrorCode; { Return last error }
  269. ErrorCode := 0; { Now clear errorcode }
  270. END;
  271. {---------------------------------------------------------------------------}
  272. { GetErrorInfo -> Platforms ALL - Updated 12Jul97 Bazsi }
  273. {---------------------------------------------------------------------------}
  274. FUNCTION GetErrorInfo: Pointer;
  275. BEGIN
  276. GetErrorInfo := ErrorInfo; { Return errorinfo ptr }
  277. END;
  278. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  279. { MINIMUM AND MAXIMUM ROUTINES }
  280. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  281. FUNCTION Min (I, J: Sw_Integer): Sw_Integer;
  282. BEGIN
  283. If (I < J) Then Min := I Else Min := J; { Select minimum }
  284. END;
  285. FUNCTION Max (I, J: Sw_Integer): Sw_Integer;
  286. BEGIN
  287. If (I > J) Then Max := I Else Max := J; { Select maximum }
  288. END;
  289. {---------------------------------------------------------------------------}
  290. { MinimumOf -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 04Oct99 LdB }
  291. {---------------------------------------------------------------------------}
  292. FUNCTION MinimumOf (A, B: Real): Real;
  293. BEGIN
  294. If (B < A) Then MinimumOf := B { B smaller take it }
  295. Else MinimumOf := A; { Else take A }
  296. END;
  297. {---------------------------------------------------------------------------}
  298. { MaximumOf -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 04Oct99 LdB }
  299. {---------------------------------------------------------------------------}
  300. FUNCTION MaximumOf (A, B: Real): Real;
  301. BEGIN
  302. If (B > A) Then MaximumOf := B { B bigger take it }
  303. Else MaximumOf := A; { Else take A }
  304. END;
  305. {---------------------------------------------------------------------------}
  306. { MinIntegerOf -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 04Oct99 LdB }
  307. {---------------------------------------------------------------------------}
  308. FUNCTION MinIntegerOf (A, B: SmallInt): SmallInt;
  309. BEGIN
  310. If (B < A) Then MinIntegerOf := B { B smaller take it }
  311. Else MinIntegerOf := A; { Else take A }
  312. END;
  313. {---------------------------------------------------------------------------}
  314. { MaxIntegerOf -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 04Oct99 LdB }
  315. {---------------------------------------------------------------------------}
  316. FUNCTION MaxIntegerOf (A, B: SmallInt): SmallInt;
  317. BEGIN
  318. If (B > A) Then MaxIntegerOf := B { B bigger take it }
  319. Else MaxIntegerOf := A; { Else take A }
  320. END;
  321. {---------------------------------------------------------------------------}
  322. { MinLongIntOf -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 04Oct99 LdB }
  323. {---------------------------------------------------------------------------}
  324. FUNCTION MinLongIntOf (A, B: LongInt): LongInt;
  325. BEGIN
  326. If (B < A) Then MinLongIntOf := B { B smaller take it }
  327. Else MinLongIntOf := A; { Else take A }
  328. END;
  329. {---------------------------------------------------------------------------}
  330. { MaxLongIntOf -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 04Oct99 LdB }
  331. {---------------------------------------------------------------------------}
  332. FUNCTION MaxLongIntOf (A, B: LongInt): LongInt;
  333. BEGIN
  334. If (B > A) Then MaxLongIntOf := B { B bigger take it }
  335. Else MaxLongIntOf := A; { Else take A }
  336. END;
  337. FUNCTION MemAvail: LongInt;
  338. BEGIN
  339. { Unlimited }
  340. MemAvail:=high(longint);
  341. END;
  342. {---------------------------------------------------------------------------}
  343. { MaxAvail -> Platforms WIN/NT - Updated 14Aug98 LdB }
  344. {---------------------------------------------------------------------------}
  345. FUNCTION MaxAvail: LongInt;
  346. BEGIN
  347. { Unlimited }
  348. MaxAvail:=high(longint);
  349. END;
  350. END.