sysutils.inc 9.2 KB

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