From d1b1ffc8b1b4e3dedab9eef06c068fe19939aece Mon Sep 17 00:00:00 2001 From: loesje_ Date: Thu, 15 Mar 2012 11:59:33 +0000 Subject: [PATCH] * Added command-line version of parser: idltopas git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@2343 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- applications/idlparser/idltopas.lpi | 82 ++++++++++++++ applications/idlparser/idltopas.pp | 163 ++++++++++++++++++++++++++++ 2 files changed, 245 insertions(+) create mode 100644 applications/idlparser/idltopas.lpi create mode 100644 applications/idlparser/idltopas.pp diff --git a/applications/idlparser/idltopas.lpi b/applications/idlparser/idltopas.lpi new file mode 100644 index 000000000..292a50f32 --- /dev/null +++ b/applications/idlparser/idltopas.lpi @@ -0,0 +1,82 @@ + + + + + + + + + + + + <UseAppBundle Value="False"/> + <ResourceType Value="res"/> + </General> + <i18n> + <EnableI18N LFM="False"/> + </i18n> + <VersionInfo> + <StringTable ProductVersion=""/> + </VersionInfo> + <BuildModes Count="1"> + <Item1 Name="Default" Default="True"/> + </BuildModes> + <PublishOptions> + <Version Value="2"/> + <IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/> + <ExcludeFileFilter Value="*.(bak|ppu|o|so);*~;backup"/> + </PublishOptions> + <RunParams> + <local> + <FormatVersion Value="1"/> + <CommandLineParams Value="/home/CNOC/joost/svn/geckoport/xulrunner-sdk-10/idl/nsIPrompt.idl"/> + </local> + </RunParams> + <Units Count="3"> + <Unit0> + <Filename Value="idltopas.pp"/> + <IsPartOfProject Value="True"/> + <UnitName Value="idltopas"/> + </Unit0> + <Unit1> + <Filename Value="idlparser.pas"/> + <IsPartOfProject Value="True"/> + <UnitName Value="idlParser"/> + </Unit1> + <Unit2> + <Filename Value="idlgenpascal.pas"/> + <IsPartOfProject Value="True"/> + <UnitName Value="idlGenPascal"/> + </Unit2> + </Units> + </ProjectOptions> + <CompilerOptions> + <Version Value="11"/> + <Target> + <Filename Value="idltopas"/> + </Target> + <SearchPaths> + <IncludeFiles Value="$(ProjOutDir)"/> + <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/> + </SearchPaths> + <Other> + <CompilerMessages> + <MsgFileName Value=""/> + </CompilerMessages> + <CompilerPath Value="$(CompPath)"/> + </Other> + </CompilerOptions> + <Debugging> + <Exceptions Count="3"> + <Item1> + <Name Value="EAbort"/> + </Item1> + <Item2> + <Name Value="ECodetoolError"/> + </Item2> + <Item3> + <Name Value="EFOpenError"/> + </Item3> + </Exceptions> + </Debugging> +</CONFIG> diff --git a/applications/idlparser/idltopas.pp b/applications/idlparser/idltopas.pp new file mode 100644 index 000000000..84ccc709b --- /dev/null +++ b/applications/idlparser/idltopas.pp @@ -0,0 +1,163 @@ +program idltopas; + +{$mode objfpc}{$H+} + +uses + {$IFDEF UNIX}{$IFDEF UseCThreads} + cthreads, + {$ENDIF}{$ENDIF} + Classes, SysUtils, CustApp, + idlParser, idlGenPascal; + +type + + { TIDLToPascal } + + TIDLToPascal = class(TCustomApplication) + protected + procedure DoRun; override; + public + constructor Create(TheOwner: TComponent); override; + destructor Destroy; override; + procedure WriteHelp; virtual; + end; + +{ TIDLToPascal } + +procedure HandleIDLFile(AFilename: string; AnOutput: TStrings; TypeConvList, CTypesList: TStrings; AlwaysAddPrefixToParam: boolean); +var + AnIDLList: TIDLList; + AnInput: TStrings; +begin + AnInput := TStringList.Create; + AnInput.LoadFromFile(AFilename); + try + AnIDLList := TIDLList.create; + try + ParseFile(AnIDLList, AnInput); + GeneratePascalSource(AnIDLList,AnOutput,TypeConvList, CTypesList, AlwaysAddPrefixToParam); + finally + AnIDLList.Free; + end; + finally + AnInput.Free; + end; +end; + +procedure TIDLToPascal.DoRun; +var + ErrorMsg: String; + i: integer; + filenames: TStrings; + output: TStrings; + OutputToFile: boolean; + OutputFilename: string; + CTypes, TypeMapList: TStrings; + AlwaysAddPrefixToParam: boolean; + +begin + filenames := TStringList.Create; + CTypes := TStringList.Create; + TypeMapList := TStringList.Create; + try + // quick check parameters + ErrorMsg:=CheckOptions('hpo::c:m:',nil,nil,filenames); + if ErrorMsg<>'' then + begin + ShowException(Exception.Create(ErrorMsg)); + Terminate; + Exit; + end; + + // parse parameters + if (HasOption('h','help')) or (filenames.Count=0) then + begin + WriteHelp; + Terminate; + Exit; + end; + + if HasOption('c') then + begin + CTypes.LoadFromFile(GetOptionValue('c')); + end; + + if HasOption('m') then + begin + TypeMapList.LoadFromFile(GetOptionValue('m')); + end; + + OutputToFile := HasOption('o'); + OutputFilename := GetOptionValue('o'); + AlwaysAddPrefixToParam := HasOption('p'); + + output := TStringList.Create; + try + for i := 0 to filenames.Count-1 do + begin + if OutputToFile and (OutputFilename='') then + Output.Clear; + + HandleIDLFile(filenames.Strings[i], output, TypeMapList, CTypes, AlwaysAddPrefixToParam); + + if OutputToFile and (OutputFilename='') then + output.SaveToFile(LowerCase(ExtractFileName(ChangeFileExt(filenames.Strings[i],'.inc')))); + end; + if not OutputToFile then + writeln(output.Text) + else if OutputFilename<>'' then + output.SaveToFile(OutputFilename); + + finally + output.Free; + end; + + finally + filenames.Free; + CTypes.Free; + TypeMapList.Free; + end; + + + // stop program loop + Terminate; +end; + +constructor TIDLToPascal.Create(TheOwner: TComponent); +begin + inherited Create(TheOwner); + StopOnException:=True; +end; + +destructor TIDLToPascal.Destroy; +begin + inherited Destroy; +end; + +procedure TIDLToPascal.WriteHelp; +begin + { add your help code here } + writeln('Usage: ',ApplicationName,' [OPTION] SOURCEFILES'); + writeln('Convert idl-SOURCEFILES to Pascal include files'); + writeln(''); + writeln(' -h Shows this help message'); + writeln(' -p Always add the ''a'' or ''an'' prefix to method-parameters names'); + writeln(' -o Write the output to file(s). The file(s) are written to the current'); + writeln(' directory with the same filenames name as the input files but with'); + writeln(' the ''.inc'' extension'); + writeln(' -o filename Write the output to one file called ''filename'''); + writeln(' -c filename Read ''filename'' to get a list of typenames that have to be'); + writeln(' prefixed with ''c'' or ''cu'' (as used in the ctypes unit)'); + writeln(' -m filename Read ''filename'' for a list of mappings between idl-type names'); + writeln(' and their Pascal counterpart'); +end; + +var + Application: TIDLToPascal; +begin + Application:=TIDLToPascal.Create(nil); + Application.Title:='IDL to Pascal parser'; + Application.Run; + Application.Free; +end. +