sysutils.pp 11 KB

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