sysutils.inc 9.4 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. { 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. Raise E at longint(Address){$ifdef ENHANCEDRAISE},longint(Frame){$endif};
  200. end;
  201. Procedure AssertErrorHandler (Const Msg,FN : ShortString;LineNo,TheAddr : Longint);
  202. Var
  203. S : String;
  204. begin
  205. If Msg='' then
  206. S:=SAssertionFailed
  207. else
  208. S:=Msg;
  209. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]); // at Pointer(theAddr);
  210. end;
  211. {$ifdef STACKCHECK_WAS_ON}
  212. {$S+}
  213. {$endif}
  214. Procedure InitExceptions;
  215. {
  216. Must install uncaught exception handler (ExceptProc)
  217. and install exceptions for system exceptions or signals.
  218. (e.g: SIGSEGV -> ESegFault or so.)
  219. }
  220. begin
  221. ExceptProc:=@CatchUnhandledException;
  222. // Create objects that may have problems when there is no memory.
  223. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  224. OutOfMemory.AllowFree:=false;
  225. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  226. InvalidPointer.AllowFree:=false;
  227. AssertErrorProc:=@AssertErrorHandler;
  228. ErrorProc:=@RunErrorToExcept;
  229. OnShowException:=Nil;
  230. end;
  231. Procedure DoneExceptions;
  232. begin
  233. OutOfMemory.AllowFree:=true;
  234. OutOfMemory.Free;
  235. InValidPointer.AllowFree:=true;
  236. InValidPointer.Free;
  237. end;
  238. { Exception handling routines }
  239. function ExceptObject: TObject;
  240. begin
  241. If RaiseList=Nil then
  242. Result:=Nil
  243. else
  244. Result:=RaiseList^.FObject;
  245. end;
  246. function ExceptAddr: Pointer;
  247. begin
  248. If RaiseList=Nil then
  249. Result:=Nil
  250. else
  251. Result:=RaiseList^.Addr;
  252. end;
  253. function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
  254. Buffer: PChar; Size: Integer): Integer;
  255. Var
  256. S : AnsiString;
  257. Len : Integer;
  258. begin
  259. S:=Format(SExceptionErrorMessage,[ExceptObject.ClassName,ExceptAddr]);
  260. If ExceptObject is Exception then
  261. S:=Format('%s:'#10'%s',[S,Exception(ExceptObject).Message]);
  262. Len:=Length(S);
  263. If S[Len]<>'.' then
  264. begin
  265. S:=S+'.';
  266. Inc(len);
  267. end;
  268. If Len>Size then
  269. Len:=Size;
  270. if Len > 0 then
  271. Move(S[1],Buffer^,Len);
  272. Result:=Len;
  273. end;
  274. procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
  275. // use shortstring. On exception, the heap may be corrupt.
  276. Var
  277. Buf : ShortString;
  278. begin
  279. SetLength(Buf,ExceptionErrorMessage(ExceptObject,ExceptAddr,@Buf[1],255));
  280. If IsConsole Then
  281. writeln(Buf)
  282. else
  283. If Assigned(OnShowException) Then
  284. OnShowException (Buf);
  285. end;
  286. procedure Abort;
  287. begin
  288. Raise EAbort.Create(SAbortError) at Get_Caller_addr(Get_Frame)
  289. end;
  290. procedure OutOfMemoryError;
  291. begin
  292. Raise OutOfMemory;
  293. end;
  294. {
  295. $Log$
  296. Revision 1.9 2002-01-25 17:42:03 peter
  297. * interface helpers
  298. Revision 1.8 2002/01/25 16:23:03 peter
  299. * merged filesearch() fix
  300. Revision 1.7 2001/10/22 21:40:55 peter
  301. * InterLocked routines added
  302. Revision 1.6 2001/08/19 21:02:02 florian
  303. * fixed and added a lot of stuff to get the Jedi DX( headers
  304. compiled
  305. Revision 1.5 2001/08/12 22:11:48 peter
  306. * freeandnil added
  307. Revision 1.4 2001/06/03 15:18:01 peter
  308. * eoutofmemory and einvalidpointer fix
  309. Revision 1.3 2000/11/23 11:04:26 sg
  310. * Protected some Move()'s by 'if' clauses so that the Move won't be
  311. executed when the length would be 0. Otherwise, the corresponding
  312. routines might get an RTE when compiled with $R+.
  313. Revision 1.2 2000/08/20 15:46:46 peter
  314. * sysutils.pp moved to target and merged with disk.inc, filutil.inc
  315. }