lcl: TXMLConfigStorage: fixed load/save utf8 filename

git-svn-id: trunk@41834 -
This commit is contained in:
mattias 2013-06-23 16:02:26 +00:00
parent 7003a719ad
commit d39b418746

View File

@ -16,7 +16,7 @@ interface
uses uses
Classes, SysUtils, FileUtil, LCLProc, Forms, PropertyStorage, XMLConf, DOM, Classes, SysUtils, FileUtil, LCLProc, Forms, PropertyStorage, XMLConf, DOM,
XMLRead, XMLWrite, LazConfigStorage; XMLRead, XMLWrite, LazConfigStorage, lazutf8classes;
type type
{ TPropStorageXMLConfig } { TPropStorageXMLConfig }
@ -71,6 +71,7 @@ type
TXMLConfigStorage = class(TConfigStorage) TXMLConfigStorage = class(TConfigStorage)
private private
FFilename: string;
FFreeXMLConfig: boolean; FFreeXMLConfig: boolean;
FXMLConfig: TXMLConfig; FXMLConfig: TXMLConfig;
protected protected
@ -299,16 +300,35 @@ end;
constructor TXMLConfigStorage.Create(const Filename: string; constructor TXMLConfigStorage.Create(const Filename: string;
LoadFromDisk: Boolean); LoadFromDisk: Boolean);
var
ms: TMemoryStream;
fs: TFileStreamUTF8;
begin begin
FXMLConfig:=TPropStorageXMLConfig.Create(nil); FXMLConfig:=TPropStorageXMLConfig.Create(nil);
FXMLConfig.StartEmpty:=not LoadFromDisk; FFilename:=Filename;
FXMLConfig.Filename:=Filename;
FFreeXMLConfig:=true; FFreeXMLConfig:=true;
if LoadFromDisk then
begin
fs:=TFileStreamUTF8.Create(Filename,fmOpenRead+fmShareDenyWrite);
try
ms:=TMemoryStream.Create;
try
ms.CopyFrom(fs,fs.Size);
ms.Position:=0;
TPropStorageXMLConfig(FXMLConfig).LoadFromStream(ms);
finally
ms.Free;
end;
finally
fs.Free;
end;
end;
end; end;
constructor TXMLConfigStorage.Create(TheXMLConfig: TXMLConfig); constructor TXMLConfigStorage.Create(TheXMLConfig: TXMLConfig);
begin begin
FXMLConfig:=TheXMLConfig; FXMLConfig:=TheXMLConfig;
FFilename:=FXMLConfig.Filename;
if FXMLConfig=nil then if FXMLConfig=nil then
raise Exception.Create(''); raise Exception.Create('');
end; end;
@ -336,13 +356,32 @@ begin
end; end;
procedure TXMLConfigStorage.WriteToDisk; procedure TXMLConfigStorage.WriteToDisk;
var
ms: TMemoryStream;
fs: TFileStreamUTF8;
begin begin
if FXMLConfig is TPropStorageXMLConfig then
begin
ms:=TMemoryStream.Create;
try
TPropStorageXMLConfig(FXMLConfig).SaveToStream(ms);
ms.Position:=0;
fs:=TFileStreamUTF8.Create(GetFilename,fmCreate);
try
fs.CopyFrom(ms,ms.Size);
finally
fs.Free;
end;
finally
ms.Free;
end;
end else
FXMLConfig.Flush; FXMLConfig.Flush;
end; end;
function TXMLConfigStorage.GetFilename: string; function TXMLConfigStorage.GetFilename: string;
begin begin
Result:=FXMLConfig.Filename; Result:=FFilename;
end; end;
procedure TXMLConfigStorage.SaveToStream(s: TStream); procedure TXMLConfigStorage.SaveToStream(s: TStream);