custapp.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2003 by the Free Pascal development team
  5. CustomApplication class.
  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. {$mode objfpc}
  13. {$h+}
  14. unit CustApp;
  15. Interface
  16. uses SysUtils,Classes;
  17. Type
  18. TExceptionEvent = Procedure (Sender : TObject; E : Exception) Of Object;
  19. TCustomApplication = Class(TComponent)
  20. Private
  21. FOnException: TExceptionEvent;
  22. FTerminated : Boolean;
  23. FHelpFile,
  24. FTitle : String;
  25. FOptionChar : Char;
  26. FCaseSensitiveOptions : Boolean;
  27. FStopOnException : Boolean;
  28. function GetEnvironmentVar(VarName : String): String;
  29. function GetExeName: string;
  30. Function GetLocation : String;
  31. function GetTitle: string;
  32. Protected
  33. procedure SetTitle(const AValue: string); Virtual;
  34. Function GetConsoleApplication : boolean; Virtual;
  35. Procedure DoRun; Virtual;
  36. Function GetParams(Index : Integer) : String;virtual;
  37. function GetParamCount: Integer;Virtual;
  38. Public
  39. constructor Create(AOwner: TComponent); override;
  40. destructor Destroy; override;
  41. // Some Delphi methods.
  42. procedure HandleException(Sender: TObject); virtual;
  43. procedure Initialize; virtual;
  44. procedure Run;
  45. procedure ShowException(E: Exception);virtual;
  46. procedure Terminate; virtual;
  47. // Extra methods.
  48. function FindOptionIndex(Const S : String; Var Longopt : Boolean) : Integer;
  49. Function GetOptionValue(Const S : String) : String;
  50. Function GetOptionValue(Const C: Char; Const S : String) : String;
  51. Function HasOption(Const S : String) : Boolean;
  52. Function HasOption(Const C : Char; Const S : String) : Boolean;
  53. Function CheckOptions(Const ShortOptions : String; Const Longopts : TStrings; Opts,NonOpts : TStrings) : String;
  54. Function CheckOptions(Const ShortOptions : String; Const Longopts : TStrings) : String;
  55. Function CheckOptions(Const ShortOptions : String; Const LongOpts : Array of string) : String;
  56. Function CheckOptions(Const ShortOptions : String; Const LongOpts : String) : String;
  57. Procedure GetEnvironmentList(List : TStrings;NamesOnly : Boolean);
  58. Procedure GetEnvironmentList(List : TStrings);
  59. // Delphi properties
  60. property ExeName: string read GetExeName;
  61. property HelpFile: string read FHelpFile write FHelpFile;
  62. property Terminated: Boolean read FTerminated;
  63. property Title: string read FTitle write SetTitle;
  64. property OnException: TExceptionEvent read FOnException write FOnException;
  65. // Extra properties
  66. Property ConsoleApplication : Boolean Read GetConsoleApplication;
  67. Property Location : String Read GetLocation;
  68. Property Params [Index : integer] : String Read GetParams;
  69. Property ParamCount : Integer Read GetParamCount;
  70. Property EnvironmentVariable[envName : String] : String Read GetEnvironmentVar;
  71. Property OptionChar : Char Read FoptionChar Write FOptionChar;
  72. Property CaseSensitiveOptions : Boolean Read FCaseSensitiveOptions Write FCaseSensitiveOptions;
  73. Property StopOnException : Boolean Read FStopOnException Write FStopOnException;
  74. end;
  75. Implementation
  76. {$i custapp.inc}
  77. { TCustomApplication }
  78. function TCustomApplication.GetExeName: string;
  79. begin
  80. Result:=Paramstr(0);
  81. end;
  82. function TCustomApplication.GetEnvironmentVar(VarName : String): String;
  83. begin
  84. Result:=GetEnvironmentVariable(VarName);
  85. end;
  86. Procedure TCustomApplication.GetEnvironmentList(List : TStrings;NamesOnly : Boolean);
  87. begin
  88. // Routine must be in custapp.inc
  89. SysGetEnvironmentList(List,NamesOnly);
  90. end;
  91. Procedure TCustomApplication.GetEnvironmentList(List : TStrings);
  92. begin
  93. GetEnvironmentList(List,False);
  94. end;
  95. function TCustomApplication.GetLocation: String;
  96. begin
  97. Result:=ExtractFilePath(GetExeName);
  98. end;
  99. function TCustomApplication.GetParamCount: Integer;
  100. begin
  101. Result:=System.ParamCount;
  102. end;
  103. function TCustomApplication.GetTitle: string;
  104. begin
  105. Result:=FTitle;
  106. end;
  107. function TCustomApplication.GetParams(Index: Integer): String;
  108. begin
  109. Result:=ParamStr(Index);
  110. end;
  111. procedure TCustomApplication.SetTitle(const AValue: string);
  112. begin
  113. FTitle:=AValue;
  114. end;
  115. function TCustomApplication.GetConsoleApplication: boolean;
  116. begin
  117. Result:=IsConsole;
  118. end;
  119. procedure TCustomApplication.DoRun;
  120. begin
  121. // Do nothing. Override in descendent classes.
  122. end;
  123. constructor TCustomApplication.Create(AOwner: TComponent);
  124. begin
  125. inherited Create(AOwner);
  126. FOptionChar:='-';
  127. FCaseSensitiveOptions:=True;
  128. FStopOnException:=False;
  129. end;
  130. destructor TCustomApplication.Destroy;
  131. begin
  132. inherited Destroy;
  133. end;
  134. procedure TCustomApplication.HandleException(Sender: TObject);
  135. begin
  136. If Not (ExceptObject is Exception) then
  137. SysUtils.showexception(ExceptObject,ExceptAddr)
  138. else
  139. begin
  140. If Not Assigned(FOnexception) then
  141. ShowException(Exception(ExceptObject))
  142. else
  143. FOnException(Sender,Exception(ExceptObject));
  144. end;
  145. If FStopOnException then
  146. FTerminated:=True;
  147. end;
  148. procedure TCustomApplication.Initialize;
  149. begin
  150. FTerminated:=False;
  151. end;
  152. procedure TCustomApplication.Run;
  153. begin
  154. Repeat
  155. Try
  156. DoRun;
  157. except
  158. HandleException(Self);
  159. end;
  160. Until FTerminated;
  161. end;
  162. procedure TCustomApplication.ShowException(E: Exception);
  163. begin
  164. Sysutils.ShowException(E,ExceptAddr)
  165. end;
  166. procedure TCustomApplication.Terminate;
  167. begin
  168. FTerminated:=True;
  169. end;
  170. function TCustomApplication.GetOptionValue(Const S: String): String;
  171. begin
  172. Result:=GetoptionValue(#255,S);
  173. end;
  174. function TCustomApplication.GetOptionValue(Const C: Char; Const S: String): String;
  175. Var
  176. B : Boolean;
  177. I,P : integer;
  178. O : String;
  179. begin
  180. Result:='';
  181. I:=FindOptionIndex(C,B);
  182. If (I=-1) then
  183. I:=FindoptionIndex(S,B);
  184. If (I<>-1) then
  185. begin
  186. If B then
  187. begin // Long options have form --option=value
  188. O:=Params[I];
  189. P:=Pos('=',O);
  190. If (P=0) then
  191. P:=Length(O);
  192. Delete(O,1,P);
  193. Result:=O;
  194. end
  195. else
  196. begin // short options have form '-o value'
  197. If (I<ParamCount) then
  198. Result:=Params[I+1];
  199. end;
  200. end;
  201. end;
  202. function TCustomApplication.HasOption(Const S: String): Boolean;
  203. Var
  204. B : Boolean;
  205. begin
  206. Result:=FindOptionIndex(S,B)<>-1;
  207. end;
  208. function TCustomApplication.FindOptionIndex(Const S : String; Var Longopt : Boolean) : Integer;
  209. Var
  210. SO,O : String;
  211. I,P : Integer;
  212. begin
  213. If Not CaseSensitiveOptions then
  214. SO:=UpperCase(S)
  215. else
  216. SO:=S;
  217. Result:=-1;
  218. I:=ParamCount;
  219. While (Result=-1) and (I>0) do
  220. begin
  221. O:=Params[i];
  222. If (Length(O)>0) and (O[1]=FOptionChar) then
  223. begin
  224. Delete(O,1,1);
  225. LongOpt:=(Length(O)>0) and (O[1]=FOptionChar);
  226. If LongOpt then
  227. begin
  228. Delete(O,1,1);
  229. P:=Pos('=',O);
  230. If (P<>0) then
  231. O:=Copy(O,1,P-1);
  232. end;
  233. If Not CaseSensitiveOptions then
  234. O:=UpperCase(O);
  235. If (O=SO) then
  236. Result:=i;
  237. end;
  238. Dec(i);
  239. end;
  240. end;
  241. function TCustomApplication.HasOption(Const C: Char; Const S: String): Boolean;
  242. Var
  243. B : Boolean;
  244. begin
  245. Result:=(FindOptionIndex(C,B)<>-1) or (FindOptionIndex(S,B)<>-1);
  246. end;
  247. Function TCustomApplication.CheckOptions(Const ShortOptions : String; Const Longopts : TStrings) : String;
  248. begin
  249. Result:=CheckOptions(ShortOptions,LongOpts,Nil,Nil);
  250. end;
  251. ResourceString
  252. SErrInvalidOption = 'Invalid option at position %d: "%s"';
  253. SErrNoOptionAllowed = 'Option at position %d does not allow an argument: %s';
  254. SErrOptionNeeded = 'Option at position %d needs an argument : %s';
  255. Function TCustomApplication.CheckOptions(Const ShortOptions : String; Const Longopts : TStrings; Opts,NonOpts : TStrings) : String;
  256. Var
  257. I,J,L,P : Integer;
  258. O,OV,SO : String;
  259. HaveArg : Boolean;
  260. Function FindLongOpt(S : String) : boolean;
  261. Var
  262. I : integer;
  263. begin
  264. If CaseSensitiveOptions then
  265. begin
  266. I:=LongOpts.Count-1;
  267. While (I>=0) and (LongOpts[i]<>S) do
  268. Dec(i);
  269. end
  270. else
  271. begin
  272. S:=UpperCase(S);
  273. I:=LongOpts.Count-1;
  274. While (I>=0) and (UpperCase(LongOpts[i])<>S) do
  275. Dec(i);
  276. end;
  277. Result:=(I<>-1);
  278. end;
  279. begin
  280. If CaseSensitiveOptions then
  281. SO:=Shortoptions
  282. else
  283. SO:=LowerCase(Shortoptions);
  284. Result:='';
  285. I:=1;
  286. While (I<=ParamCount) and (Result='') do
  287. begin
  288. O:=Paramstr(I);
  289. If (Length(O)=0) or (O[1]<>FOptionChar) then
  290. begin
  291. If Assigned(NonOpts) then
  292. NonOpts.Add(O)
  293. end
  294. else
  295. begin
  296. If (Length(O)<2) then
  297. Result:=Format(SErrInvalidOption,[i,O])
  298. else
  299. begin
  300. HaveArg:=False;
  301. OV:='';
  302. // Long option ?
  303. If (O[2]=FOptionChar) then
  304. begin
  305. Delete(O,1,2);
  306. J:=Pos('=',O);
  307. If J<>0 then
  308. begin
  309. HaveArg:=true;
  310. OV:=O;
  311. Delete(OV,1,J);
  312. O:=Copy(O,1,J-1);
  313. end;
  314. // Switch Option
  315. If FindLongopt(O) then
  316. begin
  317. If HaveArg then
  318. Result:=Format(SErrNoOptionAllowed,[I,O])
  319. end
  320. else
  321. begin // Required argument
  322. If FindLongOpt(O+':') then
  323. begin
  324. If Not HaveArg then
  325. Result:=Format(SErrOptionNeeded,[I,O]);
  326. end
  327. else
  328. begin // Optional Argument.
  329. If not FindLongOpt(O+'::') then
  330. Result:=Format(SErrInvalidOption,[I,O]);
  331. end;
  332. end;
  333. end
  334. else // Short Option.
  335. begin
  336. HaveArg:=(I<ParamCount) and (Length(ParamStr(I+1))>0) and (ParamStr(I+1)[i]<>FOptionChar);
  337. If HaveArg then
  338. OV:=Paramstr(I+1);
  339. If Not CaseSensitiveOptions then
  340. O:=LowerCase(O);
  341. L:=Length(O);
  342. J:=2;
  343. While (result='') and (J<=L) do
  344. begin
  345. P:=Pos(O[J],ShortOptions);
  346. If (P=0) or (O[j]=':') then
  347. Result:=Format(SErrInvalidOption,[I,O[J]])
  348. else
  349. begin
  350. If (P<Length(ShortOptions)) and (Shortoptions[P+1]=':') then
  351. begin
  352. // Required argument
  353. Writeln('P ',P,' J ',J,' ',O[J],' ',l,' Havearg ',HaveArg);
  354. If ((P+1)=Length(ShortOptions)) or (Shortoptions[P+2]<>':') Then
  355. If (J<L) or not haveArg then // Must be last in multi-opt !!
  356. Result:=Format(SErrOptionNeeded,[I,O[J]]);
  357. O:=O[j]; // O is added to arguments.
  358. end;
  359. end;
  360. Inc(J);
  361. end;
  362. If HaveArg then
  363. begin
  364. Inc(I); // Skip argument.
  365. O:=O[Length(O)]; // O is added to arguments !
  366. end;
  367. end;
  368. If HaveArg and (Result='') then
  369. If Assigned(Opts) then
  370. Opts.Add(O+'='+OV);
  371. end;
  372. end;
  373. Inc(I);
  374. end;
  375. end;
  376. Function TCustomApplication.CheckOptions(Const ShortOptions : String; Const LongOpts : Array of string) : String;
  377. Var
  378. L : TStringList;
  379. I : Integer;
  380. begin
  381. L:=TStringList.Create;
  382. Try
  383. For I:=0 to High(LongOpts) do
  384. L.Add(LongOpts[i]);
  385. Result:=CheckOptions(ShortOptions,L);
  386. Finally
  387. L.Free;
  388. end;
  389. end;
  390. Function TCustomApplication.CheckOptions(Const ShortOptions : String; Const LongOpts : String) : String;
  391. Const
  392. SepChars = ' '#10#13#9;
  393. Var
  394. L : TStringList;
  395. Len,I,J : Integer;
  396. begin
  397. L:=TStringList.Create;
  398. Try
  399. I:=1;
  400. Len:=Length(LongOpts);
  401. While I<=Len do
  402. begin
  403. While Isdelimiter(SepChars,LongOpts,I) do
  404. Inc(I);
  405. J:=I;
  406. While (J<=Len) and Not IsDelimiter(SepChars,LongOpts,J) do
  407. Inc(J);
  408. If (I<=J) then
  409. L.Add(Copy(LongOpts,I,(J-I)));
  410. I:=J+1;
  411. end;
  412. Result:=CheckOptions(Shortoptions,L);
  413. Finally
  414. L.Free;
  415. end;
  416. end;
  417. end.
  418. {
  419. $Log$
  420. Revision 1.6 2003-06-01 14:40:05 marco
  421. * Fix to get it to build "name" was considered dup ident. Changed to
  422. envname and added logs
  423. }