h2poptions.pas 8.0 KB

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