forked from ldr123/update_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGFileUtility.cpp
356 lines (308 loc) · 7.69 KB
/
GFileUtility.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//
// GFileUtility.cpp
// Cocos2dXTest
//
// Created by ldr123 on 12-5-7.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#include "GFileUtility.h"
#include "cocos2d.h"
#include "GUtility.h"
#include <algorithm>
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#endif
using namespace std;
using namespace cocos2d;
bool GFileUtility::IsFileExist(const std::string &fileFullPath)
{
string strFile = CCFileUtils::sharedFileUtils()->fullPathForFilename(fileFullPath.c_str());
if (CCFileUtils::sharedFileUtils()->isFileExist(strFile.c_str()))
{
return true;
}
else
{
// CCLog("[GFileUtility::IsFileExist] file(%s) don't exist!",fileFullPath.c_str());
return false;
}
}
bool GFileUtility::FixDirectorySuffix(std::string &path)
{
int nSize = path.size();
if (nSize == 0)
{
return false;
}
std::replace(path.begin(), path.end(), '\\', '/');
char c = path[nSize - 1];
if (c != '/' && c != '\\')
{
path += "/";
}
return true;
}
bool GFileUtility::GetFileNameFromFilePath(const std::string& filePath,std::string& fileName)
{
if (filePath.empty())
{
return false;
}
size_t pathSeperatorPos = filePath.rfind('/');
fileName = filePath.substr(pathSeperatorPos+1);
return true;
}
bool GFileUtility::IsFileExistInZip(const std::string& fileFullPath)
{
GCCFileData data(fileFullPath.c_str(), "rt");
unsigned long size = data.getSize();
if (size > 0)
{
return true;
}
return false;
}
bool GFileUtility::RenameFileName(const std::string &fileOldFullPath, const std::string &fileNewFullPath)
{
//int rename(char *oldname, char *newname);
if ( rename(fileOldFullPath.c_str(), fileNewFullPath.c_str()) == 0 )
{
return true;
}
else
{
return false;
}
}
bool GFileUtility::DeletePath(const std::string& _strFullPath)
{
string strFullPath = _strFullPath;
FixDirectorySuffix(strFullPath);
#ifdef _WIN32
_finddata_t fileInfo;
string strFind = strFullPath + "*";
long nHandle = _findfirst(strFind.c_str(), &fileInfo);
if (nHandle != -1)
{
do
{
if (fileInfo.attrib & _A_SUBDIR)
{
if ((strcmp(fileInfo.name, ".") != 0) && (strcmp(fileInfo.name, "..") != 0))
{
DeletePath(strFullPath + fileInfo.name);
}
}
else
{
string filename = (strFullPath + fileInfo.name);
DELETE_FILE(filename.c_str());
}
} while (_findnext(nHandle, &fileInfo) == 0);
_findclose(nHandle);
}
#else
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if ((dp = opendir(strFullPath.c_str())) == nullptr)
{
return false;
}
chdir(strFullPath.c_str());
while ((entry = readdir(dp)) != nullptr)
{
lstat(entry->d_name, &statbuf);
if (S_ISDIR(statbuf.st_mode))
{
if (strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0)
continue;
DeletePath(entry->d_name);
}
else
{
string filename = entry->d_name;
DELETE_FILE(filename.c_str());
}
}
chdir("..");
closedir(dp);
#endif
REMOVE_DIRECTORY(strFullPath.c_str());
return true;
}
bool GFileUtility::DeleteFile(const std::string &fileFullPath)
{
return DELETE_FILE(fileFullPath.c_str());
}
bool GFileUtility::CopyFile(const std::string& fileOldFullPath, const std::string& fileNewFullPath)
{
if( !GFileUtility::IsFileExist(fileOldFullPath) )
{
CCLog("[GFileUtility] Error:(CopyFile) file(%s) don't exist!",fileOldFullPath.c_str());
return false;
}
FILE* source_file = fopen(fileOldFullPath.c_str(), "rb");
if (source_file == nullptr)
{
CCLog("[GFileUtility] Error:(CopyFile) open file(%s) failed!",fileOldFullPath.c_str());
return false;
}
if (!GFileUtility::CreateDirectoryFromFilePath(fileNewFullPath))
{
CCLog("[GFileUtility] Error:(CopyFile) create directory for file(%s) failed!",fileNewFullPath.c_str());
return false;
}
fseek(source_file, 0, SEEK_END);
int file_length = (int)ftell(source_file);
fseek(source_file, 0, SEEK_SET);
unsigned char *buf = new unsigned char[file_length];
fread(buf, 1, file_length, source_file);
fclose(source_file);
FILE* destination_file = fopen(fileNewFullPath.c_str(), "wb");
if (destination_file)
{
fwrite(buf, 1, file_length, destination_file);
fclose(destination_file);
}
else
{
delete []buf;
CCLog("[GFileUtility] Error:(CopyFile) open file(%s) failed!",fileNewFullPath.c_str());
return false;
}
delete []buf;
return true;
}
bool GFileUtility::CopyFileFromZip(const std::string& fileOldFullPath, const std::string& fileNewFullPath)
{
GCCFileData data(fileOldFullPath.c_str(), "rt");
unsigned long size = data.getSize();
char *pBuffer = (char*) data.getBuffer();
FILE* new_file = fopen(fileNewFullPath.c_str(), "w");
if (new_file == nullptr)
{
return false;
}
if(size != fwrite(pBuffer, 1, size, new_file) )
{
fclose(new_file);
return false;
}
fclose(new_file);
return true;
}
// e.g:
// input: fileFullPath = "Document/TempDirLevelOne/TempDirLevelTwo/test"
// output: makedir(Document/TempDirLevelOne) makedir(Document/TempDirLevelOne/TempDirLevelTwo)
bool GFileUtility::CreateDirectory(const std::string &_fileFullPath)
{
string fileFullPath = _fileFullPath;
if (!FixDirectorySuffix(fileFullPath))
{
return false;
}
string strTmp = "";
for (int i = 0; i < fileFullPath.size(); ++i)
{
strTmp += fileFullPath[i];
if (fileFullPath[i] == '/')
{
if (!IsFileExist(strTmp))
{
CREATE_DIRECTORY(strTmp.c_str());
}
}
}
return true;
}
bool GFileUtility::CreateDirectoryFromFilePath(const std::string& file)
{
int nSize = file.size();
if (nSize == 0)
{
return false;
}
string fileFullPath = file;
std::replace(fileFullPath.begin(), fileFullPath.end(), '\\', '/');
size_t nPos = fileFullPath.rfind('/');
if (nPos == string::npos)
{
return false;
}
fileFullPath = file.substr(0, nPos);
return CreateDirectory(fileFullPath);
}
bool GFileUtility::CopyDirectory(const std::string &from, const std::string &to)
{
return _CopyDirectory(from, from, to);
}
bool GFileUtility::_CopyDirectory(const string &_fromStart, const string &_from, const string &_to)
{
string strFromStart = _fromStart;
FixDirectorySuffix(strFromStart);
string strFrom = _from;
FixDirectorySuffix(strFrom);
string strTo = _to;
FixDirectorySuffix(strTo);
#ifdef _WIN32
_finddata_t fileInfo;
string strFind = strFrom + "*";
long nHandle = _findfirst(strFind.c_str(), &fileInfo);
if (nHandle != -1)
{
do
{
if (fileInfo.attrib & _A_SUBDIR)
{
if ((strcmp(fileInfo.name, ".") != 0) && (strcmp(fileInfo.name, "..") != 0))
{
return _CopyDirectory(_fromStart, strFrom + fileInfo.name, strTo);
}
}
else
{
string filename = (strFrom + fileInfo.name);
string strRelative = filename;
strRelative = strRelative.substr(_fromStart.size());
CopyFile(filename, strTo + strRelative);
}
} while (_findnext(nHandle, &fileInfo) == 0);
_findclose(nHandle);
}
#else
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if ((dp = opendir(_from.c_str())) == nullptr)
{
return false;
}
chdir(_from.c_str());
while ((entry = readdir(dp)) != nullptr)
{
lstat(entry->d_name, &statbuf);
if (S_ISDIR(statbuf.st_mode))
{
if (strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0)
continue;
return _CopyDirectory(_fromStart, entry->d_name, strTo);
}
else
{
string filename = entry->d_name;
string strRelative = filename;
strRelative = strRelative.substr(_fromStart.size());
CopyFile(filename, strTo + strRelative);
}
}
chdir("..");
closedir(dp);
#endif
return true;
}