From 3c3a4aa9118e2b7c707505cc62f7b3eddc250618 Mon Sep 17 00:00:00 2001 From: tobigun Date: Wed, 20 Aug 2008 23:55:05 +0000 Subject: TPlatform.CopyFile added git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/trunk@1281 b956fd51-792f-4845-bead-9b4dfca2ff2c --- Game/Code/Classes/UPlatform.pas | 63 +++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 9 deletions(-) (limited to 'Game/Code/Classes/UPlatform.pas') diff --git a/Game/Code/Classes/UPlatform.pas b/Game/Code/Classes/UPlatform.pas index 5d197937..b71ac1b8 100644 --- a/Game/Code/Classes/UPlatform.pas +++ b/Game/Code/Classes/UPlatform.pas @@ -26,13 +26,14 @@ type TPlatform = class procedure Init; virtual; - function DirectoryFindFiles(Dir, Filter: WideString; ReturnAllSubDirs: boolean): TDirectoryEntryArray; virtual; abstract; - function TerminateIfAlreadyRunning(var WndTitle : string): boolean; virtual; - function FindSongFile(Dir, Mask: WideString): WideString; virtual; + function DirectoryFindFiles(Dir, Filter: WideString; ReturnAllSubDirs: boolean): TDirectoryEntryArray; virtual; abstract; + function TerminateIfAlreadyRunning(var WndTitle : string): boolean; virtual; + function FindSongFile(Dir, Mask: WideString): WideString; virtual; procedure Halt; virtual; - function GetLogPath : WideString; virtual; abstract; - function GetGameSharedPath : WideString; virtual; abstract; - function GetGameUserPath : WideString; virtual; abstract; + function GetLogPath : WideString; virtual; abstract; + function GetGameSharedPath : WideString; virtual; abstract; + function GetGameUserPath : WideString; virtual; abstract; + function CopyFile(const Source, Target: WideString; FailIfExists: boolean): boolean; virtual; end; function Platform(): TPlatform; @@ -42,14 +43,15 @@ implementation uses SysUtils, {$IFDEF MSWINDOWS} - UPlatformWindows; + UPlatformWindows, {$ENDIF} {$IFDEF LINUX} - UPlatformLinux; + UPlatformLinux, {$ENDIF} {$IFDEF DARWIN} - UPlatformMacOSX; + UPlatformMacOSX, {$ENDIF} + ULog; // I have modified it to use the Platform_singleton in this location ( in the implementaiton ) @@ -102,6 +104,49 @@ begin SysUtils.FindClose(SR); end; +function TPlatform.CopyFile(const Source, Target: WideString; FailIfExists: boolean): boolean; +const + COPY_BUFFER_SIZE = 4096; // a good tradeoff between speed and memory consumption +var + SourceFile, TargetFile: TFileStream; + FileCopyBuffer: array [0..COPY_BUFFER_SIZE-1] of byte; // temporary copy-buffer. + NumberOfBytes: integer; // number of bytes read from SourceFile +begin + Result := false; + SourceFile := nil; + TargetFile := nil; + + // if overwrite is disabled return if the target file already exists + if (FailIfExists and FileExists(Target)) then + Exit; + + try + try + // open source and target file (might throw an exception on error) + SourceFile := TFileStream.Create(Source, fmOpenRead); + TargetFile := TFileStream.Create(Target, fmCreate or fmOpenWrite); + + while true do + begin + // read a block from the source file and check for errors or EOF + NumberOfBytes := SourceFile.Read(FileCopyBuffer, SizeOf(FileCopyBuffer)); + if (NumberOfBytes <= 0) then + Break; + // write block to target file and check if everything was written + if (TargetFile.Write(FileCopyBuffer, NumberOfBytes) <> NumberOfBytes) then + Exit; + end; + except + Exit; + end; + finally + SourceFile.Free; + TargetFile.Free; + end; + + Result := true; +end; + initialization {$IFDEF MSWINDOWS} -- cgit v1.2.3