h2poptions.pas 8.6 KB

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