1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
{
***************************************************************************
* *
* This source is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
***************************************************************************
Author: Mattias Gaertner
Abstract:
Demonstration of how to convert c header files to pascal interfaces.
Usage:
h2pastest [filename.h]
}
program H2PasTest;
{$mode objfpc}{$H+}
uses
Classes, SysUtils,
// LazUtils
LazFileUtils,
// CodeTools
CodeCache, CodeToolManager, CCodeParserTool, FileProcs,
H2PasTool;
const
ConfigFilename = 'codetools.config';
var
Filename: String;
CCode, PasCode: TCodeBuffer;
Tool: TH2PasTool;
Caret: TCodeXYPosition;
OutputFilename: String;
CCodeTool: TCCodeParserTool;
i: Integer;
Param: String;
Filenames: TStringList;
Src: String;
Merger: TCHeaderFileMerger;
MergeAll: Boolean;
MergeFlags: TCHFileMergeFlags;
MacroName: String;
p: LongInt;
MacroValue: String;
MacroFuncName: String;
begin
Merger:=nil;
try
Tool:=TH2PasTool.Create;
Merger:=TCHeaderFileMerger.Create;
Filenames:=TStringList.Create;
OutputFilename:=CleanAndExpandFilename(AppendPathDelim(GetCurrentDir)+'h2pasoutput.pas');
MergeAll:=false;
for i:=1 to Paramcount do begin
Param:=ParamStr(i);
if copy(Param,1,2)='-d' then begin
MacroName:=copy(Param,3,length(Param));
MacroValue:='';
p:=Pos('=',MacroName);
if p>0 then begin
MacroValue:=copy(MacroName,p+1,length(MacroName));
MacroName:=copy(MacroName,1,p-1);
end;
MacroName:=copy(MacroName,1,255);
p:=System.Pos('(',MacroName);
if p<1 then begin
// macro (not macro function)
if not IsValidIdent(MacroName) then begin
writeln('invalid macro name "',MacroName,'"');
Halt;
end;
Merger.Macros[MacroName]:=MacroValue;
Tool.Defines.Add(MacroName,MacroValue);
end else begin
// maybe a macro function
MacroFuncName:=copy(MacroName,1,p-1);
if not IsValidIdent(MacroFuncName) then begin
writeln('invalid macro name "',MacroFuncName,'"');
Halt;
end;
if p=length(MacroName) then begin
writeln('invalid macro function "',MacroName,'"');
Halt;
end;
if MacroName[p+1]<>')' then begin
writeln('macro function "',MacroName,'": parameters are not supported yet');
Halt;
end;
Merger.Macros[MacroName]:=MacroValue;
end
end
else if copy(Param,1,2)='-u' then begin
MacroName:=copy(Param,3,255);
if not IsValidIdent(MacroName) then begin
writeln('invalid macro name "',MacroName,'"');
Halt;
end;
Merger.Macros[MacroName]:='';
Tool.Undefines.Add(MacroName,'');
end
else if copy(Param,1,2)='-o' then
OutputFilename:=CleanAndExpandFilename(Param)
else if Param='--merge-all' then
MergeAll:=true
else if copy(Param,1,1)='-' then begin
writeln('Usage: ',ParamStr(0),' [--merge-all] [-d<definesymbol>]... [-u<undefinesymbol>]... <main header filename> <sub header file> ... -o<Outputfilename>');
writeln();
writeln(' Scans for include directives and inserts given sub include files.');
writeln('');
writeln(' --merge-all');
writeln(' Merge all given files. Normally only files needed by the main header files are merged.');
writeln(' -u<name>');
writeln(' Undefines a macro. "#idfef name" will give false.');
writeln(' -d<name>');
writeln(' Defines a macro with an empty value. "#idfef name" will give true.');
writeln(' -d<name>=<value>');
writeln(' Defines a macro with a value. Value is not replaced recursively.');
writeln(' -d"<name>()"=<value>');
writeln(' Defines a macro function with a value. Value is not replaced recursively.');
writeln(' The function fits any number of parameters.');
writeln('');
writeln('For example:');
writeln(' ',ParamStr(0),' -dEnableFlag -dFlag2=Value -uUndefineMacro test.h header2.h');
writeln(' ',ParamStr(0),' -d"printf()" test.h');
Halt;
end else begin
Filename:=CleanAndExpandFilename(Param);
Filenames.Add(Filename);
end;
end;
CodeToolBoss.SimpleInit(ConfigFilename);
// for demonstration purpose run the tool on the example file
if Filenames.Count=0 then
Filenames.Add(CleanAndExpandFilename(GetCurrentDir+'/scanexamples/test.h'));
// Step 1: load all input files
MergeFlags:=[];
if MergeAll then Include(MergeFlags,chfmfAll);
Merger.Merge(Filenames,CodeToolBoss.SourceCache,MergeFlags);
//Merger.WriteDebugReport;
Src:=Merger.CombinedSource.Source;
{writeln;
writeln('======Combined c header files================');
writeln(Src);
writeln('=============================================');}
// ToDo:
// Tool.UndefineEnclosingIFNDEF(CCode);
// Step 2: create a temporary file
Filename:='h2pasoutput.pas';
CCode:=CodeToolBoss.CreateTempFile(Filename);
if CCode=nil then
raise Exception.Create('failed creating temporary file '+Filename);
CCode.Source:=Src;
// Step 3: create the output file
PasCode:=CodeToolBoss.CreateFile(OutputFilename);
if PasCode=nil then
raise Exception.Create('creating failed '+OutputFilename);
// Step 4: convert
Tool.SourceName:=ExtractFileNameOnly(PasCode.Filename);
Tool.Convert(CCode,PasCode);
//Tool.WriteDebugReport;
Tool.WriteH2PNodeReport;
Tool.WriteH2PDirectivesNodeReport;
writeln;
writeln('=============================================');
writeln(PasCode.Source);
// clean up
CCode.ReleaseRefCount;
Tool.Free;
Filenames.Free;
// Step 5: write unit
if PasCode.Save then
writeln('Wrote ',PasCode.Filename)
else
writeln('Failed writing ',PasCode.Filename);
FreeAndNil(Merger);
except
on E: ECCodeParserException do begin
CCodeTool:=ECCodeParserException(E).Sender;
CCodeTool.CleanPosToCaret(CCodeTool.LastErrorReportPos,Caret);
writeln('Combined source: ',
Caret.Code.Filename+'('+IntToStr(Caret.Y)+','+IntToStr(Caret.X)+')',
' ',dbgstr(copy(CCodeTool.Src,CCodeTool.LastErrorReportPos-40,40)),'|',
dbgstr(copy(CCodeTool.Src,CCodeTool.LastErrorReportPos,40)));
if Merger<>nil then begin
Merger.MergedPosToOriginal(Caret.X,Caret.Y,Caret.Code,Caret.X,Caret.Y);
end;
writeln('Origin: ',Caret.Code.Filename+'('+IntToStr(Caret.Y)+','+IntToStr(Caret.X)+')'+' Error: '+E.Message);
end;
on E: Exception do begin
writeln(E.Message);
end;
end;
end.
|