sysutils.pp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. dos,windows
  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. TMethod = packed record
  42. Code, Data: Pointer;
  43. end;
  44. { exceptions }
  45. exception = class(TObject)
  46. private
  47. fmessage : string;
  48. fhelpcontext : longint;
  49. public
  50. constructor create(const msg : string);
  51. constructor createfmt(const msg : string; const args : array of const);
  52. constructor createres(ident : longint);
  53. { !!!! }
  54. property helpcontext : longint read fhelpcontext write fhelpcontext;
  55. property message : string read fmessage write fmessage;
  56. end;
  57. exceptclass = class of exception;
  58. { integer math exceptions }
  59. EInterror = Class(Exception);
  60. EDivByZero = Class(EIntError);
  61. ERangeError = Class(EIntError);
  62. EIntOverflow = Class(EIntError);
  63. { General math errors }
  64. EMathError = Class(Exception);
  65. EInvalidOp = Class(EMathError);
  66. EZeroDivide = Class(EMathError);
  67. EOverflow = Class(EMathError);
  68. EUnderflow = Class(EMathError);
  69. { Run-time and I/O Errors }
  70. EInOutError = class(Exception)
  71. public
  72. ErrorCode : Longint;
  73. end;
  74. EInvalidPointer = Class(Exception);
  75. EOutOfMemory = Class(Exception);
  76. EAccessViolation = Class(Exception);
  77. EInvalidCast = Class(Exception);
  78. { String conversion errors }
  79. EConvertError = class(Exception);
  80. { Other errors }
  81. EAbort = Class(Exception);
  82. EAbstractError = Class(Exception);
  83. EAssertionFailed = Class(Exception);
  84. { FileRec/TextRec }
  85. {$i filerec.inc}
  86. {$i textrec.inc}
  87. { Read internationalization settings }
  88. {$i sysinth.inc}
  89. { Read date & Time function declarations }
  90. {$i datih.inc}
  91. { Read String Handling functions declaration }
  92. {$i sysstrh.inc}
  93. { Read pchar handling functions declration }
  94. {$i syspchh.inc}
  95. { Read filename handling functions declaration }
  96. {$i finah.inc}
  97. { Read other file handling function declarations }
  98. {$i filutilh.inc}
  99. { Read disk function declarations }
  100. {$i diskh.inc}
  101. implementation
  102. { Read message string definitions }
  103. {
  104. Add a language with IFDEF LANG_NAME
  105. just befor the final ELSE. This way English will always be the default.
  106. }
  107. {$IFDEF LANG_GERMAN}
  108. {$i strg.inc} // Does not exist yet !!
  109. {$ELSE}
  110. {$i stre.inc}
  111. {$ENDIF}
  112. { Read filename handling functions implementation }
  113. {$i fina.inc}
  114. { Read String Handling functions implementation }
  115. {$i sysstr.inc}
  116. { Read other file handling function implementations }
  117. {$i filutil.inc}
  118. { Read disk function implementations }
  119. {$i disk.inc}
  120. { Read date & Time function implementations }
  121. {$i dati.inc}
  122. { Read pchar handling functions implementation }
  123. {$i syspch.inc}
  124. constructor exception.create(const msg : string);
  125. begin
  126. inherited create;
  127. fmessage:=msg;
  128. end;
  129. constructor exception.createfmt(const msg : string; const args : array of const);
  130. begin
  131. inherited create;
  132. fmessage:=Format(msg,args);
  133. end;
  134. constructor exception.createres(ident : longint);
  135. begin
  136. inherited create;
  137. {!!!!!}
  138. end;
  139. Procedure CatchUnhandledException (Obj : TObject; Addr: Pointer);
  140. Var
  141. Message : String;
  142. begin
  143. Writeln(stdout,'An unhandled exception occurred at 0x',HexStr(Longint(Addr),8),' :');
  144. if Obj is exception then
  145. begin
  146. Message:=Exception(Obj).Message;
  147. Writeln(stdout,Message);
  148. end
  149. else
  150. Writeln(stdout,'Exception object ',Obj.ClassName,' is not of class Exception.');
  151. Writeln(stdout,'');
  152. Halt(217);
  153. end;
  154. Var OutOfMemory : EOutOfMemory;
  155. InValidPointer : EInvalidPointer;
  156. Procedure RunErrorToExcept (ErrNo : Longint; Address : Pointer);
  157. Var E : Exception;
  158. S : String;
  159. begin
  160. Case Errno of
  161. 1,203 : E:=OutOfMemory;
  162. 204 : E:=InvalidPointer;
  163. 2,3,4,5,6,100,101,102,103,105,106 : { I/O errors }
  164. begin
  165. Case Errno of
  166. 2 : S:=SFileNotFound;
  167. 3 : S:=SInvalidFileName;
  168. 4 : S:=STooManyOpenFiles;
  169. 5 : S:=SAccessDenied;
  170. 6 : S:=SInvalidFileHandle;
  171. 15 : S:=SInvalidDrive;
  172. 100 : S:=SEndOfFile;
  173. 101 : S:=SDiskFull;
  174. 102 : S:=SFileNotAssigned;
  175. 103 : S:=SFileNotOpen;
  176. 104 : S:=SFileNotOpenForInput;
  177. 105 : S:=SFileNotOpenForOutput;
  178. 106 : S:=SInvalidInput;
  179. end;
  180. E:=EinOutError.Create (S);
  181. EInoutError(E).ErrorCode:=IOresult; // Clears InOutRes !!
  182. end;
  183. // We don't set abstracterrorhandler, but we do it here.
  184. // Unless the use sets another handler we'll get here anyway...
  185. 200 : E:=EDivByZero.Create(SDivByZero);
  186. 201 : E:=ERangeError.Create(SRangeError);
  187. 205 : E:=EOverflow.Create(SOverflow);
  188. 206 : E:=EOverflow.Create(SUnderflow);
  189. 207 : E:=EInvalidOp.Create(SInvalidOp);
  190. 211 : E:=EAbstractError.Create(SAbstractError);
  191. 215 : E:=EIntOverflow.Create(SIntOverflow);
  192. 216 : E:=EAccessViolation.Create(SAccessViolation);
  193. 219 : E:=EInvalidCast.Create(SInvalidCast);
  194. 227 : E:=EAssertionFailed.Create(SAssertionFailed);
  195. else
  196. E:=Exception.CreateFmt (SUnKnownRunTimeError,[Errno]);
  197. end;
  198. Raise E at longint(Address);
  199. end;
  200. Procedure AssertErrorHandler (Const Msg,FN : ShortString;LineNo,TheAddr : Longint);
  201. Var
  202. S : String;
  203. begin
  204. If Msg='' then
  205. S:=SAssertionFailed
  206. else
  207. S:=Msg;
  208. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]); // at Pointer(theAddr);
  209. end;
  210. Procedure InitExceptions;
  211. {
  212. Must install uncaught exception handler (ExceptProc)
  213. and install exceptions for system exceptions or signals.
  214. (e.g: SIGSEGV -> ESegFault or so.)
  215. }
  216. begin
  217. ExceptProc:=@CatchUnhandledException;
  218. // Create objects that may have problems when there is no memory.
  219. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  220. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  221. AssertErrorProc:=@AssertErrorHandler;
  222. ErrorProc:=@RunErrorToExcept;
  223. end;
  224. { Initialization code. }
  225. Initialization
  226. InitExceptions; { Initialize exceptions. OS independent }
  227. InitInternational; { Initialize internationalization settings }
  228. Finalization
  229. OutOfMemory.Free;
  230. InValidPointer.Free;
  231. end.
  232. {
  233. $Log$
  234. Revision 1.36 1999-11-15 21:49:47 peter
  235. * exception address fixes
  236. Revision 1.35 1999/11/06 14:41:31 peter
  237. * truncated log
  238. Revision 1.34 1999/10/30 17:39:05 peter
  239. * memorymanager expanded with allocmem/reallocmem
  240. Revision 1.33 1999/10/26 12:29:07 peter
  241. * assert handler must use shortstring
  242. Revision 1.32 1999/09/15 20:26:30 florian
  243. * patch from Sebastian Guenther applied: TMethod implementation
  244. Revision 1.31 1999/08/28 14:53:27 florian
  245. * bug 471 fixed: run time error 2 is now converted into a file not
  246. found exception
  247. Revision 1.30 1999/08/18 11:28:24 michael
  248. * Fixed reallocmem bug 535
  249. Revision 1.29 1999/07/27 13:01:12 peter
  250. + filerec,textrec declarations
  251. Revision 1.28 1999/07/08 19:32:36 michael
  252. + Freed exception classes in finalization code
  253. Revision 1.27 1999/07/02 17:03:24 florian
  254. + added some runtime->excpetin wrappers: eintoverflow, eoverflow, eunderflow, einvalidop
  255. Revision 1.26 1999/04/09 08:40:46 michael
  256. + Fixed tfiletime problem
  257. Revision 1.25 1999/04/08 16:26:31 michael
  258. + Added (re)allocmem
  259. Revision 1.24 1999/04/08 12:23:05 peter
  260. * removed os.inc
  261. Revision 1.23 1999/02/28 13:17:37 michael
  262. + Added internationalization support and more format functions
  263. Revision 1.22 1999/02/10 22:15:13 michael
  264. + Changed to ansistrings
  265. Revision 1.21 1999/02/09 14:24:50 pierre
  266. * dos unit missing for go32v2 !!
  267. Revision 1.20 1999/02/09 12:38:44 michael
  268. * Fixed INt() proble. Defined THandle, included Filemode constants
  269. Revision 1.19 1999/02/03 16:18:58 michael
  270. + Uses Windows on win32 platform
  271. Revision 1.18 1998/12/15 22:43:12 peter
  272. * removed temp symbols
  273. Revision 1.17 1998/10/20 19:26:37 michael
  274. + Forgot to include disk functions
  275. Revision 1.16 1998/10/11 12:23:41 michael
  276. + More sysutils calls.
  277. Revision 1.15 1998/10/10 09:53:10 michael
  278. Added assertion handling
  279. }