~ubuntu-branches/ubuntu/oneiric/nux/oneiric

« back to all changes in this revision

Viewing changes to NuxCore/FileManager/NFileManagerGeneric.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-11-18 19:17:32 UTC
  • Revision ID: james.westby@ubuntu.com-20101118191732-rn35790vekj6o4my
Tags: upstream-0.9.4
ImportĀ upstreamĀ versionĀ 0.9.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2010 Inalogic Inc.
 
3
 *
 
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.
 
7
 *
 
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.
 
13
 *
 
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/>
 
17
 *
 
18
 * Authored by: Jay Taoko <jay.taoko_AT_gmail_DOT_com>
 
19
 *
 
20
 */
 
21
 
 
22
 
 
23
#include "NuxCore.h"
 
24
#include "Math/MathUtility.h"
 
25
 
 
26
namespace nux
 
27
{
 
28
 
 
29
#define COPYBLOCKSIZE   32768
 
30
 
 
31
 
 
32
  int NFileManagerGeneric::Copy (const TCHAR *InDestFile, const TCHAR *InSrcFile, bool OverWriteExisting, bool OverWriteReadOnly, NFileTransferMonitor *Monitor)
 
33
  {
 
34
    // Direct file copier.
 
35
    if (Monitor && !Monitor->Progress (0.0) )
 
36
    {
 
37
      return COPY_CANCELED;
 
38
    }
 
39
 
 
40
    int     Result              = COPY_OK;
 
41
    NString SrcFile             = InSrcFile;
 
42
    NString DestFile    = InDestFile;
 
43
 
 
44
    NSerializer *Src = CreateFileReader (SrcFile.GetTCharPtr() );
 
45
 
 
46
    if (!Src)
 
47
    {
 
48
      Result = COPY_READFAIL;
 
49
    }
 
50
    else
 
51
    {
 
52
      t_u32 Size = Src->GetFileSize();
 
53
      NSerializer *Dest = CreateFileWriter (DestFile.GetTCharPtr(), (OverWriteExisting ? 0 : FILEWRITE_NOREPLACEEXISTING) | (OverWriteReadOnly ? FILEWRITE_EVENIFREADONLY : 0) );
 
54
 
 
55
      if (!Dest)
 
56
      {
 
57
        Result = COPY_WRITEFAIL;
 
58
      }
 
59
      else
 
60
      {
 
61
        t_u32 Percent = 0, NewPercent = 0;
 
62
        BYTE Buffer[COPYBLOCKSIZE];
 
63
 
 
64
        for (t_u32 Total = 0; Total < Size; Total += sizeof (Buffer) )
 
65
        {
 
66
          t_u32 Count = Min<t_u32> (Size - Total, (t_u32) sizeof (Buffer) );
 
67
          Src->Serialize (Buffer, Count);
 
68
 
 
69
          if (Src->IsError() )
 
70
          {
 
71
            Result = COPY_READFAIL;
 
72
            break;
 
73
          }
 
74
 
 
75
          Dest->Serialize (Buffer, Count);
 
76
 
 
77
          if (Dest->IsError() )
 
78
          {
 
79
            Result = COPY_WRITEFAIL;
 
80
            break;
 
81
          }
 
82
 
 
83
          NewPercent = Total * 100 / Size;
 
84
 
 
85
          if (Monitor && Percent != NewPercent && !Monitor->Progress ( (float) NewPercent / 100.f) )
 
86
          {
 
87
            Result = COPY_CANCELED;
 
88
            break;
 
89
          }
 
90
 
 
91
          Percent = NewPercent;
 
92
        }
 
93
 
 
94
        if (Result == COPY_OK)
 
95
        {
 
96
          if (!Dest->Close() )
 
97
          {
 
98
            Result = COPY_WRITEFAIL;
 
99
          }
 
100
        }
 
101
 
 
102
        delete Dest;
 
103
 
 
104
        if (Result != COPY_OK)
 
105
        {
 
106
          Delete (DestFile.GetTCharPtr() );
 
107
        }
 
108
      }
 
109
 
 
110
      if (Result == COPY_OK)
 
111
      {
 
112
        if (!Src->Close() )
 
113
        {
 
114
          Result = COPY_READFAIL;
 
115
        }
 
116
      }
 
117
 
 
118
      delete Src;
 
119
    }
 
120
 
 
121
    if (Monitor && Result == COPY_OK && !Monitor->Progress (1.0) )
 
122
    {
 
123
      Result = COPY_CANCELED;
 
124
    }
 
125
 
 
126
    return Result;
 
127
  }
 
128
 
 
129
  bool NFileManagerGeneric::IsDrive (const TCHAR *Path)
 
130
  {
 
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"
 
137
 
 
138
    if (Stricmp (Path, TEXT ("") ) == 0)
 
139
      return 1;
 
140
    else if ( (ToUpperCase (Path[0]) != ToLowerCase (Path[0]) ) && (Path[1] == TEXT (':') ) && (Path[2] == 0) ) // look for "a:", "c:", "d:" ...
 
141
      return 1;
 
142
    else if (Stricmp (Path, TEXT ("\\") ) == 0) // look for "\"
 
143
      return 1;
 
144
    else if (Stricmp (Path, TEXT ("\\\\") ) == 0) // look for "\\"
 
145
      return 1;
 
146
    else if (Path[0] == TEXT ('\\') && Path[1] == TEXT ('\\') && !Strchr (Path + 2, TEXT ('\\') ) ) // look for "\\Server"
 
147
      return 1;
 
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"
 
150
      return 1;
 
151
    else
 
152
      return 0;
 
153
  }
 
154
 
 
155
  bool NFileManagerGeneric::MakeDirectory (const TCHAR *Path, bool CreateCompletePath)
 
156
  {
 
157
    // Support code for making a directory tree.
 
158
    nuxAssert (CreateCompletePath);
 
159
    t_u32 SlashCount = 0, CreateCount = 0;
 
160
 
 
161
    for (TCHAR Full[256] = TEXT (""), *Ptr = Full; ; *Ptr++ = *Path++)
 
162
    {
 
163
      if ( (*Path == NUX_BACKSLASH_CHAR) || (*Path == NUX_SLASH_CHAR) || (*Path == 0) )
 
164
      {
 
165
        if ( (SlashCount++ > 0) && !IsDrive (Full) )
 
166
        {
 
167
          *Ptr = 0;
 
168
 
 
169
          if (MakeDirectory (Full, 0) != NUX_OK)
 
170
            return 0;
 
171
 
 
172
          CreateCount++;
 
173
        }
 
174
      }
 
175
 
 
176
      if (*Path == 0)
 
177
        break;
 
178
    }
 
179
 
 
180
    return CreateCount != 0;
 
181
  }
 
182
 
 
183
  bool NFileManagerGeneric::DeleteDirectory (const TCHAR *Path, bool DeleteContentFirst)
 
184
  {
 
185
    nuxAssert (DeleteContentFirst);
 
186
    nuxAssert (Path != NULL);
 
187
 
 
188
    t_size PathLength = StringLength (Path);
 
189
 
 
190
    if (PathLength == 0)
 
191
      return false;
 
192
 
 
193
    NString WildcardPath = NString (Path);
 
194
 
 
195
    if ( (WildcardPath[PathLength - 1] != NUX_BACKSLASH_CHAR) && (WildcardPath[PathLength - 1] != NUX_SLASH_CHAR) )
 
196
      WildcardPath += NUX_BACKSLASH_CHAR;
 
197
 
 
198
    WildcardPath += TEXT ("*");
 
199
 
 
200
    std::vector<NString> List;
 
201
    FindFiles (List, *WildcardPath, 1, 0);
 
202
 
 
203
    for (t_u32 i = 0; i < List.size(); i++)
 
204
    {
 
205
      if (!Delete (* (NString (Path) + NUX_BACKSLASH_CHAR + List[i]), 1) )
 
206
        return 0;
 
207
    }
 
208
 
 
209
    List.clear();
 
210
    FindFiles (List, *WildcardPath, 0, 1);
 
211
 
 
212
    for (t_u32 i = 0; i < List.size(); i++)
 
213
    {
 
214
      if (!DeleteDirectory (* (NString (Path) + NUX_BACKSLASH_CHAR + List[i]), true) )
 
215
        return 0;
 
216
    }
 
217
 
 
218
    List.clear();
 
219
    return DeleteDirectory (Path, false);
 
220
  }
 
221
 
 
222
  bool NFileManagerGeneric::Move (const TCHAR *Dest, const TCHAR *Src, bool OverWriteExisting, bool OverWriteReadOnly, NFileTransferMonitor *Monitor)
 
223
  {
 
224
    // Move file manually.
 
225
    if (Copy (Dest, Src, OverWriteExisting, OverWriteReadOnly, NULL) != COPY_OK)
 
226
      return 0;
 
227
 
 
228
    Delete (Src, 1);
 
229
    return 1;
 
230
  }
 
231
 
 
232
 
 
233
  int NFileManagerGeneric::CreateUniqueFileName (const TCHAR *Filename, const TCHAR *Extension, NString &OutputFilename, unsigned int BaseIndex)
 
234
  {
 
235
    nuxAssert (Filename);
 
236
    nuxAssert (Extension);
 
237
 
 
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;
 
242
 
 
243
    // Iterate over indices, searching for a file that doesn't exist.
 
244
    for (DWORD i = BaseIndex + 1 ; i < 10000 ; ++i)
 
245
    {
 
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');
 
250
 
 
251
      if (GFileManager.FileSize (FullPath.GetTCharPtr() ) == -1)
 
252
      {
 
253
        // The file doesn't exist; output success.
 
254
        OutputFilename = FullPath;
 
255
        return static_cast<int> (i);
 
256
      }
 
257
    }
 
258
 
 
259
    // Can't find an empty filename slot with index in (StartVal, 9999].
 
260
    return -1;
 
261
  }
 
262
 
 
263
}
 
264