From d3ebe6ba7839cead55c583d91cf908ad32c8457a Mon Sep 17 00:00:00 2001 From: mattias Date: Fri, 24 Jun 2011 18:38:00 +0000 Subject: [PATCH] lazbuild: convert encoding of loaded source files git-svn-id: trunk@31363 - --- ide/dialogprocs.pas | 31 +++++++++++++++++----- ide/lazarusidestrconsts.pas | 3 --- ide/lazbuild.lpr | 52 ++++++++++++++++++++++++++++++++++--- packager/packagedefs.pas | 4 +-- 4 files changed, 74 insertions(+), 16 deletions(-) diff --git a/ide/dialogprocs.pas b/ide/dialogprocs.pas index d4e268d0f1..71831e2f52 100644 --- a/ide/dialogprocs.pas +++ b/ide/dialogprocs.pas @@ -636,10 +636,13 @@ var LFMMemStream, LRSMemStream: TMemoryStream; LFMBuffer: TCodeBuffer; LRSBuffer: TCodeBuffer; + FormClassName: String; + BinStream: TMemoryStream; begin // read lfm file Result:=LoadCodeBuffer(LFMBuffer,LFMFilename,[lbfUpdateFromDisk],ShowAbort); if Result<>mrOk then exit; + //debugln(['ConvertLFMToLRSFileInteractive ',LFMBuffer.Filename,' DiskEncoding=',LFMBuffer.DiskEncoding,' MemEncoding=',LFMBuffer.MemEncoding]); LFMMemStream:=nil; LRSMemStream:=nil; try @@ -647,13 +650,27 @@ begin LFMBuffer.SaveToStream(LFMMemStream); LFMMemStream.Position:=0; LRSMemStream:=TMemoryStream.Create; - // convert - if not LFMtoLRSstream(LFMMemStream,LRSMemStream) then begin - Result:=IDEMessageDialogAb(lisStreamError, - Format(lisUnableToUpdateTheBinaryResourceFileFromFileTheText, [#13, - LRSFilename, #13, #13, LFMFilename, #13, #13]), - mtError,[mbCancel,mbIgnore],ShowAbort); - exit; + try + FormClassName:=FindLFMClassName(LFMMemStream); + //debugln(['ConvertLFMToLRSFileInteractive FormClassName="',FormClassName,'"']); + BinStream:=TMemoryStream.Create; + try + LRSObjectTextToBinary(LFMMemStream,BinStream); + BinStream.Position:=0; + BinaryToLazarusResourceCode(BinStream,LRSMemStream,FormClassName,'FORMDATA'); + finally + BinStream.Free; + end; + except + on E: Exception do begin + {$IFNDEF DisableChecks} + DebugLn('LFMtoLRSstream ',E.Message); + {$ENDIF} + Result:=IDEMessageDialogAb('Error', + 'Error while converting '+LFMFilename+' to '+LRSFilename+':'#13 + +E.Message,mtError,[mbCancel,mbIgnore],ShowAbort); + exit; + end; end; LRSMemStream.Position:=0; // save lrs file diff --git a/ide/lazarusidestrconsts.pas b/ide/lazarusidestrconsts.pas index 0d5f9b3ef4..ec059d2108 100644 --- a/ide/lazarusidestrconsts.pas +++ b/ide/lazarusidestrconsts.pas @@ -4564,9 +4564,6 @@ resourcestring lisFileLinkError = 'File link error'; lisWriteErrorFile = 'Write error: %s%sFile: %s%s%s'; lisStreamError = 'Stream Error'; - lisUnableToUpdateTheBinaryResourceFileFromFileTheText = 'Unable to update ' - +'the binary resource file%s%s%sfrom file the text resource file%s%s%s%' - +'sProbably the text file is corrupt.'; lisTheCodetoolsFoundAnError = 'The codetools found an error:%s%s%s'; lisIgnoreAndContinue = 'Ignore and continue'; lisNotImplemented = 'Not implemented'; diff --git a/ide/lazbuild.lpr b/ide/lazbuild.lpr index 0a1781b616..3b8e21ebbf 100644 --- a/ide/lazbuild.lpr +++ b/ide/lazbuild.lpr @@ -26,9 +26,9 @@ program lazbuild; uses Classes, SysUtils, CustApp, LCLProc, Dialogs, Forms, Controls, FileUtil, - Interfaces, InterfaceBase, UTF8Process, + Interfaces, InterfaceBase, UTF8Process, LConvEncoding, // codetools - CodeToolManager, DefineTemplates, Laz_XMLCfg, + CodeCache, CodeToolManager, DefineTemplates, Laz_XMLCfg, // IDEIntf MacroIntf, PackageIntf, IDEDialogs, ProjectIntf, IDEExternToolIntf, CompOptsIntf, // IDE @@ -64,6 +64,12 @@ type procedure OnExtToolNeedsOutputFilter(var OutputFilter: TOutputFilter; var {%H-}Abort: boolean); + // codetools + procedure OnCodeBufferDecodeLoaded(Code: TCodeBuffer; + const Filename: string; var Source, DiskEncoding, MemEncoding: string); + procedure OnCodeBufferEncodeSaving(Code: TCodeBuffer; + const Filename: string; var Source: string); + // global package functions procedure GetDependencyOwnerDescription(Dependency: TPkgDependency; out Description: string); @@ -109,6 +115,7 @@ type procedure LoadMiscellaneousOptions; procedure SetupOutputFilter; procedure SetupMacros; + procedure SetupCodetools; procedure SetupPackageSystem; procedure SetupDialogs; function RepairedCheckOptions(Const ShortOptions : String; @@ -207,6 +214,35 @@ begin OutputFilter:=TheOutputFilter; end; +procedure TLazBuildApplication.OnCodeBufferEncodeSaving(Code: TCodeBuffer; + const Filename: string; var Source: string); +begin + if (Code.DiskEncoding<>'') and (Code.MemEncoding<>'') + and (Code.DiskEncoding<>Code.MemEncoding) then begin + {$IFDEF VerboseIDEEncoding} + DebugLn(['TLazBuildApplication.OnCodeBufferEncodeSaving Filename=',Code.Filename,' Mem=',Code.MemEncoding,' to Disk=',Code.DiskEncoding]); + {$ENDIF} + Source:=ConvertEncoding(Source,Code.MemEncoding,Code.DiskEncoding); + end; +end; + +procedure TLazBuildApplication.OnCodeBufferDecodeLoaded(Code: TCodeBuffer; + const Filename: string; var Source, DiskEncoding, MemEncoding: string); +begin + //DebugLn(['TLazBuildApplication.OnCodeBufferDecodeLoaded Filename=',Filename,' Encoding=',GuessEncoding(Source)]); + DiskEncoding:=''; + if DiskEncoding='' then + DiskEncoding:=GuessEncoding(Source); + MemEncoding:=EncodingUTF8; + if (DiskEncoding<>MemEncoding) then begin + {$IFDEF VerboseIDEEncoding} + DebugLn(['TLazBuildApplication.OnCodeBufferDecodeLoaded Filename=',Filename,' Disk=',DiskEncoding,' to Mem=',MemEncoding]); + {$ENDIF} + Source:=ConvertEncoding(Source,DiskEncoding,MemEncoding); + //DebugLn(['TLazBuildApplication.OnCodeBufferDecodeLoaded ',Source]); + end; +end; + procedure TLazBuildApplication.GetDependencyOwnerDescription( Dependency: TPkgDependency; out Description: string); begin @@ -764,8 +800,7 @@ begin LoadMiscellaneousOptions; SetupMacros; SetupLazarusDirectory; - // create a test unit needed to get from the compiler all macros and search paths - CodeToolBoss.FPCDefinesCache.TestFilename:=CreateCompilerTestPascalFilename; + SetupCodetools; SetupCompilerFilename; SetupPackageSystem; SetupOutputFilter; @@ -821,6 +856,15 @@ begin MainBuildBoss.SetupTransferMacros; end; +procedure TLazBuildApplication.SetupCodetools; +begin + // create a test unit needed to get from the compiler all macros and search paths + CodeToolBoss.FPCDefinesCache.TestFilename:=CreateCompilerTestPascalFilename; + CodeToolBoss.SourceCache.OnEncodeSaving:=@OnCodeBufferEncodeSaving; + CodeToolBoss.SourceCache.OnDecodeLoaded:=@OnCodeBufferDecodeLoaded; + CodeToolBoss.SourceCache.DefaultEncoding:=EncodingUTF8; +end; + procedure TLazBuildApplication.SetupPackageSystem; begin OnGetDependencyOwnerDescription:=@GetDependencyOwnerDescription; diff --git a/packager/packagedefs.pas b/packager/packagedefs.pas index e6d0f480a4..3309832f65 100644 --- a/packager/packagedefs.pas +++ b/packager/packagedefs.pas @@ -635,9 +635,9 @@ type procedure SetPackageType(const AValue: TLazPackageType); procedure SetStorePathDelim(const AValue: TPathDelimSwitch); procedure SetUserReadOnly(const AValue: boolean); - procedure OnMacroListSubstitution(TheMacro: TTransferMacro; + procedure OnMacroListSubstitution({%H-}TheMacro: TTransferMacro; const MacroName: string; var s: string; - const Data: PtrInt; var Handled, Abort: boolean; Depth: integer); + const Data: PtrInt; var Handled, {%H-}Abort: boolean; {%H-}Depth: integer); procedure Clear; procedure UpdateSourceDirectories; procedure SourceDirectoriesChanged(Sender: TObject);