sysutils.inc 9.1 KB

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