webidl2pas.pp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. {
  2. This file is part of the Free Component Library
  3. WEBIDL to pascal code converter program
  4. Copyright (c) 2018 by Michael Van Canneyt [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program webidl2pas;
  12. {$mode objfpc}{$H+}
  13. uses
  14. Classes, SysUtils, CustApp, webidlscanner, webidltopas, pascodegen, typinfo;
  15. type
  16. { TWebIDLToPasApplication }
  17. TWebIDLToPasApplication = class(TCustomApplication)
  18. private
  19. FWebIDLToPas: TWebIDLToPas;
  20. function Checkoption(Var O: TCOnversionOPtions; C: TCOnversionOPtion;
  21. const AShort: Char; const aLong: String): Boolean;
  22. procedure DoConvertLog(Sender: TObject; {%H-}LogType: TCodegenLogType; const Msg: String);
  23. function GetInputFileName: String;
  24. function GetOutputFileName: String;
  25. function GetUnitName: String;
  26. procedure SetinputFileName(AValue: String);
  27. procedure SetOutputFileName(AValue: String);
  28. procedure SetunitName(AValue: String);
  29. protected
  30. procedure DoRun; override;
  31. Protected
  32. Property WebIDLToPas : TWebIDLToPas Read FWebIDLToPas;
  33. public
  34. constructor Create(TheOwner: TComponent); override;
  35. destructor Destroy; override;
  36. procedure WriteHelp(Const Msg : string); virtual;
  37. Property UnitName : String Read GetUnitName Write SetunitName;
  38. property InputFileName : String Read GetInputFileName Write SetinputFileName;
  39. property OutputFileName : String Read GetOutputFileName Write SetOutputFileName;
  40. end;
  41. { TWebIDLToPasApplication }
  42. function TWebIDLToPasApplication.GetInputFileName: String;
  43. begin
  44. Result:=FWebIDLToPas.InputFileName;
  45. end;
  46. procedure TWebIDLToPasApplication.DoConvertLog(Sender: TObject;
  47. LogType: TCodegenLogType; const Msg: String);
  48. begin
  49. {AllowWriteln}
  50. Writeln(Msg);
  51. {AllowWriteln-}
  52. end;
  53. function TWebIDLToPasApplication.GetOutputFileName: String;
  54. begin
  55. Result:=FWebIDLToPas.OutputFileName
  56. end;
  57. function TWebIDLToPasApplication.GetUnitName: String;
  58. begin
  59. Result:=FWebIDLToPas.OutputUnitName;
  60. end;
  61. procedure TWebIDLToPasApplication.SetinputFileName(AValue: String);
  62. begin
  63. FWebIDLToPas.InputFileName:=aValue;
  64. end;
  65. procedure TWebIDLToPasApplication.SetOutputFileName(AValue: String);
  66. begin
  67. FWebIDLToPas.OutputFileName:=aValue;
  68. end;
  69. procedure TWebIDLToPasApplication.SetunitName(AValue: String);
  70. begin
  71. FWebIDLToPas.OutputUnitName:=aValue;
  72. end;
  73. Function TWebIDLToPasApplication.Checkoption(Var O : TCOnversionOPtions;C : TCOnversionOPtion; Const AShort : Char; Const aLong : String) : Boolean;
  74. begin
  75. Result:=HasOption(aShort,ALong);
  76. if Result then
  77. Include(O,C);
  78. end;
  79. procedure TWebIDLToPasApplication.DoRun;
  80. var
  81. A,ErrorMsg: String;
  82. O : TConversionOptions;
  83. I : Integer;
  84. begin
  85. Terminate;
  86. // quick check parameters
  87. ErrorMsg:=CheckOptions('hi:o:u:m:n:vx:t:ced::pw:', ['help','input:','output:','unitname:','include:','implementation:','verbose','extra:','typealiases:','constexternal','expandunionargs','dicttoclass::','optionsinheader','webidlversion:']);
  88. if (ErrorMsg<>'') or HasOption('h','help') then
  89. begin
  90. WriteHelp(ErrorMsg);
  91. Exit;
  92. end;
  93. O:=[];
  94. Checkoption(O,coExternalConst,'c','constexternal');
  95. Checkoption(O,coExpandUnionTypeArgs,'e','expandunionargs');
  96. CheckOption(O,coaddOptionsToheader,'p','optionsinheader');
  97. if Checkoption(O,coDictionaryAsClass,'d','dicttoclass') then
  98. FWebIDLToPas.DictionaryClassParent:=GetOptionValue('d','dicttoclass');
  99. FWebIDLToPas.Options:=O;
  100. InputFileName:=GetOptionValue('i','input');
  101. OutputFileName:=GetOptionValue('o','output');
  102. UnitName:=GetOptionValue('u','unitname');
  103. FWebIDLToPas.Verbose:=HasOption('v','verbose');
  104. if HasOption('w','webidlversion') then
  105. begin
  106. A:=GetOptionValue('w','webidlversion');
  107. I:=GetEnumValue(TypeInfo(TWebIDLVersion),A);
  108. if (I<>-1) then
  109. FWebIDLToPas.WebIDLVersion:=TWebIDLVersion(I)
  110. else
  111. Raise EConvertError.CreateFmt('Invalid webidl version: %s',[A]);
  112. end;
  113. if hasoption('n','include') then
  114. FWebIDLToPas.IncludeInterfaceCode.LoadFromFile(GetOptionValue('n','include'));
  115. if hasoption('m','implementation') then
  116. FWebIDLToPas.IncludeImplementationCode.LoadFromFile(GetOptionValue('m','implementation'));
  117. FWebIDLToPas.ExtraUnits:=GetOPtionValue('x','extra');
  118. A:=GetOptionValue('t','typealiases');
  119. if (Copy(A,1,1)='@') then
  120. begin
  121. Delete(A,1,1);
  122. FWebIDLToPas.TypeAliases.LoadFromFile(A);
  123. end
  124. else
  125. FWebIDLToPas.TypeAliases.CommaText:=A;
  126. if UnitName='' then
  127. UnitName:=ChangeFileExt(ExtractFileName(InputFileName),'');
  128. if OutputFileName='' then
  129. begin
  130. if (UnitName<>'') then
  131. OutputFileName:=ExtractFilePath(InputFileName)+UnitName+'.pas';
  132. end;
  133. FWebIDLToPas.Execute;
  134. // stop program loop
  135. Terminate;
  136. end;
  137. constructor TWebIDLToPasApplication.Create(TheOwner: TComponent);
  138. begin
  139. inherited Create(TheOwner);
  140. StopOnException:=True;
  141. FWebIDLToPas:=TWebIDLToPas.Create(Self);
  142. FWebIDLToPas.OnLog:=@DoConvertLog;
  143. FWebIDLToPas.ClassPrefix:='TJS';
  144. FWebIDLToPas.ClassSuffix:='';
  145. FWebIDLToPas.KeywordSuffix:='_';
  146. FWebIDLToPas.KeywordPrefix:='';
  147. end;
  148. destructor TWebIDLToPasApplication.Destroy;
  149. begin
  150. FreeAndNil(FWebIDLToPas);
  151. inherited Destroy;
  152. end;
  153. procedure TWebIDLToPasApplication.WriteHelp(const Msg: string);
  154. begin
  155. {AllowWriteln}
  156. if (Msg<>'') then
  157. Writeln(StdErr,'Error : ',Msg);
  158. writeln(StdErr,'Usage: ', ExeName, ' [options]');
  159. Writeln(StdErr,'Where option is one or more of');
  160. Writeln(StdErr,'-h --help this help text');
  161. Writeln(StdErr,'-c --constexternal Write consts as external const (no value)');
  162. Writeln(StdErr,'-e --expandunionargs Add overloads for all Union typed function arguments');
  163. Writeln(StdErr,'-d --dicttoclass[=Parent] Write dictionaries as classes');
  164. Writeln(StdErr,'-i --input=FileName input webidl file');
  165. Writeln(StdErr,'-m --implementation=Filename include file as implementation');
  166. Writeln(StdErr,'-n --include=Filename include file at end of interface');
  167. Writeln(StdErr,'-o --output=FileName output file. Defaults to unit name with .pas extension appended.');
  168. Writeln(StdErr,'-p --optionsinheader add options to header of generated file');
  169. Writeln(StdErr,'-t --typealiases=alias A comma separated list of type aliases in Alias=Name form');
  170. Writeln(StdErr,' use @filename to load the aliases from file.');
  171. Writeln(StdErr,'-u --unitname=Name name for unit. Defaults to input file without extension.');
  172. Writeln(StdErr,'-v --verbose Output some diagnostic information');
  173. Writeln(StdErr,'-w --webidlversion=V Set web IDL version. Allowed values: v1 or v2');
  174. Writeln(StdErr,'-x --extra=units Extra units to put in uses clause (comma separated list)');
  175. ExitCode:=Ord(Msg<>'');
  176. {AllowWriteln-}
  177. end;
  178. var
  179. Application: TWebIDLToPasApplication;
  180. begin
  181. Application:=TWebIDLToPasApplication.Create(nil);
  182. Application.Title:='WebIDL To Pascal converter Application';
  183. Application.Run;
  184. Application.Free;
  185. end.