sysutils.pp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1998 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. unit sysutils;
  13. interface
  14. {$MODE objfpc}
  15. { force ansistrings }
  16. {$H+}
  17. uses
  18. {$ifdef linux}
  19. linux
  20. {$else}
  21. {$ifdef win32}
  22. dos,windows
  23. {$else}
  24. {$ifdef go32v2}
  25. dos,go32
  26. {$endif go32v2}
  27. {$endif win32}
  28. {$endif linux}
  29. ;
  30. type
  31. { some helpful data types }
  32. tprocedure = procedure;
  33. tfilename = string;
  34. longrec = packed record
  35. lo,hi : word;
  36. end;
  37. wordrec = packed record
  38. lo,hi : byte;
  39. end;
  40. { exceptions }
  41. exception = class(TObject)
  42. private
  43. fmessage : string;
  44. fhelpcontext : longint;
  45. public
  46. constructor create(const msg : string);
  47. constructor createfmt(const msg : string; const args : array of const);
  48. constructor createres(ident : longint);
  49. { !!!! }
  50. property helpcontext : longint read fhelpcontext write fhelpcontext;
  51. property message : string read fmessage write fmessage;
  52. end;
  53. exceptclass = class of exception;
  54. { integer math exceptions }
  55. EInterror = Class(Exception);
  56. EDivByZero = Class(EIntError);
  57. ERangeError = Class(EIntError);
  58. EIntOverflow = Class(EIntError);
  59. { General math errors }
  60. EMathError = Class(Exception);
  61. EInvalidOp = Class(EMathError);
  62. EZeroDivide = Class(EMathError);
  63. EOverflow = Class(EMathError);
  64. EUnderflow = Class(EMathError);
  65. { Run-time and I/O Errors }
  66. EInOutError = class(Exception)
  67. public
  68. ErrorCode : Longint;
  69. end;
  70. EInvalidPointer = Class(Exception);
  71. EOutOfMemory = Class(Exception);
  72. EAccessViolation = Class(Exception);
  73. EInvalidCast = Class(Exception);
  74. { String conversion errors }
  75. EConvertError = class(Exception);
  76. { Other errors }
  77. EAbort = Class(Exception);
  78. EAbstractError = Class(Exception);
  79. EAssertionFailed = Class(Exception);
  80. { Read date & Time function declarations }
  81. {$i datih.inc}
  82. { Read String Handling functions declaration }
  83. {$i sysstrh.inc}
  84. { Read pchar handling functions declration }
  85. {$i syspchh.inc}
  86. { Read filename handling functions declaration }
  87. {$i finah.inc}
  88. { Read other file handling function declarations }
  89. {$i filutilh.inc}
  90. { Read disk function declarations }
  91. {$i diskh.inc}
  92. implementation
  93. { Read message string definitions }
  94. {
  95. Add a language with IFDEF LANG_NAME
  96. just befor the final ELSE. This way English will always be the default.
  97. }
  98. {$IFDEF LANG_GERMAN}
  99. {$i strg.inc} // Does not exist yet !!
  100. {$ELSE}
  101. {$i stre.inc}
  102. {$ENDIF}
  103. { Read filename handling functions implementation }
  104. {$i fina.inc}
  105. { Read other file handling function implementations }
  106. {$i filutil.inc}
  107. { Read disk function implementations }
  108. {$i disk.inc}
  109. { Read date & Time function implementations }
  110. {$i dati.inc}
  111. { Read String Handling functions implementation }
  112. {$i sysstr.inc}
  113. { Read pchar handling functions implementation }
  114. {$i syspch.inc}
  115. constructor exception.create(const msg : string);
  116. begin
  117. inherited create;
  118. fmessage:=msg;
  119. end;
  120. constructor exception.createfmt(const msg : string; const args : array of const);
  121. begin
  122. inherited create;
  123. fmessage:=Format(msg,args);
  124. end;
  125. constructor exception.createres(ident : longint);
  126. begin
  127. inherited create;
  128. {!!!!!}
  129. end;
  130. Procedure CatchUnhandledException (Obj : TObject; Addr: Pointer);
  131. Var
  132. Message : String;
  133. begin
  134. {$ifndef USE_WINDOWS}
  135. Writeln ('An unhandled exception occurred at ',HexStr(Longint(Addr),8),' : ');
  136. if Obj is exception then
  137. begin
  138. Message:=Exception(Obj).Message;
  139. Writeln (Message);
  140. end
  141. else
  142. Writeln ('Exception object ',Obj.ClassName,' is not of class Exception.');
  143. Halt(217);
  144. {$else}
  145. {$endif}
  146. end;
  147. Var OutOfMemory : EOutOfMemory;
  148. InValidPointer : EInvalidPointer;
  149. Procedure RunErrorToExcept (ErrNo : Longint; Address : Pointer);
  150. Var E : Exception;
  151. S : String;
  152. begin
  153. Case Errno of
  154. 1,203 : E:=OutOfMemory;
  155. //!! ?? 2 is a 'file not found error' ??
  156. 2,204 : E:=InvalidPointer;
  157. 3,4,5,6,100,101,102,103,105,106 : { I/O errors }
  158. begin
  159. Case Errno of
  160. 3 : S:=SInvalidFileName;
  161. 4 : S:=STooManyOpenFiles;
  162. 5 : S:=SAccessDenied;
  163. 6 : S:=SInvalidFileHandle;
  164. 15 : S:=SInvalidDrive;
  165. 100 : S:=SEndOfFile;
  166. 101 : S:=SDiskFull;
  167. 102 : S:=SFileNotAssigned;
  168. 103 : S:=SFileNotOpen;
  169. 104 : S:=SFileNotOpenForInput;
  170. 105 : S:=SFileNotOpenForOutput;
  171. 106 : S:=SInvalidInput;
  172. end;
  173. E:=EinOutError.Create (S);
  174. EInoutError(E).ErrorCode:=IOresult; // Clears InOutRes !!
  175. end;
  176. // We don't set abstracterrorhandler, but we do it here.
  177. // Unless the use sets another handler we'll get here anyway...
  178. 200 : E:=EDivByZero.Create(SDivByZero);
  179. 201 : E:=ERangeError.Create(SRangeError);
  180. 211 : E:=EAbstractError.Create(SAbstractError);
  181. 216 : E:=EAccessViolation.Create(SAccessViolation);
  182. 219 : E:=EInvalidCast.Create(SInvalidCast);
  183. 227 : E:=EAssertionFailed.Create(SAssertionFailed);
  184. else
  185. E:=Exception.CreateFmt (SUnKnownRunTimeError,[Errno]);
  186. end;
  187. Raise E {at Address};
  188. end;
  189. Procedure AssertErrorHandler (Const Msg,FN : String;LineNo,TheAddr : Longint);
  190. Var
  191. S : String;
  192. begin
  193. If Msg='' then
  194. S:=SAssertionFailed
  195. else
  196. S:=Msg;
  197. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]); // at Pointer(theAddr);
  198. end;
  199. Procedure InitExceptions;
  200. {
  201. Must install uncaught exception handler (ExceptProc)
  202. and install exceptions for system exceptions or signals.
  203. (e.g: SIGSEGV -> ESegFault or so.)
  204. }
  205. begin
  206. ExceptProc:=@CatchUnhandledException;
  207. // Create objects that may have problems when there is no memory.
  208. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  209. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  210. AssertErrorProc:=@AssertErrorHandler;
  211. ErrorProc:=@RunErrorToExcept;
  212. end;
  213. {Initialization code.}
  214. begin
  215. InitExceptions;
  216. end.
  217. {
  218. $Log$
  219. Revision 1.22 1999-02-10 22:15:13 michael
  220. + Changed to ansistrings
  221. Revision 1.21 1999/02/09 14:24:50 pierre
  222. * dos unit missing for go32v2 !!
  223. Revision 1.20 1999/02/09 12:38:44 michael
  224. * Fixed INt() proble. Defined THandle, included Filemode constants
  225. Revision 1.19 1999/02/03 16:18:58 michael
  226. + Uses Windows on win32 platform
  227. Revision 1.18 1998/12/15 22:43:12 peter
  228. * removed temp symbols
  229. Revision 1.17 1998/10/20 19:26:37 michael
  230. + Forgot to include disk functions
  231. Revision 1.16 1998/10/11 12:23:41 michael
  232. + More sysutils calls.
  233. Revision 1.15 1998/10/10 09:53:10 michael
  234. Added assertion handling
  235. Revision 1.14 1998/10/03 15:08:05 florian
  236. * EInvalidCast added (from runerror 219)
  237. Revision 1.13 1998/10/02 13:00:11 michael
  238. + More RTL error handling
  239. Revision 1.12 1998/10/02 12:17:18 michael
  240. + Made sure it compiles with official 0.99.8
  241. Revision 1.11 1998/10/01 16:04:11 michael
  242. + Added RTL error handling
  243. Revision 1.10 1998/09/24 23:45:27 peter
  244. * updated for auto objpas loading
  245. Revision 1.9 1998/09/24 16:13:49 michael
  246. Changes in exception and open array handling
  247. Revision 1.8 1998/09/18 23:57:26 michael
  248. * Changed use_excepions to useexceptions
  249. Revision 1.7 1998/09/16 14:34:38 pierre
  250. * go32v2 did not compile
  251. * wrong code in systr.inc corrected
  252. Revision 1.6 1998/09/16 08:28:44 michael
  253. Update from gertjan Schouten, plus small fix for linux
  254. Revision 1.5 1998/09/04 08:49:07 peter
  255. * 0.99.5 doesn't compile a whole objpas anymore to overcome crashes
  256. Revision 1.4 1998/08/10 15:52:27 peter
  257. * fixed so 0.99.5 compiles it, but no exception class
  258. Revision 1.3 1998/07/29 15:44:32 michael
  259. included sysutils and math.pp as target. They compile now.
  260. }