sysutils.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 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. { Read filename handling functions implementation }
  13. {$i fina.inc}
  14. Function FileSearch (Const Name, DirList : String) : String;
  15. Var
  16. I : longint;
  17. Temp : String;
  18. begin
  19. Result:='';
  20. temp:=Dirlist;
  21. repeat
  22. While (Length(Temp)>0) and (Temp[1]=PathSeparator) do
  23. Delete(Temp,1,1);
  24. I:=pos(PathSep,Temp);
  25. If I<>0 then
  26. begin
  27. Result:=Copy (Temp,1,i-1);
  28. system.Delete(Temp,1,I);
  29. end
  30. else
  31. begin
  32. Result:=Temp;
  33. Temp:='';
  34. end;
  35. If (Length(Result)>0) and (result[length(result)]<>DirectorySeparator) then
  36. Result:=Result+DirectorySeparator;
  37. Result:=Result+name;
  38. If not FileExists(Result) Then
  39. Result:='';
  40. until (length(temp)=0) or (length(result)<>0);
  41. end;
  42. { Read String Handling functions implementation }
  43. {$i sysstr.inc}
  44. { Read date & Time function implementations }
  45. {$i dati.inc}
  46. { Read pchar handling functions implementation }
  47. {$i syspch.inc}
  48. { MCBS functions }
  49. {$i sysansi.inc}
  50. { CPU Specific code }
  51. {$i sysutilp.inc}
  52. procedure FreeAndNil(var obj);
  53. var
  54. temp: tobject;
  55. begin
  56. temp:=tobject(obj);
  57. pointer(obj):=nil;
  58. temp.free;
  59. end;
  60. {$ifdef HASINTF}
  61. { Interfaces support }
  62. {$i intf.inc}
  63. {$endif HASINTF}
  64. constructor Exception.Create(const msg : string);
  65. begin
  66. inherited create;
  67. fmessage:=msg;
  68. end;
  69. constructor Exception.CreateFmt(const msg : string; const args : array of const);
  70. begin
  71. inherited create;
  72. fmessage:=Format(msg,args);
  73. end;
  74. constructor Exception.CreateRes(ResString: PString);
  75. begin
  76. inherited create;
  77. fmessage:=ResString^;
  78. end;
  79. constructor Exception.CreateResFmt(ResString: PString; const Args: array of const);
  80. begin
  81. inherited create;
  82. fmessage:=Format(ResString^,args);
  83. end;
  84. constructor Exception.CreateHelp(const Msg: string; AHelpContext: Integer);
  85. begin
  86. inherited create;
  87. fmessage:=Msg;
  88. fhelpcontext:=AHelpContext;
  89. end;
  90. constructor Exception.CreateFmtHelp(const Msg: string; const Args: array of const;
  91. AHelpContext: Integer);
  92. begin
  93. inherited create;
  94. fmessage:=Format(Msg,args);
  95. fhelpcontext:=AHelpContext;
  96. end;
  97. constructor Exception.CreateResHelp(ResString: PString; AHelpContext: Integer);
  98. begin
  99. inherited create;
  100. fmessage:=ResString^;
  101. fhelpcontext:=AHelpContext;
  102. end;
  103. constructor Exception.CreateResFmtHelp(ResString: PString; const Args: array of const;
  104. AHelpContext: Integer);
  105. begin
  106. inherited create;
  107. fmessage:=Format(ResString^,args);
  108. fhelpcontext:=AHelpContext;
  109. end;
  110. procedure EHeapMemoryError.FreeInstance;
  111. begin
  112. if AllowFree then
  113. inherited FreeInstance;
  114. end;
  115. {$ifopt S+}
  116. {$define STACKCHECK_WAS_ON}
  117. {$S-}
  118. {$endif OPT S }
  119. Procedure CatchUnhandledException (Obj : TObject; Addr,Frame: Pointer);
  120. Var
  121. Message : String;
  122. {$IFDEF VIRTUALPASCAL}
  123. stdout:text absolute output;
  124. {$ENDIF}
  125. begin
  126. Writeln(stdout,'An unhandled exception occurred at 0x',HexStr(Longint(Addr),8),' :');
  127. if Obj is exception then
  128. begin
  129. Message:=Exception(Obj).ClassName+' : '+Exception(Obj).Message;
  130. Writeln(stdout,Message);
  131. end
  132. else
  133. Writeln(stdout,'Exception object ',Obj.ClassName,' is not of class Exception.');
  134. { to get a nice symify }
  135. {$IFNDEF VIRTUALPASCAL}
  136. Writeln(stdout,BackTraceStrFunc(Addr));
  137. Dump_Stack(stdout,frame);
  138. {$ENDIF}
  139. Writeln(stdout,'');
  140. Halt(217);
  141. end;
  142. Var OutOfMemory : EOutOfMemory;
  143. InValidPointer : EInvalidPointer;
  144. Procedure RunErrorToExcept (ErrNo : Longint; Address,Frame : Pointer);
  145. Var E : Exception;
  146. S : String;
  147. begin
  148. Case Errno of
  149. 1,203 : E:=OutOfMemory;
  150. 204 : E:=InvalidPointer;
  151. 2,3,4,5,6,100,101,102,103,105,106 : { I/O errors }
  152. begin
  153. Case Errno of
  154. 2 : S:=SFileNotFound;
  155. 3 : S:=SInvalidFileName;
  156. 4 : S:=STooManyOpenFiles;
  157. 5 : S:=SAccessDenied;
  158. 6 : S:=SInvalidFileHandle;
  159. 15 : S:=SInvalidDrive;
  160. 100 : S:=SEndOfFile;
  161. 101 : S:=SDiskFull;
  162. 102 : S:=SFileNotAssigned;
  163. 103 : S:=SFileNotOpen;
  164. 104 : S:=SFileNotOpenForInput;
  165. 105 : S:=SFileNotOpenForOutput;
  166. 106 : S:=SInvalidInput;
  167. end;
  168. E:=EinOutError.Create (S);
  169. EInoutError(E).ErrorCode:=IOresult; // Clears InOutRes !!
  170. end;
  171. // We don't set abstracterrorhandler, but we do it here.
  172. // Unless the use sets another handler we'll get here anyway...
  173. 200 : E:=EDivByZero.Create(SDivByZero);
  174. 201 : E:=ERangeError.Create(SRangeError);
  175. 205 : E:=EOverflow.Create(SOverflow);
  176. 206 : E:=EOverflow.Create(SUnderflow);
  177. 207 : E:=EInvalidOp.Create(SInvalidOp);
  178. 211 : E:=EAbstractError.Create(SAbstractError);
  179. 215 : E:=EIntOverflow.Create(SIntOverflow);
  180. 216 : E:=EAccessViolation.Create(SAccessViolation);
  181. 217 : E:=EPrivilege.Create(SPrivilege);
  182. 218 : E:=EControlC.Create(SControlC);
  183. 219 : E:=EInvalidCast.Create(SInvalidCast);
  184. 220 : E:=EVariantError.Create(SInvalidVarCast);
  185. 221 : E:=EVariantError.Create(SInvalidVarOp);
  186. 222 : E:=EVariantError.Create(SDispatchError);
  187. 223 : E:=EVariantError.Create(SVarArrayCreate);
  188. 224 : E:=EVariantError.Create(SVarNotArray);
  189. 225 : E:=EVariantError.Create(SVarArrayBounds);
  190. 227 : E:=EAssertionFailed.Create(SAssertionFailed);
  191. 228 : E:=EExternalException.Create(SExternalException);
  192. 229 : E:=EIntfCastError.Create(SIntfCastError);
  193. 230 : E:=ESafecallException.Create(SSafecallException);
  194. else
  195. E:=Exception.CreateFmt (SUnKnownRunTimeError,[Errno]);
  196. end;
  197. {$ifdef VER1_0}
  198. Raise E at longint(Address){$ifdef ENHANCEDRAISE},longint(Frame){$endif};
  199. {$else VER1_0}
  200. Raise E at Address{$ifdef ENHANCEDRAISE},Frame){$endif};
  201. {$endif VER1_0}
  202. end;
  203. Procedure AssertErrorHandler (Const Msg,FN : ShortString;LineNo:longint; TheAddr : pointer);
  204. Var
  205. S : String;
  206. begin
  207. If Msg='' then
  208. S:=SAssertionFailed
  209. else
  210. S:=Msg;
  211. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]); // at Pointer(theAddr);
  212. end;
  213. {$ifdef STACKCHECK_WAS_ON}
  214. {$S+}
  215. {$endif}
  216. Procedure InitExceptions;
  217. {
  218. Must install uncaught exception handler (ExceptProc)
  219. and install exceptions for system exceptions or signals.
  220. (e.g: SIGSEGV -> ESegFault or so.)
  221. }
  222. begin
  223. ExceptProc:=@CatchUnhandledException;
  224. // Create objects that may have problems when there is no memory.
  225. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  226. OutOfMemory.AllowFree:=false;
  227. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  228. InvalidPointer.AllowFree:=false;
  229. AssertErrorProc:=@AssertErrorHandler;
  230. ErrorProc:=@RunErrorToExcept;
  231. OnShowException:=Nil;
  232. end;
  233. Procedure DoneExceptions;
  234. begin
  235. OutOfMemory.AllowFree:=true;
  236. OutOfMemory.Free;
  237. InValidPointer.AllowFree:=true;
  238. InValidPointer.Free;
  239. end;
  240. { Exception handling routines }
  241. function ExceptObject: TObject;
  242. begin
  243. {$IFDEF VIRTUALPASCAL}
  244. // vpascal does exceptions more the delphi way...
  245. // this needs to be written from scratch.
  246. {$ELSE}
  247. If RaiseList=Nil then
  248. Result:=Nil
  249. else
  250. Result:=RaiseList^.FObject;
  251. {$ENDIF}
  252. end;
  253. function ExceptAddr: Pointer;
  254. begin
  255. {$IFDEF VIRTUALPASCAL}
  256. // vpascal does exceptions more the delphi way...
  257. // this needs to be written from scratch.
  258. {$ELSE}
  259. If RaiseList=Nil then
  260. Result:=Nil
  261. else
  262. Result:=RaiseList^.Addr;
  263. {$ENDIF}
  264. end;
  265. function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
  266. Buffer: PChar; Size: Integer): Integer;
  267. Var
  268. S : AnsiString;
  269. Len : Integer;
  270. begin
  271. S:=Format(SExceptionErrorMessage,[ExceptObject.ClassName,ExceptAddr]);
  272. If ExceptObject is Exception then
  273. S:=Format('%s:'#10'%s',[S,Exception(ExceptObject).Message]);
  274. Len:=Length(S);
  275. If S[Len]<>'.' then
  276. begin
  277. S:=S+'.';
  278. Inc(len);
  279. end;
  280. If Len>Size then
  281. Len:=Size;
  282. if Len > 0 then
  283. Move(S[1],Buffer^,Len);
  284. Result:=Len;
  285. end;
  286. procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
  287. // use shortstring. On exception, the heap may be corrupt.
  288. Var
  289. Buf : ShortString;
  290. begin
  291. SetLength(Buf,ExceptionErrorMessage(ExceptObject,ExceptAddr,@Buf[1],255));
  292. If IsConsole Then
  293. writeln(Buf)
  294. else
  295. If Assigned(OnShowException) Then
  296. OnShowException (Buf);
  297. end;
  298. procedure Abort;
  299. begin
  300. {$ifdef VER1_0}
  301. Raise EAbort.Create(SAbortError) at Longint(Get_Caller_addr(Get_Frame));
  302. {$else VER1_0}
  303. Raise EAbort.Create(SAbortError)
  304. {$IFNDEF VIRTUALPASCAL}
  305. at Pointer(Get_Caller_addr(Get_Frame));
  306. {$ENDIF}
  307. {$endif VER1_0}
  308. end;
  309. procedure OutOfMemoryError;
  310. begin
  311. Raise OutOfMemory;
  312. end;
  313. {
  314. $Log$
  315. Revision 1.2 2003-11-26 20:00:19 florian
  316. * error handling for Variants improved
  317. Revision 1.1 2003/10/06 21:01:06 peter
  318. * moved classes unit to rtl
  319. Revision 1.17 2003/09/06 20:46:07 marco
  320. * 3 small VP fixes from Noah Silva. One (OutOfMemory error) failed.
  321. Revision 1.16 2003/04/06 11:06:39 michael
  322. + Added exception classname to output of unhandled exception for better identification
  323. Revision 1.15 2003/03/18 08:28:23 michael
  324. Patch from peter for Abort routine
  325. Revision 1.14 2003/03/17 15:11:51 armin
  326. + someone AssertErrorHandler, BackTraceFunc and Dump_Stack so that pointer instead of longint is needed
  327. Revision 1.13 2003/01/01 20:58:07 florian
  328. + added invalid instruction exception
  329. Revision 1.12 2002/10/07 19:43:24 florian
  330. + empty prototypes for the AnsiStr* multi byte functions added
  331. Revision 1.11 2002/09/07 16:01:22 peter
  332. * old logs removed and tabs fixed
  333. Revision 1.10 2002/07/16 13:57:39 florian
  334. * raise takes now a void pointer as at and frame address
  335. instead of a longint, fixed
  336. Revision 1.9 2002/01/25 17:42:03 peter
  337. * interface helpers
  338. Revision 1.8 2002/01/25 16:23:03 peter
  339. * merged filesearch() fix
  340. }