h2poptions.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. {
  2. Copyright (c) 1998-2000 by Florian Klaempfl
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  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. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. ****************************************************************************}
  15. unit h2poptions;
  16. {$H+}
  17. interface
  18. uses classes;
  19. const
  20. version = '0.99.16';
  21. var
  22. inputfilename, outputfilename : string; { Filenames }
  23. LibFileName, unitname : string; { external library name }
  24. CompactMode,
  25. stripinfo, { Don't write info comments to output }
  26. UseLib, { Append external to implementation ? }
  27. UseName, { Append 'libname name 'funcname ' }
  28. UsePPOinters, { Use P instead of ^ for pointers }
  29. EnumToConst, { Write enumeration types as constants }
  30. Win32headers, { allows dec_specifier }
  31. stripcomment, { strip comments from inputfile }
  32. PrependTypes, { Print T in front of type names ? }
  33. UseCTypesUnit, { Use types defined in the ctypes unit}
  34. createdynlib, { creates a unit which loads dynamically the imports to proc vars }
  35. RemoveUnderscore : Boolean;
  36. usevarparas : boolean; { generate var parameters, when a pointer }
  37. { is passed }
  38. includefile : boolean; { creates an include file instead of a unit }
  39. palmpilot : boolean; { handling of PalmOS SYS_CALLs }
  40. packrecords: boolean; { All records should be packed in the file }
  41. pointerprefix: boolean; { put P in front of pointers }
  42. PTypeList : TStringList; { list of all types }
  43. freedynlibproc,
  44. loaddynlibproc : tstringlist;
  45. { Helpers }
  46. Function ForceExtension(Const HStr,ext:String):String;
  47. Function MaybeExtension(Const HStr,ext:String):String;
  48. { Options }
  49. Procedure InitGlobals;
  50. Procedure ProcessOptions;
  51. Implementation
  52. {*****************************************************************************
  53. Helpers
  54. *****************************************************************************}
  55. Function ForceExtension(Const HStr,ext:String):String;
  56. {
  57. Return a filename which certainly has the extension ext
  58. (no dot in ext !!)
  59. }
  60. var
  61. j : longint;
  62. begin
  63. j:=length(Hstr);
  64. while (j>0) and (Hstr[j]<>'.') do
  65. dec(j);
  66. if j=0 then
  67. j:=255;
  68. ForceExtension:=Copy(Hstr,1,j-1)+'.'+Ext;
  69. end;
  70. Function MaybeExtension(Const HStr,ext:String):String;
  71. {
  72. Return a filename which certainly has the extension ext
  73. (no dot in ext !!)
  74. }
  75. var
  76. j : longint;
  77. begin
  78. j:=length(Hstr);
  79. while (j>0) and (Hstr[j]<>'.') do
  80. dec(j);
  81. if j=0 then
  82. MaybeExtension:=Hstr+'.'+Ext
  83. else
  84. MaybeExtension:=Hstr;
  85. end;
  86. function ExtractFileName(const AFilePath : String): String;
  87. var i : Integer;
  88. begin
  89. i := Length(AFilePath);
  90. while (i>0) and (AFilePath[i]<>DirectorySeparator) and
  91. (AFilePath[i]<>DriveSeparator) do
  92. begin
  93. Dec(i);
  94. end;
  95. ExtractFileName := Copy(AFilePath,i+1,Length(AFilePath));
  96. end;
  97. {*****************************************************************************
  98. Options
  99. *****************************************************************************}
  100. Procedure Usage;
  101. begin
  102. writeln ('Usage : ',paramstr(0),' [options] filename');
  103. writeln (' Where [options] is one or more of:');
  104. writeln (' -d Use external;');
  105. writeln (' -D use external libname name ''func_name'';');
  106. writeln (' -e change enum type to list of constants');
  107. writeln (' -c Compact outputmode, less spaces and empty lines');
  108. WriteLn (' -C Use types in ctypes unit');
  109. writeln (' -i create include files (no unit header)');
  110. writeln (' -l libname Specify the library name for external');
  111. writeln (' -o outputfilename Specify the outputfilename');
  112. writeln (' -p Use "P" instead of "^" for pointers');
  113. writeln (' -pr Pack all records (1 byte alignment)');
  114. writeln (' -P use proc. vars for imports');
  115. writeln (' -s strip comments from inputfile');
  116. writeln (' -S strip comments and don''t write info to outputfile.');
  117. writeln (' -t Prepend typedef type names with T');
  118. writeln (' -T Prepend typedef type names with T, and remove _');
  119. writeln (' -u unitname Specify the name of the unit.');
  120. writeln (' -v replace pointer parameters by call by');
  121. writeln (' reference parameters');
  122. writeln (' -w special for win32 headers');
  123. writeln (' -x handle SYS_TRAP of PalmOS header files');
  124. halt (0);
  125. end;
  126. procedure InitGlobals;
  127. begin
  128. PTypeList:=TStringList.Create;
  129. PTypeList.Sorted := true;
  130. PTypeList.Duplicates := dupIgnore;
  131. freedynlibproc:=TStringList.Create;
  132. loaddynlibproc:=TStringList.Create;
  133. end;
  134. Procedure ProcessOptions;
  135. Var
  136. cp : string[255]; {because of cp[3] indexing}
  137. I : longint;
  138. Function GetNextParam (const Opt,Name : String) : string;
  139. begin
  140. if i=paramcount then
  141. begin
  142. writeln ('Error : -',Opt,' : ',name,' expected');
  143. halt(1);
  144. end
  145. else
  146. begin
  147. GetNextParam:=paramstr(i+1);
  148. inc(i);
  149. end;
  150. end;
  151. begin
  152. if paramcount=0 then
  153. Usage;
  154. inputfilename:='';
  155. outputfilename:='';
  156. LibFileName:='';
  157. UnitName:='';
  158. CompactMode:=false;
  159. UseLib:=false;
  160. UseName:=false;
  161. StripComment:=false;
  162. StripInfo:=false;
  163. UsePPointers:=false;
  164. UseCTypesUnit := false;
  165. EnumToCOnst:=false;
  166. usevarparas:=false;
  167. palmpilot:=false;
  168. includefile:=false;
  169. packrecords:=false;
  170. createdynlib:=false;
  171. i:=1;
  172. while i<=paramcount do
  173. begin
  174. cp:=paramstr(i);
  175. if cp[1]='-' then
  176. begin
  177. case cp[2] of
  178. 'c' : CompactMode:=true;
  179. 'C' : UseCTypesUnit := true;
  180. 'e' : EnumToConst :=true;
  181. 'd' : UseLib :=true;
  182. 'D' : begin
  183. UseLib :=true;
  184. usename :=true;
  185. end;
  186. 'i' : includefile:=true;
  187. 'l' : LibFileName:=GetNextParam ('l','libname');
  188. 'o' : outputfilename:=GetNextParam('o','outputfilename');
  189. 'P' : createdynlib:=true;
  190. 'p' : begin
  191. if (cp[3] = 'r') then
  192. begin
  193. PackRecords := true;
  194. end
  195. else
  196. UsePPointers:=true;
  197. end;
  198. 's' : stripcomment:=true;
  199. 'S' : begin
  200. stripcomment:=true;
  201. stripinfo:=true;
  202. end;
  203. 't' : PrependTypes:=true;
  204. 'T' : begin
  205. PrependTypes:=true;
  206. RemoveUnderscore:=true;
  207. end;
  208. 'u' : UnitName:=GetNextParam ('u','unitname');
  209. 'v' : usevarparas:=true;
  210. 'w' : begin
  211. Win32headers:=true;
  212. UseLib:=true;
  213. usename:=true;
  214. usevarparas:=true;
  215. LibFileName:='kernel32';
  216. end;
  217. 'x' : palmpilot:=true;
  218. else
  219. Writeln ('Illegal option : ',cp);
  220. end
  221. end
  222. else
  223. begin { filename }
  224. if inputfilename<>'' then
  225. begin
  226. writeln ('Error : only one filename supported. Found also :',cp);
  227. halt(1);
  228. end;
  229. inputfilename:=MaybeExtension(cp,'h');
  230. if outputfilename='' then
  231. outputfilename:=ForceExtension (inputfilename,'pp');
  232. end;
  233. inc(i);
  234. end;
  235. If inputfilename='' then
  236. Usage;
  237. if UnitName='' then
  238. begin
  239. UnitName := ExtractFileName(outputfilename);
  240. i:=pos('.',UnitName)-1;
  241. if i>0 then
  242. UnitName:=Copy(UnitName,1,i);
  243. end;
  244. end;
  245. end.