sysutils.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. { 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. { Memory management routines }
  82. function AllocMem(size : longint) : Pointer;
  83. procedure ReAllocMem(var P: Pointer; currentSize: longint; newSize: longint);
  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. {$ifndef USE_WINDOWS}
  144. Writeln ('An unhandled exception occurred at ',HexStr(Longint(Addr),8),' : ');
  145. if Obj is exception then
  146. begin
  147. Message:=Exception(Obj).Message;
  148. Writeln (Message);
  149. end
  150. else
  151. Writeln ('Exception object ',Obj.ClassName,' is not of class Exception.');
  152. Halt(217);
  153. {$else}
  154. {$endif}
  155. end;
  156. Var OutOfMemory : EOutOfMemory;
  157. InValidPointer : EInvalidPointer;
  158. Procedure RunErrorToExcept (ErrNo : Longint; Address : Pointer);
  159. Var E : Exception;
  160. S : String;
  161. begin
  162. Case Errno of
  163. 1,203 : E:=OutOfMemory;
  164. //!! ?? 2 is a 'file not found error' ??
  165. 2,204 : E:=InvalidPointer;
  166. 3,4,5,6,100,101,102,103,105,106 : { I/O errors }
  167. begin
  168. Case Errno of
  169. 3 : S:=SInvalidFileName;
  170. 4 : S:=STooManyOpenFiles;
  171. 5 : S:=SAccessDenied;
  172. 6 : S:=SInvalidFileHandle;
  173. 15 : S:=SInvalidDrive;
  174. 100 : S:=SEndOfFile;
  175. 101 : S:=SDiskFull;
  176. 102 : S:=SFileNotAssigned;
  177. 103 : S:=SFileNotOpen;
  178. 104 : S:=SFileNotOpenForInput;
  179. 105 : S:=SFileNotOpenForOutput;
  180. 106 : S:=SInvalidInput;
  181. end;
  182. E:=EinOutError.Create (S);
  183. EInoutError(E).ErrorCode:=IOresult; // Clears InOutRes !!
  184. end;
  185. // We don't set abstracterrorhandler, but we do it here.
  186. // Unless the use sets another handler we'll get here anyway...
  187. 200 : E:=EDivByZero.Create(SDivByZero);
  188. 201 : E:=ERangeError.Create(SRangeError);
  189. 205 : E:=EOverflow.Create(SOverflow);
  190. 206 : E:=EOverflow.Create(SUnderflow);
  191. 207 : E:=EInvalidOp.Create(SInvalidOp);
  192. 211 : E:=EAbstractError.Create(SAbstractError);
  193. 215 : E:=EIntOverflow.Create(SIntOverflow);
  194. 216 : E:=EAccessViolation.Create(SAccessViolation);
  195. 219 : E:=EInvalidCast.Create(SInvalidCast);
  196. 227 : E:=EAssertionFailed.Create(SAssertionFailed);
  197. else
  198. E:=Exception.CreateFmt (SUnKnownRunTimeError,[Errno]);
  199. end;
  200. Raise E {at Address};
  201. end;
  202. Procedure AssertErrorHandler (Const Msg,FN : String;LineNo,TheAddr : Longint);
  203. Var
  204. S : String;
  205. begin
  206. If Msg='' then
  207. S:=SAssertionFailed
  208. else
  209. S:=Msg;
  210. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]); // at Pointer(theAddr);
  211. end;
  212. Procedure InitExceptions;
  213. {
  214. Must install uncaught exception handler (ExceptProc)
  215. and install exceptions for system exceptions or signals.
  216. (e.g: SIGSEGV -> ESegFault or so.)
  217. }
  218. begin
  219. ExceptProc:=@CatchUnhandledException;
  220. // Create objects that may have problems when there is no memory.
  221. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  222. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  223. AssertErrorProc:=@AssertErrorHandler;
  224. ErrorProc:=@RunErrorToExcept;
  225. end;
  226. { ---------------------------------------------------------------------
  227. Memory handling routines.
  228. ---------------------------------------------------------------------}
  229. function AllocMem(size : longint) : Pointer;
  230. var
  231. newP : Pointer;
  232. begin
  233. GetMem(newP, size);
  234. if newP <> nil then
  235. FillChar(newP^, size, 0);
  236. result := newP;
  237. end;
  238. { ReAllocMem
  239. 1. if P is nil and newSize is zero do nothing
  240. 2. if P is nil and newSize is NOT zero allocate memory and clear it to 0
  241. 3. if P is NOT nil and newSize is NOT zero a new memory block is allocated
  242. the data is copied from the old block to the new block and the old
  243. block is disposed of.
  244. if P is NOT nil then currentSize must be the size used to allocate memory
  245. for P whether it was using AllocMem or ReAllocMem.
  246. This is similar to the functions found in Delphi 1
  247. The same functions in Dephi 2, 3, and 4 use memory management. When
  248. I get a chance I might attempt to incorporate that feature.
  249. }
  250. procedure ReAllocMem(var P: Pointer; currentSize: longint; newSize: longint);
  251. var
  252. newP : Pointer;
  253. moveSize : longint;
  254. begin
  255. if ((P = nil) and (newSize > 0)) then
  256. P := AllocMem(newSize)
  257. else if ((P <> nil) and (newSize > 0)) then
  258. begin
  259. newP := AllocMem(newSize);
  260. if newP <> nil then
  261. begin
  262. if newSize > currentSize then
  263. moveSize := currentSize
  264. else
  265. moveSize := newSize;
  266. Move(P^, newP^, moveSize);
  267. FreeMem(P, currentSize);
  268. P := newP;
  269. end;
  270. end;
  271. end;
  272. { Initialization code. }
  273. Initialization
  274. InitExceptions; { Initialize exceptions. OS independent }
  275. InitInternational; { Initialize internationalization settings }
  276. Finalization
  277. OutOfMemory.Free;
  278. InValidPointer.Free;
  279. end.
  280. {
  281. $Log$
  282. Revision 1.29 1999-07-27 13:01:12 peter
  283. + filerec,textrec declarations
  284. Revision 1.28 1999/07/08 19:32:36 michael
  285. + Freed exception classes in finalization code
  286. Revision 1.27 1999/07/02 17:03:24 florian
  287. + added some runtime->excpetin wrappers: eintoverflow, eoverflow, eunderflow, einvalidop
  288. Revision 1.26 1999/04/09 08:40:46 michael
  289. + Fixed tfiletime problem
  290. Revision 1.25 1999/04/08 16:26:31 michael
  291. + Added (re)allocmem
  292. Revision 1.24 1999/04/08 12:23:05 peter
  293. * removed os.inc
  294. Revision 1.23 1999/02/28 13:17:37 michael
  295. + Added internationalization support and more format functions
  296. Revision 1.22 1999/02/10 22:15:13 michael
  297. + Changed to ansistrings
  298. Revision 1.21 1999/02/09 14:24:50 pierre
  299. * dos unit missing for go32v2 !!
  300. Revision 1.20 1999/02/09 12:38:44 michael
  301. * Fixed INt() proble. Defined THandle, included Filemode constants
  302. Revision 1.19 1999/02/03 16:18:58 michael
  303. + Uses Windows on win32 platform
  304. Revision 1.18 1998/12/15 22:43:12 peter
  305. * removed temp symbols
  306. Revision 1.17 1998/10/20 19:26:37 michael
  307. + Forgot to include disk functions
  308. Revision 1.16 1998/10/11 12:23:41 michael
  309. + More sysutils calls.
  310. Revision 1.15 1998/10/10 09:53:10 michael
  311. Added assertion handling
  312. Revision 1.14 1998/10/03 15:08:05 florian
  313. * EInvalidCast added (from runerror 219)
  314. Revision 1.13 1998/10/02 13:00:11 michael
  315. + More RTL error handling
  316. Revision 1.12 1998/10/02 12:17:18 michael
  317. + Made sure it compiles with official 0.99.8
  318. Revision 1.11 1998/10/01 16:04:11 michael
  319. + Added RTL error handling
  320. Revision 1.10 1998/09/24 23:45:27 peter
  321. * updated for auto objpas loading
  322. Revision 1.9 1998/09/24 16:13:49 michael
  323. Changes in exception and open array handling
  324. Revision 1.8 1998/09/18 23:57:26 michael
  325. * Changed use_excepions to useexceptions
  326. Revision 1.7 1998/09/16 14:34:38 pierre
  327. * go32v2 did not compile
  328. * wrong code in systr.inc corrected
  329. Revision 1.6 1998/09/16 08:28:44 michael
  330. Update from gertjan Schouten, plus small fix for linux
  331. Revision 1.5 1998/09/04 08:49:07 peter
  332. * 0.99.5 doesn't compile a whole objpas anymore to overcome crashes
  333. Revision 1.4 1998/08/10 15:52:27 peter
  334. * fixed so 0.99.5 compiles it, but no exception class
  335. Revision 1.3 1998/07/29 15:44:32 michael
  336. included sysutils and math.pp as target. They compile now.
  337. }