importtl.pas 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. program importtl;
  2. {$mode objfpc}{$H+}
  3. {$apptype console}
  4. uses
  5. classes,typelib,sysutils,getopts;
  6. var
  7. theopts : array[1..2] of TOption;
  8. procedure InitOptions;
  9. begin
  10. with theopts[1] do
  11. begin
  12. name:='ref-style';
  13. has_arg:=Required_Argument;
  14. flag:=nil;
  15. value:=#0;
  16. end;
  17. with theopts[2] do
  18. begin
  19. name:='';
  20. has_arg:=0;
  21. flag:=nil;
  22. value:=#0;
  23. end;
  24. end;
  25. var
  26. unitname,sPackageSource,sPackageRegUnitSource:string;
  27. sTL,sOutDir:string;
  28. F:text;
  29. slDep:TStringList;
  30. i:integer;
  31. FileName : string;
  32. bNoRecurse,bHelp,bActiveX,bPackage,bRemoveStructTag:boolean;
  33. InRefStyle : TParamInputRefType;
  34. optionindex : Longint;
  35. c:char;
  36. begin
  37. InitOptions;
  38. slDep:=TStringList.Create;
  39. bNoRecurse:=false;
  40. bHelp:=false;
  41. bActiveX:=false;
  42. bPackage:=false;
  43. InRefStyle:=ParamInputVar;
  44. repeat
  45. c:=getlongopts('ad:hnpt',@theopts[1],optionindex);
  46. case c of
  47. #0 : begin
  48. case optionindex-1 of
  49. 0 : if lowercase(optarg)='var' then
  50. InRefStyle:=ParamInputVar
  51. else
  52. if lowercase(optarg)='constref' then
  53. InRefStyle:=ParamInputConstRef
  54. else
  55. if lowercase(optarg)='constrefdelphi' then
  56. InRefStyle:=ParamInputConstRefDelphi
  57. end;
  58. end;
  59. 'n' : bNoRecurse:=true;
  60. 'a' : bActiveX:=true;
  61. 'p' : bPackage:=true;
  62. 'h' : bHelp:=true;
  63. 't' : bRemoveStructTag:=true;
  64. 'd' : if (length(optarg)>0) and (optarg[1]='-') then
  65. bHelp:=true
  66. else
  67. sOutDir:=IncludeTrailingPathDelimiter(optarg);
  68. '?',':' : writeln ('Error parsing option : ',optopt);
  69. end; { case }
  70. until c=endofoptions;
  71. FileName:='';
  72. if optind=paramcount then
  73. FileName:=paramstr(optind);
  74. if bHelp or (Paramcount=0) or (filename='')then
  75. begin
  76. writeln('Usage: importtl [options] file');
  77. writeln('Reads type information from "file" and converts it into a freepascal binding');
  78. writeln('units.');
  79. writeln('Options.');
  80. writeln(' -h : displays this text.');
  81. writeln(' -a : create ActiveXContainer descendants');
  82. writeln(' -d dir: set output directory. Default: current directory.');
  83. writeln(' -n : do not recurse typelibs. Default: create bindings for all');
  84. writeln(' dependencies.');
  85. writeln(' -p : create lazarus package for ActiveXContainer descendants');
  86. writeln(' -t : remove "tag" prefix from structs');
  87. writeln(' --ref-style st : input parameter style, parameter st=var,constref');
  88. writeln(' or constrefdelphi (= XE3+ const [ref])');
  89. halt;
  90. end;
  91. slDep.Add(paramstr(Paramcount));
  92. i:=0;
  93. repeat
  94. writeln('Reading typelib from '+slDep[i]+ ' ...');
  95. sTL:=ImportTypelib(slDep[i],unitname,slDep,bActiveX,bPackage,bRemoveStructTag,sPackageSource,sPackageRegUnitSource,InRefStyle);
  96. unitname:=sOutDir+unitname;
  97. if (bPackage) and (sPackageSource<>'') then
  98. begin
  99. writeln('Writing package file to '+unitname+'P.lpk' );
  100. AssignFile(F,unitname+'P.lpk');
  101. Rewrite(F);
  102. Write(F,sPackageSource);
  103. CloseFile(F);
  104. writeln('Writing package registration file to '+unitname+'Preg.pas');
  105. AssignFile(F,unitname+'Preg.pas');
  106. Rewrite(F);
  107. Write(F,sPackageSource);
  108. CloseFile(F);
  109. end;
  110. bActiveX:=false; //don't create ActiveXContainer descendants in descendants
  111. bPackage:=false;
  112. writeln('Writing to '+unitname+'.pas');
  113. AssignFile(F,unitname+'.pas');
  114. Rewrite(F);
  115. Write(F,sTL);
  116. CloseFile(F);
  117. i:=i+1;
  118. until bNoRecurse or (i=slDep.Count);
  119. slDep.Destroy;
  120. end.