sysutils.pp 11 KB

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