mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-12 06:16:05 +02:00
codetools: h2pas example: added defines, undefines, outputfile and multiple input files
git-svn-id: trunk@15905 -
This commit is contained in:
parent
7092ac70e6
commit
41d5425de5
@ -980,10 +980,17 @@ function TCodeToolManager.CreateTempFile(const AFilename: string): TCodeBuffer;
|
||||
var
|
||||
i: Integer;
|
||||
TempFilename: string;
|
||||
CurName: String;
|
||||
CurExt: String;
|
||||
begin
|
||||
TempFilename:=VirtualTempDir+PathDelim+AFilename;
|
||||
Result:=FindFile(TempFilename);
|
||||
if (Result<>nil) and (Result.ReferenceCount=0) then exit;
|
||||
CurName:=ExtractFileNameOnly(AFilename);
|
||||
CurExt:=ExtractFileExt(AFilename);
|
||||
i:=1;
|
||||
repeat
|
||||
TempFilename:=VirtualTempDir+PathDelim+IntToStr(i)+PathDelim+AFilename;
|
||||
TempFilename:=VirtualTempDir+PathDelim+CurName+IntToStr(i)+CurExt;
|
||||
Result:=FindFile(TempFilename);
|
||||
if (Result<>nil) and (Result.ReferenceCount=0) then exit;
|
||||
inc(i);
|
||||
|
@ -44,25 +44,61 @@ var
|
||||
Caret: TCodeXYPosition;
|
||||
OutputFilename: String;
|
||||
CCodeTool: TCCodeParserTool;
|
||||
i: Integer;
|
||||
Param: String;
|
||||
Filenames: TStringList;
|
||||
Src: String;
|
||||
begin
|
||||
try
|
||||
CodeToolBoss.SimpleInit(ConfigFilename);
|
||||
Filename:=CleanAndExpandFilename(GetCurrentDir+'/scanexamples/test.h');
|
||||
if ParamCount=1 then
|
||||
Filename:=CleanAndExpandFilename(ParamStr(1));
|
||||
|
||||
// Step 1: load the file
|
||||
CCode:=CodeToolBoss.LoadFile(Filename,false,false);
|
||||
if CCode=nil then
|
||||
raise Exception.Create('loading failed '+Filename);
|
||||
// Step 2: create the output file
|
||||
Tool:=TH2PasTool.Create;
|
||||
Filenames:=TStringList.Create;
|
||||
OutputFilename:=CleanAndExpandFilename(AppendPathDelim(GetCurrentDir)+'h2pasoutput.pas');
|
||||
for i:=1 to Paramcount do begin
|
||||
Param:=ParamStr(i);
|
||||
if copy(Param,1,2)='-d' then
|
||||
Tool.Defines.Add(copy(Param,3,255),'')
|
||||
else if copy(Param,1,2)='-u' then
|
||||
Tool.Undefines.Add(copy(Param,3,255),'')
|
||||
else if copy(Param,1,2)='-o' then
|
||||
OutputFilename:=CleanAndExpandFilename(Param)
|
||||
else if copy(Param,1,1)='-' then begin
|
||||
writeln('Usage: ',ParamStr(0),' [-d<definesymbol>]... [-u<undefinesymbol>]... <header filename1> ... -o<Outputfilename>');
|
||||
end else begin
|
||||
Filename:=CleanAndExpandFilename(Param);
|
||||
Filenames.Add(Filename);
|
||||
end;
|
||||
end;
|
||||
|
||||
// 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 filenames
|
||||
Src:='';
|
||||
for i:=0 to Filenames.Count-1 do begin
|
||||
Filename:=Filenames[i];
|
||||
CCode:=CodeToolBoss.LoadFile(Filename,false,false);
|
||||
if CCode=nil then
|
||||
raise Exception.Create('loading failed '+Filename);
|
||||
if Src<>'' then
|
||||
Src:=Src+LineEnding;
|
||||
Src:=Src+CCode.Source;
|
||||
end;
|
||||
|
||||
// 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);
|
||||
|
||||
// Step3: convert
|
||||
Tool:=TH2PasTool.Create;
|
||||
// Step 4: convert
|
||||
Tool.SourceName:=ExtractFileNameOnly(PasCode.Filename);
|
||||
Tool.Convert(CCode,PasCode);
|
||||
//Tool.WriteDebugReport;
|
||||
@ -71,9 +107,13 @@ begin
|
||||
writeln;
|
||||
writeln('=============================================');
|
||||
writeln(PasCode.Source);
|
||||
|
||||
// clean up
|
||||
CCode.ReleaseRefCount;
|
||||
Tool.Free;
|
||||
|
||||
// Step 4: write unit
|
||||
Filenames.Free;
|
||||
|
||||
// Step 5: write unit
|
||||
if PasCode.Save then
|
||||
writeln('Wrote ',PasCode.Filename)
|
||||
else
|
||||
|
@ -566,6 +566,7 @@ end;
|
||||
function ExtractFileNameOnly(const AFilename: string): string;
|
||||
var ExtLen: integer;
|
||||
begin
|
||||
// beware: filename.ext1.ext2
|
||||
Result:=ExtractFilename(AFilename);
|
||||
ExtLen:=length(ExtractFileExt(Result));
|
||||
Result:=copy(Result,1,length(Result)-ExtLen);
|
||||
|
Loading…
Reference in New Issue
Block a user