sysutils.pp 8.6 KB

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