sysutils.inc 9.3 KB

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