lazbuild: convert encoding of loaded source files

git-svn-id: trunk@31363 -
This commit is contained in:
mattias 2011-06-24 18:38:00 +00:00
parent d13ac6fe8a
commit d3ebe6ba78
4 changed files with 74 additions and 16 deletions

View File

@ -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

View File

@ -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';

View File

@ -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;

View File

@ -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);