~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

Viewing changes to NuxCore/FileManager/NFileManagerGeneric.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-01 19:25:37 UTC
  • Revision ID: neil.patel@canonical.com-20100901192537-mfz7rm6q262pewg6
Import and build NuxCore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "NKernel.h"
 
2
#include "Math/MathUtility.h"
 
3
 
 
4
NAMESPACE_BEGIN
 
5
 
 
6
#define COPYBLOCKSIZE   32768
 
7
 
 
8
 
 
9
int NFileManagerGeneric::Copy(const TCHAR* InDestFile, const TCHAR* InSrcFile, bool OverWriteExisting, bool OverWriteReadOnly, NFileTransferMonitor* Monitor)
 
10
{
 
11
    // Direct file copier.
 
12
    if(Monitor && !Monitor->Progress(0.0))
 
13
    {
 
14
        return COPY_Canceled;
 
15
    }
 
16
    int     Result              = COPY_OK;
 
17
    NString SrcFile             = InSrcFile;
 
18
    NString DestFile    = InDestFile;
 
19
 
 
20
    NSerializer* Src = CreateFileReader(SrcFile.GetTCharPtr());
 
21
    if(!Src)
 
22
    {
 
23
        Result = COPY_ReadFail;
 
24
    }
 
25
    else
 
26
    {
 
27
        t_u32 Size = Src->GetFileSize();
 
28
        NSerializer* Dest = CreateFileWriter(DestFile.GetTCharPtr(), (OverWriteExisting?0:FILEWRITE_NoReplaceExisting) | (OverWriteReadOnly?FILEWRITE_EvenIfReadOnly:0));
 
29
        if(!Dest)
 
30
        {
 
31
            Result = COPY_WriteFail;
 
32
        }
 
33
        else
 
34
        {
 
35
            t_u32 Percent=0, NewPercent=0;
 
36
            BYTE Buffer[COPYBLOCKSIZE];
 
37
            for(t_u32 Total=0; Total<Size; Total+=sizeof(Buffer))
 
38
            {
 
39
                t_u32 Count = Min<t_u32>(Size-Total, (t_u32)sizeof(Buffer));
 
40
                Src->Serialize(Buffer, Count);
 
41
                if(Src->IsError())
 
42
                {
 
43
                    Result = COPY_ReadFail;
 
44
                    break;
 
45
                }
 
46
                Dest->Serialize(Buffer, Count);
 
47
                if(Dest->IsError())
 
48
                {
 
49
                    Result = COPY_WriteFail;
 
50
                    break;
 
51
                }
 
52
                NewPercent = Total * 100 / Size;
 
53
                if(Monitor && Percent != NewPercent && !Monitor->Progress((float)NewPercent / 100.f))
 
54
                {
 
55
                    Result = COPY_Canceled;
 
56
                    break;
 
57
                }
 
58
                Percent = NewPercent;
 
59
            }
 
60
            if(Result == COPY_OK)
 
61
            {
 
62
                if(!Dest->Close())
 
63
                {
 
64
                    Result = COPY_WriteFail;
 
65
                }
 
66
            }
 
67
            delete Dest;
 
68
            if(Result != COPY_OK)
 
69
            {
 
70
                Delete(DestFile.GetTCharPtr());
 
71
            }
 
72
        }
 
73
        if(Result == COPY_OK)
 
74
        {
 
75
            if(!Src->Close())
 
76
            {
 
77
                Result = COPY_ReadFail;
 
78
            }
 
79
        }
 
80
        delete Src;
 
81
    }
 
82
    if(Monitor && Result==COPY_OK && !Monitor->Progress(1.0))
 
83
    {
 
84
        Result = COPY_Canceled;
 
85
    }
 
86
    return Result;
 
87
}
 
88
 
 
89
bool NFileManagerGeneric::IsDrive(const TCHAR* Path)
 
90
{
 
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:
 
95
    //      \\<Server>\<Share> 
 
96
    // The share is a drive: D:\Folder of ServerA = "\\ServerA\D\Folder"
 
97
 
 
98
    if(Stricmp(Path,TEXT(""))==0)
 
99
        return 1;
 
100
    else if((ToUpperCase(Path[0]) != ToLowerCase(Path[0])) && (Path[1] == TEXT(':')) && (Path[2] == 0)) // look for "a:", "c:", "d:" ...
 
101
        return 1;
 
102
    else if(Stricmp(Path,TEXT("\\"))==0)   // look for "\"
 
103
        return 1;
 
104
    else if(Stricmp(Path,TEXT("\\\\"))==0) // look for "\\"
 
105
        return 1;
 
106
    else if(Path[0]==TEXT('\\') && Path[1]==TEXT('\\') && !Strchr(Path+2,TEXT('\\')))    // look for "\\Server"
 
107
        return 1;
 
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"
 
110
        return 1;
 
111
    else
 
112
        return 0;
 
113
}
 
114
 
 
115
bool NFileManagerGeneric::MakeDirectory(const TCHAR* Path, bool CreateCompletePath)
 
116
{
 
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++)
 
121
    {
 
122
        if((*Path == INL_BACKSLASH_CHAR) || (*Path == INL_SLASH_CHAR) || (*Path == 0))
 
123
        {
 
124
            if((SlashCount++ > 0) && !IsDrive(Full))
 
125
            {
 
126
                *Ptr = 0;
 
127
                if(MakeDirectory(Full, 0) != INL_OK)
 
128
                    return 0;
 
129
                CreateCount++;
 
130
            }
 
131
        }
 
132
        if(*Path == 0)
 
133
            break;
 
134
    }
 
135
    return CreateCount!=0;
 
136
}
 
137
 
 
138
bool NFileManagerGeneric::DeleteDirectory(const TCHAR* Path, bool DeleteContentFirst)
 
139
{
 
140
   nuxAssert(DeleteContentFirst);
 
141
   nuxAssert(Path != NULL);
 
142
 
 
143
   t_size PathLength = StringLength(Path);
 
144
   if(PathLength == 0)
 
145
       return false;
 
146
 
 
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("*");
 
151
 
 
152
   std::vector<NString> List;
 
153
   FindFiles(List, *WildcardPath, 1, 0);
 
154
   for(t_u32 i = 0; i < List.size(); i++)
 
155
   {
 
156
       if(!Delete(*(NString(Path) + INL_BACKSLASH_CHAR + List[i]), 1))
 
157
           return 0;
 
158
   }
 
159
   List.clear();
 
160
   FindFiles(List, *WildcardPath, 0, 1);
 
161
   for(t_u32 i = 0; i < List.size(); i++)
 
162
   {
 
163
       if(!DeleteDirectory(*(NString(Path) + INL_BACKSLASH_CHAR + List[i]), true))
 
164
           return 0;
 
165
   }
 
166
   List.clear();
 
167
   return DeleteDirectory(Path, false);
 
168
}
 
169
 
 
170
bool NFileManagerGeneric::Move(const TCHAR* Dest, const TCHAR* Src, bool OverWriteExisting, bool OverWriteReadOnly, NFileTransferMonitor* Monitor)
 
171
{
 
172
    // Move file manually.
 
173
    if(Copy(Dest, Src, OverWriteExisting, OverWriteReadOnly, NULL) != COPY_OK)
 
174
        return 0;
 
175
    Delete(Src, 1);
 
176
    return 1;
 
177
}
 
178
 
 
179
 
 
180
int NFileManagerGeneric::CreateUniqueFileName(const TCHAR* Filename, const TCHAR* Extension, NString& OutputFilename, unsigned int BaseIndex)
 
181
{
 
182
    nuxAssert(Filename);
 
183
    nuxAssert(Extension);
 
184
 
 
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;
 
189
 
 
190
    // Iterate over indices, searching for a file that doesn't exist.
 
191
    for(DWORD i = BaseIndex + 1 ; i < 10000 ; ++i)
 
192
    {
 
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');
 
197
 
 
198
        if (GFileManager.FileSize(FullPath.GetTCharPtr()) == -1)
 
199
        {
 
200
            // The file doesn't exist; output success.
 
201
            OutputFilename = FullPath;
 
202
            return static_cast<int>(i);
 
203
        }
 
204
    }
 
205
 
 
206
    // Can't find an empty filename slot with index in (StartVal, 9999].
 
207
    return -1;
 
208
}
 
209
 
 
210
NAMESPACE_END
 
211