2
* Copyright 2010 Inalogic Inc.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License version 3, as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the applicable version of the GNU Lesser General Public
12
* License for more details.
14
* You should have received a copy of both the GNU Lesser General Public
15
* License version 3 along with this program. If not, see
16
* <http://www.gnu.org/licenses/>
18
* Authored by: Jay Taoko <jay.taoko_AT_gmail_DOT_com>
24
#include "Math/MathUtility.h"
29
#define COPYBLOCKSIZE 32768
32
int NFileManagerGeneric::Copy (const TCHAR *InDestFile, const TCHAR *InSrcFile, bool OverWriteExisting, bool OverWriteReadOnly, NFileTransferMonitor *Monitor)
34
// Direct file copier.
35
if (Monitor && !Monitor->Progress (0.0) )
41
NString SrcFile = InSrcFile;
42
NString DestFile = InDestFile;
44
NSerializer *Src = CreateFileReader (SrcFile.GetTCharPtr() );
48
Result = COPY_READFAIL;
52
t_u32 Size = Src->GetFileSize();
53
NSerializer *Dest = CreateFileWriter (DestFile.GetTCharPtr(), (OverWriteExisting ? 0 : FILEWRITE_NOREPLACEEXISTING) | (OverWriteReadOnly ? FILEWRITE_EVENIFREADONLY : 0) );
57
Result = COPY_WRITEFAIL;
61
t_u32 Percent = 0, NewPercent = 0;
62
BYTE Buffer[COPYBLOCKSIZE];
64
for (t_u32 Total = 0; Total < Size; Total += sizeof (Buffer) )
66
t_u32 Count = Min<t_u32> (Size - Total, (t_u32) sizeof (Buffer) );
67
Src->Serialize (Buffer, Count);
71
Result = COPY_READFAIL;
75
Dest->Serialize (Buffer, Count);
79
Result = COPY_WRITEFAIL;
83
NewPercent = Total * 100 / Size;
85
if (Monitor && Percent != NewPercent && !Monitor->Progress ( (float) NewPercent / 100.f) )
87
Result = COPY_CANCELED;
94
if (Result == COPY_OK)
98
Result = COPY_WRITEFAIL;
104
if (Result != COPY_OK)
106
Delete (DestFile.GetTCharPtr() );
110
if (Result == COPY_OK)
114
Result = COPY_READFAIL;
121
if (Monitor && Result == COPY_OK && !Monitor->Progress (1.0) )
123
Result = COPY_CANCELED;
129
bool NFileManagerGeneric::IsDrive (const TCHAR *Path)
131
// Does Path refer to a drive letter or UNC path?
132
// A UNC is a naming convention that permits you to use a network resource,
133
// such as a network server, without formally connecting to the network resource
134
// with a mapped drive. A UNC path uses the following syntax:
135
// \\<Server>\<Share>
136
// The share is a drive: D:\Folder of ServerA = "\\ServerA\D\Folder"
138
if (Stricmp (Path, TEXT ("") ) == 0)
140
else if ( (ToUpperCase (Path[0]) != ToLowerCase (Path[0]) ) && (Path[1] == TEXT (':') ) && (Path[2] == 0) ) // look for "a:", "c:", "d:" ...
142
else if (Stricmp (Path, TEXT ("\\") ) == 0) // look for "\"
144
else if (Stricmp (Path, TEXT ("\\\\") ) == 0) // look for "\\"
146
else if (Path[0] == TEXT ('\\') && Path[1] == TEXT ('\\') && !Strchr (Path + 2, TEXT ('\\') ) ) // look for "\\Server"
148
else if (Path[0] == TEXT ('\\') && Path[1] == TEXT ('\\') && Strchr (Path + 2, TEXT ('\\') ) && !Strchr (Strchr (Path + 2, TEXT ('\\') ) + 1, TEXT ('\\') ) )
149
// look for "\\Server\share"
155
bool NFileManagerGeneric::MakeDirectory (const TCHAR *Path, bool CreateCompletePath)
157
// Support code for making a directory tree.
158
nuxAssert (CreateCompletePath);
159
t_u32 SlashCount = 0, CreateCount = 0;
161
for (TCHAR Full[256] = TEXT (""), *Ptr = Full; ; *Ptr++ = *Path++)
163
if ( (*Path == NUX_BACKSLASH_CHAR) || (*Path == NUX_SLASH_CHAR) || (*Path == 0) )
165
if ( (SlashCount++ > 0) && !IsDrive (Full) )
169
if (MakeDirectory (Full, 0) != NUX_OK)
180
return CreateCount != 0;
183
bool NFileManagerGeneric::DeleteDirectory (const TCHAR *Path, bool DeleteContentFirst)
185
nuxAssert (DeleteContentFirst);
186
nuxAssert (Path != NULL);
188
t_size PathLength = StringLength (Path);
193
NString WildcardPath = NString (Path);
195
if ( (WildcardPath[PathLength - 1] != NUX_BACKSLASH_CHAR) && (WildcardPath[PathLength - 1] != NUX_SLASH_CHAR) )
196
WildcardPath += NUX_BACKSLASH_CHAR;
198
WildcardPath += TEXT ("*");
200
std::vector<NString> List;
201
FindFiles (List, *WildcardPath, 1, 0);
203
for (t_u32 i = 0; i < List.size(); i++)
205
if (!Delete (* (NString (Path) + NUX_BACKSLASH_CHAR + List[i]), 1) )
210
FindFiles (List, *WildcardPath, 0, 1);
212
for (t_u32 i = 0; i < List.size(); i++)
214
if (!DeleteDirectory (* (NString (Path) + NUX_BACKSLASH_CHAR + List[i]), true) )
219
return DeleteDirectory (Path, false);
222
bool NFileManagerGeneric::Move (const TCHAR *Dest, const TCHAR *Src, bool OverWriteExisting, bool OverWriteReadOnly, NFileTransferMonitor *Monitor)
224
// Move file manually.
225
if (Copy (Dest, Src, OverWriteExisting, OverWriteReadOnly, NULL) != COPY_OK)
233
int NFileManagerGeneric::CreateUniqueFileName (const TCHAR *Filename, const TCHAR *Extension, NString &OutputFilename, unsigned int BaseIndex)
235
nuxAssert (Filename);
236
nuxAssert (Extension);
238
NString FullPath (Filename);
239
const t_size IndexMarker = FullPath.Length(); // Marks location of the four-digit index.
240
FullPath += TEXT ("0000.");
241
FullPath += Extension;
243
// Iterate over indices, searching for a file that doesn't exist.
244
for (DWORD i = BaseIndex + 1 ; i < 10000 ; ++i)
246
FullPath[IndexMarker ] = i / 1000 + TEXT ('0');
247
FullPath[IndexMarker+1] = (i / 100) % 10 + TEXT ('0');
248
FullPath[IndexMarker+2] = (i / 10) % 10 + TEXT ('0');
249
FullPath[IndexMarker+3] = i % 10 + TEXT ('0');
251
if (GFileManager.FileSize (FullPath.GetTCharPtr() ) == -1)
253
// The file doesn't exist; output success.
254
OutputFilename = FullPath;
255
return static_cast<int> (i);
259
// Can't find an empty filename slot with index in (StartVal, 9999].