aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortobigun <tobigun@b956fd51-792f-4845-bead-9b4dfca2ff2c>2008-08-20 23:55:05 +0000
committertobigun <tobigun@b956fd51-792f-4845-bead-9b4dfca2ff2c>2008-08-20 23:55:05 +0000
commit3c3a4aa9118e2b7c707505cc62f7b3eddc250618 (patch)
treec6448ed2ab1bc536d4f563b377070311ea670d7a
parent1495d0dab6dd7dd702d043c305760b8600d4609f (diff)
downloadusdx-3c3a4aa9118e2b7c707505cc62f7b3eddc250618.tar.gz
usdx-3c3a4aa9118e2b7c707505cc62f7b3eddc250618.tar.xz
usdx-3c3a4aa9118e2b7c707505cc62f7b3eddc250618.zip
TPlatform.CopyFile added
git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/trunk@1281 b956fd51-792f-4845-bead-9b4dfca2ff2c
Diffstat (limited to '')
-rw-r--r--Game/Code/Classes/UPlatform.pas63
-rw-r--r--Game/Code/Classes/UPlatformWindows.pas7
2 files changed, 61 insertions, 9 deletions
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}
diff --git a/Game/Code/Classes/UPlatformWindows.pas b/Game/Code/Classes/UPlatformWindows.pas
index 5c7b9655..ee132a7b 100644
--- a/Game/Code/Classes/UPlatformWindows.pas
+++ b/Game/Code/Classes/UPlatformWindows.pas
@@ -26,6 +26,8 @@ type
function GetLogPath: WideString; override;
function GetGameSharedPath: WideString; override;
function GetGameUserPath: WideString; override;
+
+ function CopyFile(const Source, Target: WideString; FailIfExists: boolean): boolean; override;
end;
implementation
@@ -226,4 +228,9 @@ begin
Result := ExtractFilePath(ParamStr(0));
end;
+function TPlatformWindows.CopyFile(const Source, Target: WideString; FailIfExists: boolean): boolean;
+begin
+ Result := Windows.CopyFileW(PWideChar(Source), PWideChar(Target), FailIfExists);
+end;
+
end.