h2poptions.pas 8.0 KB

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