2
#include "Math/MathUtility.h"
6
#define COPYBLOCKSIZE 32768
9
int NFileManagerGeneric::Copy(const TCHAR* InDestFile, const TCHAR* InSrcFile, bool OverWriteExisting, bool OverWriteReadOnly, NFileTransferMonitor* Monitor)
11
// Direct file copier.
12
if(Monitor && !Monitor->Progress(0.0))
17
NString SrcFile = InSrcFile;
18
NString DestFile = InDestFile;
20
NSerializer* Src = CreateFileReader(SrcFile.GetTCharPtr());
23
Result = COPY_ReadFail;
27
t_u32 Size = Src->GetFileSize();
28
NSerializer* Dest = CreateFileWriter(DestFile.GetTCharPtr(), (OverWriteExisting?0:FILEWRITE_NoReplaceExisting) | (OverWriteReadOnly?FILEWRITE_EvenIfReadOnly:0));
31
Result = COPY_WriteFail;
35
t_u32 Percent=0, NewPercent=0;
36
BYTE Buffer[COPYBLOCKSIZE];
37
for(t_u32 Total=0; Total<Size; Total+=sizeof(Buffer))
39
t_u32 Count = Min<t_u32>(Size-Total, (t_u32)sizeof(Buffer));
40
Src->Serialize(Buffer, Count);
43
Result = COPY_ReadFail;
46
Dest->Serialize(Buffer, Count);
49
Result = COPY_WriteFail;
52
NewPercent = Total * 100 / Size;
53
if(Monitor && Percent != NewPercent && !Monitor->Progress((float)NewPercent / 100.f))
55
Result = COPY_Canceled;
64
Result = COPY_WriteFail;
70
Delete(DestFile.GetTCharPtr());
77
Result = COPY_ReadFail;
82
if(Monitor && Result==COPY_OK && !Monitor->Progress(1.0))
84
Result = COPY_Canceled;
89
bool NFileManagerGeneric::IsDrive(const TCHAR* Path)
91
// Does Path refer to a drive letter or UNC path?
92
// A UNC is a naming convention that permits you to use a network resource,
93
// such as a network server, without formally connecting to the network resource
94
// with a mapped drive. A UNC path uses the following syntax:
96
// The share is a drive: D:\Folder of ServerA = "\\ServerA\D\Folder"
98
if(Stricmp(Path,TEXT(""))==0)
100
else if((ToUpperCase(Path[0]) != ToLowerCase(Path[0])) && (Path[1] == TEXT(':')) && (Path[2] == 0)) // look for "a:", "c:", "d:" ...
102
else if(Stricmp(Path,TEXT("\\"))==0) // look for "\"
104
else if(Stricmp(Path,TEXT("\\\\"))==0) // look for "\\"
106
else if(Path[0]==TEXT('\\') && Path[1]==TEXT('\\') && !Strchr(Path+2,TEXT('\\'))) // look for "\\Server"
108
else if(Path[0]==TEXT('\\') && Path[1]==TEXT('\\') && Strchr(Path+2,TEXT('\\')) && !Strchr(Strchr(Path+2,TEXT('\\')) + 1, TEXT('\\')))
109
// look for "\\Server\share"
115
bool NFileManagerGeneric::MakeDirectory(const TCHAR* Path, bool CreateCompletePath)
117
// Support code for making a directory tree.
118
nuxAssert(CreateCompletePath);
119
t_u32 SlashCount=0, CreateCount=0;
120
for(TCHAR Full[256] = TEXT(""), *Ptr = Full; ; *Ptr++ = *Path++)
122
if((*Path == INL_BACKSLASH_CHAR) || (*Path == INL_SLASH_CHAR) || (*Path == 0))
124
if((SlashCount++ > 0) && !IsDrive(Full))
127
if(MakeDirectory(Full, 0) != INL_OK)
135
return CreateCount!=0;
138
bool NFileManagerGeneric::DeleteDirectory(const TCHAR* Path, bool DeleteContentFirst)
140
nuxAssert(DeleteContentFirst);
141
nuxAssert(Path != NULL);
143
t_size PathLength = StringLength(Path);
147
NString WildcardPath = NString(Path);
148
if((WildcardPath[PathLength - 1] != INL_BACKSLASH_CHAR) && (WildcardPath[PathLength - 1] != INL_SLASH_CHAR))
149
WildcardPath += INL_BACKSLASH_CHAR;
150
WildcardPath += TEXT("*");
152
std::vector<NString> List;
153
FindFiles(List, *WildcardPath, 1, 0);
154
for(t_u32 i = 0; i < List.size(); i++)
156
if(!Delete(*(NString(Path) + INL_BACKSLASH_CHAR + List[i]), 1))
160
FindFiles(List, *WildcardPath, 0, 1);
161
for(t_u32 i = 0; i < List.size(); i++)
163
if(!DeleteDirectory(*(NString(Path) + INL_BACKSLASH_CHAR + List[i]), true))
167
return DeleteDirectory(Path, false);
170
bool NFileManagerGeneric::Move(const TCHAR* Dest, const TCHAR* Src, bool OverWriteExisting, bool OverWriteReadOnly, NFileTransferMonitor* Monitor)
172
// Move file manually.
173
if(Copy(Dest, Src, OverWriteExisting, OverWriteReadOnly, NULL) != COPY_OK)
180
int NFileManagerGeneric::CreateUniqueFileName(const TCHAR* Filename, const TCHAR* Extension, NString& OutputFilename, unsigned int BaseIndex)
183
nuxAssert(Extension);
185
NString FullPath(Filename);
186
const t_size IndexMarker = FullPath.Length(); // Marks location of the four-digit index.
187
FullPath += TEXT("0000.");
188
FullPath += Extension;
190
// Iterate over indices, searching for a file that doesn't exist.
191
for(DWORD i = BaseIndex + 1 ; i < 10000 ; ++i)
193
FullPath[IndexMarker ] = i / 1000 + TEXT('0');
194
FullPath[IndexMarker+1] = (i/100) % 10 + TEXT('0');
195
FullPath[IndexMarker+2] = (i/10) % 10 + TEXT('0');
196
FullPath[IndexMarker+3] = i % 10 + TEXT('0');
198
if (GFileManager.FileSize(FullPath.GetTCharPtr()) == -1)
200
// The file doesn't exist; output success.
201
OutputFilename = FullPath;
202
return static_cast<int>(i);
206
// Can't find an empty filename slot with index in (StartVal, 9999].