sysutils.pp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. uses
  16. {$ifdef linux}
  17. linux
  18. {$else}
  19. dos
  20. {$ifdef go32v2}
  21. ,go32
  22. {$endif go32v2}
  23. {$endif linux}
  24. {$ifndef AUTOOBJPAS}
  25. ,objpas
  26. {$endif}
  27. ;
  28. type
  29. { some helpful data types }
  30. tprocedure = procedure;
  31. tfilename = string;
  32. longrec = packed record
  33. lo,hi : word;
  34. end;
  35. wordrec = packed record
  36. lo,hi : byte;
  37. end;
  38. { exceptions }
  39. exception = class(TObject)
  40. private
  41. fmessage : string;
  42. fhelpcontext : longint;
  43. public
  44. constructor create(const msg : string);
  45. constructor createfmt(const msg : string; const args : array of const);
  46. constructor createres(ident : longint);
  47. { !!!! }
  48. property helpcontext : longint read fhelpcontext write fhelpcontext;
  49. property message : string read fmessage write fmessage;
  50. end;
  51. exceptclass = class of exception;
  52. { integer math exceptions }
  53. EInterror = Class(Exception);
  54. EDivByZero = Class(EIntError);
  55. ERangeError = Class(EIntError);
  56. EIntOverflow = Class(EIntError);
  57. { General math errors }
  58. EMathError = Class(Exception);
  59. EInvalidOp = Class(EMathError);
  60. EZeroDivide = Class(EMathError);
  61. EOverflow = Class(EMathError);
  62. EUnderflow = Class(EMathError);
  63. { Run-time and I/O Errors }
  64. EInOutError = class(Exception)
  65. public
  66. ErrorCode : Longint;
  67. end;
  68. EInvalidPointer = Class(Exception);
  69. EOutOfMemory = Class(Exception);
  70. econverterror = class(exception);
  71. { Read date & Time function declarations }
  72. {$i datih.inc}
  73. { Read String Handling functions declaration }
  74. {$i sysstrh.inc}
  75. { Read pchar handling functions declration }
  76. {$i syspchh.inc}
  77. { Read filename handling functions declaration }
  78. {$i finah.inc}
  79. implementation
  80. { Read message string definitions }
  81. {
  82. Add a language with IFDEF LANG_NAME
  83. just befor the final ELSE. This way English will always be the default.
  84. }
  85. {$IFDEF LANG_GERMAN}
  86. {$i strg.inc} // Does not exist yet !!
  87. {$ELSE}
  88. {$i stre.inc}
  89. {$ENDIF}
  90. { Read filename handling functions implementation }
  91. {$i fina.inc}
  92. { Read date & Time function implementations }
  93. {$i dati.inc}
  94. { Read String Handling functions implementation }
  95. {$i sysstr.inc}
  96. { Read pchar handling functions implementation }
  97. {$i syspch.inc}
  98. constructor exception.create(const msg : string);
  99. begin
  100. inherited create;
  101. fmessage:=msg;
  102. {!!!!!}
  103. end;
  104. constructor exception.createfmt(const msg : string; const args : array of const);
  105. begin
  106. inherited create;
  107. fmessage:=Format(msg,args);
  108. end;
  109. constructor exception.createres(ident : longint);
  110. begin
  111. inherited create;
  112. {!!!!!}
  113. end;
  114. Procedure CatchUnhandledException (Obj : TObject; Addr: Pointer);
  115. Var
  116. Message : String;
  117. begin
  118. {$ifndef USE_WINDOWS}
  119. Writeln ('An unhandled exception occurred at ',HexStr(Longint(Addr),8),' : ');
  120. if Obj is exception then
  121. begin
  122. Message:=Exception(Obj).Message;
  123. Writeln (Message);
  124. end
  125. else
  126. Writeln ('Exception object ',Obj.ClassName,' is not of class Exception.');
  127. Halt(217);
  128. {$else}
  129. {$endif}
  130. end;
  131. Var OutOfMemory : EOutOfMemory;
  132. InValidPointer : EInvalidPointer;
  133. Procedure RunErrorToExcept (ErrNo : Longint; Address : Pointer);
  134. Var E : Exception;
  135. S : String;
  136. begin
  137. Case Errno of
  138. 1 : E:=OutOfMemory;
  139. //!! ?? 2 is a 'file not found error' ??
  140. 2 : E:=InvalidPointer;
  141. 3,4,5,100,101,106 : { I/O errors }
  142. begin
  143. Case Errno of
  144. 3 : S:=SInvalidFileName;
  145. 4 : S:=STooManyOpenFiles;
  146. 5 : S:=SAccessDenied;
  147. 100 : S:=SEndOfFile;
  148. 101 : S:=SDiskFull;
  149. 106 : S:=SInvalidInput;
  150. end;
  151. E:=EinOutError.Create (S);
  152. EInoutError(E).ErrorCode:=IOresult; // Clears InOutRes !!
  153. end;
  154. else
  155. E:=Exception.CreateFmt (SUnKnownRunTimeError,[Errno]);
  156. end;
  157. Raise E {at Address};
  158. end;
  159. Procedure InitExceptions;
  160. {
  161. Must install uncaught exception handler (ExceptProc)
  162. and install exceptions for system exceptions or signals.
  163. (e.g: SIGSEGV -> ESegFault or so.)
  164. }
  165. begin
  166. ExceptProc:=@CatchUnhandledException;
  167. // Create objects that may have problems when there is no memory.
  168. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  169. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  170. ErrorProc:=@RunErrorToExcept;
  171. end;
  172. {Initialization code.}
  173. begin
  174. InitExceptions;
  175. end.
  176. {
  177. $Log$
  178. Revision 1.11 1998-10-01 16:04:11 michael
  179. + Added RTL error handling
  180. Revision 1.10 1998/09/24 23:45:27 peter
  181. * updated for auto objpas loading
  182. Revision 1.9 1998/09/24 16:13:49 michael
  183. Changes in exception and open array handling
  184. Revision 1.8 1998/09/18 23:57:26 michael
  185. * Changed use_excepions to useexceptions
  186. Revision 1.7 1998/09/16 14:34:38 pierre
  187. * go32v2 did not compile
  188. * wrong code in systr.inc corrected
  189. Revision 1.6 1998/09/16 08:28:44 michael
  190. Update from gertjan Schouten, plus small fix for linux
  191. Revision 1.5 1998/09/04 08:49:07 peter
  192. * 0.99.5 doesn't compile a whole objpas anymore to overcome crashes
  193. Revision 1.4 1998/08/10 15:52:27 peter
  194. * fixed so 0.99.5 compiles it, but no exception class
  195. Revision 1.3 1998/07/29 15:44:32 michael
  196. included sysutils and math.pp as target. They compile now.
  197. }