sysutils.pp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. {$ifdef autoobjpas}
  46. constructor createfmt(const msg : string; const args : array of const);
  47. {$endif}
  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. econverterror = class(exception);
  73. { Read date & Time function declarations }
  74. {$i datih.inc}
  75. { Read String Handling functions declaration }
  76. {$i sysstrh.inc}
  77. { Read pchar handling functions declration }
  78. {$i syspchh.inc}
  79. { Read filename handling functions declaration }
  80. {$i finah.inc}
  81. implementation
  82. { Read message string definitions }
  83. {
  84. Add a language with IFDEF LANG_NAME
  85. just befor the final ELSE. This way English will always be the default.
  86. }
  87. {$IFDEF LANG_GERMAN}
  88. {$i strg.inc} // Does not exist yet !!
  89. {$ELSE}
  90. {$i stre.inc}
  91. {$ENDIF}
  92. { Read filename handling functions implementation }
  93. {$i fina.inc}
  94. { Read date & Time function implementations }
  95. {$i dati.inc}
  96. { Read String Handling functions implementation }
  97. {$i sysstr.inc}
  98. { Read pchar handling functions implementation }
  99. {$i syspch.inc}
  100. constructor exception.create(const msg : string);
  101. begin
  102. inherited create;
  103. fmessage:=msg;
  104. {!!!!!}
  105. end;
  106. {$ifdef autoobjpas}
  107. constructor exception.createfmt(const msg : string; const args : array of const);
  108. begin
  109. inherited create;
  110. fmessage:=Format(msg,args);
  111. end;
  112. {$endif}
  113. constructor exception.createres(ident : longint);
  114. begin
  115. inherited create;
  116. {!!!!!}
  117. end;
  118. Procedure CatchUnhandledException (Obj : TObject; Addr: Pointer);
  119. Var
  120. Message : String;
  121. begin
  122. {$ifndef USE_WINDOWS}
  123. Writeln ('An unhandled exception occurred at ',HexStr(Longint(Addr),8),' : ');
  124. if Obj is exception then
  125. begin
  126. Message:=Exception(Obj).Message;
  127. Writeln (Message);
  128. end
  129. else
  130. Writeln ('Exception object ',Obj.ClassName,' is not of class Exception.');
  131. Halt(217);
  132. {$else}
  133. {$endif}
  134. end;
  135. Var OutOfMemory : EOutOfMemory;
  136. InValidPointer : EInvalidPointer;
  137. Procedure RunErrorToExcept (ErrNo : Longint; Address : Pointer);
  138. Var E : Exception;
  139. S : String;
  140. begin
  141. Case Errno of
  142. 1 : E:=OutOfMemory;
  143. //!! ?? 2 is a 'file not found error' ??
  144. 2 : E:=InvalidPointer;
  145. 3,4,5,100,101,106 : { I/O errors }
  146. begin
  147. Case Errno of
  148. 3 : S:=SInvalidFileName;
  149. 4 : S:=STooManyOpenFiles;
  150. 5 : S:=SAccessDenied;
  151. 100 : S:=SEndOfFile;
  152. 101 : S:=SDiskFull;
  153. 106 : S:=SInvalidInput;
  154. end;
  155. E:=EinOutError.Create (S);
  156. EInoutError(E).ErrorCode:=IOresult; // Clears InOutRes !!
  157. end;
  158. else
  159. {$ifdef autoobjpas}
  160. E:=Exception.CreateFmt (SUnKnownRunTimeError,[Errno]);
  161. {$else}
  162. E:=Exception.Create(SUnknownRunTimeError);
  163. {$endif}
  164. end;
  165. Raise E {at Address};
  166. end;
  167. Procedure InitExceptions;
  168. {
  169. Must install uncaught exception handler (ExceptProc)
  170. and install exceptions for system exceptions or signals.
  171. (e.g: SIGSEGV -> ESegFault or so.)
  172. }
  173. begin
  174. ExceptProc:=@CatchUnhandledException;
  175. // Create objects that may have problems when there is no memory.
  176. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  177. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  178. ErrorProc:=@RunErrorToExcept;
  179. end;
  180. {Initialization code.}
  181. begin
  182. InitExceptions;
  183. end.
  184. {
  185. $Log$
  186. Revision 1.12 1998-10-02 12:17:18 michael
  187. + Made sure it compiles with official 0.99.8
  188. Revision 1.11 1998/10/01 16:04:11 michael
  189. + Added RTL error handling
  190. Revision 1.10 1998/09/24 23:45:27 peter
  191. * updated for auto objpas loading
  192. Revision 1.9 1998/09/24 16:13:49 michael
  193. Changes in exception and open array handling
  194. Revision 1.8 1998/09/18 23:57:26 michael
  195. * Changed use_excepions to useexceptions
  196. Revision 1.7 1998/09/16 14:34:38 pierre
  197. * go32v2 did not compile
  198. * wrong code in systr.inc corrected
  199. Revision 1.6 1998/09/16 08:28:44 michael
  200. Update from gertjan Schouten, plus small fix for linux
  201. Revision 1.5 1998/09/04 08:49:07 peter
  202. * 0.99.5 doesn't compile a whole objpas anymore to overcome crashes
  203. Revision 1.4 1998/08/10 15:52:27 peter
  204. * fixed so 0.99.5 compiles it, but no exception class
  205. Revision 1.3 1998/07/29 15:44:32 michael
  206. included sysutils and math.pp as target. They compile now.
  207. }