sysutils.pp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. 211 : E:=EAbstractError.Create(SAbstractError);
  187. 216 : E:=EAccessViolation.Create(SAccessViolation);
  188. 219 : E:=EInvalidCast.Create(SInvalidCast);
  189. 227 : E:=EAssertionFailed.Create(SAssertionFailed);
  190. else
  191. E:=Exception.CreateFmt (SUnKnownRunTimeError,[Errno]);
  192. end;
  193. Raise E {at Address};
  194. end;
  195. Procedure AssertErrorHandler (Const Msg,FN : String;LineNo,TheAddr : Longint);
  196. Var
  197. S : String;
  198. begin
  199. If Msg='' then
  200. S:=SAssertionFailed
  201. else
  202. S:=Msg;
  203. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]); // at Pointer(theAddr);
  204. end;
  205. Procedure InitExceptions;
  206. {
  207. Must install uncaught exception handler (ExceptProc)
  208. and install exceptions for system exceptions or signals.
  209. (e.g: SIGSEGV -> ESegFault or so.)
  210. }
  211. begin
  212. ExceptProc:=@CatchUnhandledException;
  213. // Create objects that may have problems when there is no memory.
  214. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  215. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  216. AssertErrorProc:=@AssertErrorHandler;
  217. ErrorProc:=@RunErrorToExcept;
  218. end;
  219. { ---------------------------------------------------------------------
  220. Memory handling routines.
  221. ---------------------------------------------------------------------}
  222. function AllocMem(size : longint) : Pointer;
  223. var
  224. newP : Pointer;
  225. begin
  226. GetMem(newP, size);
  227. if newP <> nil then
  228. FillChar(newP^, size, 0);
  229. result := newP;
  230. end;
  231. { ReAllocMem
  232. 1. if P is nil and newSize is zero do nothing
  233. 2. if P is nil and newSize is NOT zero allocate memory and clear it to 0
  234. 3. if P is NOT nil and newSize is NOT zero a new memory block is allocated
  235. the data is copied from the old block to the new block and the old
  236. block is disposed of.
  237. if P is NOT nil then currentSize must be the size used to allocate memory
  238. for P whether it was using AllocMem or ReAllocMem.
  239. This is similar to the functions found in Delphi 1
  240. The same functions in Dephi 2, 3, and 4 use memory management. When
  241. I get a chance I might attempt to incorporate that feature.
  242. }
  243. procedure ReAllocMem(var P: Pointer; currentSize: longint; newSize: longint);
  244. var
  245. newP : Pointer;
  246. moveSize : longint;
  247. begin
  248. if ((P = nil) and (newSize > 0)) then
  249. P := AllocMem(newSize)
  250. else if ((P <> nil) and (newSize > 0)) then
  251. begin
  252. newP := AllocMem(newSize);
  253. if newP <> nil then
  254. begin
  255. if newSize > currentSize then
  256. moveSize := currentSize
  257. else
  258. moveSize := newSize;
  259. Move(P^, newP^, moveSize);
  260. FreeMem(P, currentSize);
  261. P := newP;
  262. end;
  263. end;
  264. end;
  265. { Initialization code. }
  266. begin
  267. InitExceptions; { Initialize exceptions. OS independent }
  268. InitInternational; { Initialize internationalization settings }
  269. end.
  270. {
  271. $Log$
  272. Revision 1.26 1999-04-09 08:40:46 michael
  273. + Fixed tfiletime problem
  274. Revision 1.25 1999/04/08 16:26:31 michael
  275. + Added (re)allocmem
  276. Revision 1.24 1999/04/08 12:23:05 peter
  277. * removed os.inc
  278. Revision 1.23 1999/02/28 13:17:37 michael
  279. + Added internationalization support and more format functions
  280. Revision 1.22 1999/02/10 22:15:13 michael
  281. + Changed to ansistrings
  282. Revision 1.21 1999/02/09 14:24:50 pierre
  283. * dos unit missing for go32v2 !!
  284. Revision 1.20 1999/02/09 12:38:44 michael
  285. * Fixed INt() proble. Defined THandle, included Filemode constants
  286. Revision 1.19 1999/02/03 16:18:58 michael
  287. + Uses Windows on win32 platform
  288. Revision 1.18 1998/12/15 22:43:12 peter
  289. * removed temp symbols
  290. Revision 1.17 1998/10/20 19:26:37 michael
  291. + Forgot to include disk functions
  292. Revision 1.16 1998/10/11 12:23:41 michael
  293. + More sysutils calls.
  294. Revision 1.15 1998/10/10 09:53:10 michael
  295. Added assertion handling
  296. Revision 1.14 1998/10/03 15:08:05 florian
  297. * EInvalidCast added (from runerror 219)
  298. Revision 1.13 1998/10/02 13:00:11 michael
  299. + More RTL error handling
  300. Revision 1.12 1998/10/02 12:17:18 michael
  301. + Made sure it compiles with official 0.99.8
  302. Revision 1.11 1998/10/01 16:04:11 michael
  303. + Added RTL error handling
  304. Revision 1.10 1998/09/24 23:45:27 peter
  305. * updated for auto objpas loading
  306. Revision 1.9 1998/09/24 16:13:49 michael
  307. Changes in exception and open array handling
  308. Revision 1.8 1998/09/18 23:57:26 michael
  309. * Changed use_excepions to useexceptions
  310. Revision 1.7 1998/09/16 14:34:38 pierre
  311. * go32v2 did not compile
  312. * wrong code in systr.inc corrected
  313. Revision 1.6 1998/09/16 08:28:44 michael
  314. Update from gertjan Schouten, plus small fix for linux
  315. Revision 1.5 1998/09/04 08:49:07 peter
  316. * 0.99.5 doesn't compile a whole objpas anymore to overcome crashes
  317. Revision 1.4 1998/08/10 15:52:27 peter
  318. * fixed so 0.99.5 compiles it, but no exception class
  319. Revision 1.3 1998/07/29 15:44:32 michael
  320. included sysutils and math.pp as target. They compile now.
  321. }