-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Copy pathFileUtils.cpp
308 lines (259 loc) · 8.05 KB
/
FileUtils.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
#include "FileUtils.h"
#include "zip_support/unzip.h"
#include <string>
#include <cassert>
#define BREAK_IF(cond) if (cond) break;
namespace CocosDenshion {
static char s_ResourcePath[EOS_FILE_MAX_PATH] = {0};
static char s_ZipFilePath[EOS_FILE_MAX_PATH] = {0};
static char s_AppDataPath[EOS_FILE_MAX_PATH] = {0};
unsigned char* getFileDataFromZip(const char* pszZipFilePath, const char* pszFileName, unsigned long * pSize);
void fullPathFromRelativePath(const char *pszRelativePath, char* fullPath);
void updateZipFilePath(const char* pResPath);
void setZipFilePath(const char* pZipFileName);
const char* getDataPath();
bool FileUtils::isFileExisted(const char* pFilePath)
{
bool bRet = false;
if (strlen(s_ZipFilePath) != 0)
{
// if have set the zip file path,find the resource in the zip file
unzFile pZipFile = unzOpen(s_ZipFilePath);
do
{
BREAK_IF(!pZipFile);
Int32 nPos = unzLocateFile(pZipFile, pFilePath, 1);
BREAK_IF(nPos != UNZ_OK);
bRet = true;
unzClose(pZipFile);
} while (0);
}
else
{
char fullPath[EOS_FILE_MAX_PATH];
fullPathFromRelativePath(pFilePath, fullPath);
if (strlen(fullPath) > 0)
{
TUChar FilePath[EOS_FILE_MAX_PATH] = {0};
TUString::StrGBToUnicode(FilePath, (const Char *) fullPath);
// find in the hardware
if (EOS_IsFileExist(FilePath))
{
bRet = true;
}
}
}
return bRet;
}
void FileUtils::setResource(const char* pszResPath, const char* pszZipFileName)
{
if (pszResPath != NULL && pszZipFileName != NULL)
{
// record the resource path
strcpy(s_ResourcePath, pszResPath);
// record the zip file path
setZipFilePath(pszZipFileName);
}
else if (pszResPath != NULL)
{
// update the zip file path
updateZipFilePath(pszResPath);
// record the resource path
strcpy(s_ResourcePath, pszResPath);
}
else if (pszZipFileName != NULL)
{
// record the zip file path
setZipFilePath(pszZipFileName);
}
}
unsigned char* FileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize)
{
unsigned char * pBuffer = NULL;
do
{
if (strlen(s_ZipFilePath) != 0)
{
// if specify the zip file,load from it first
pBuffer = getFileDataFromZip(s_ZipFilePath, pszFileName, pSize);
BREAK_IF(pBuffer);
}
// read the file from hardware
char fullPath[EOS_FILE_MAX_PATH] = {0};
fullPathFromRelativePath(pszFileName, fullPath);
BREAK_IF(strlen(fullPath) <= 0);
FILE *fp = fopen(fullPath, pszMode);
BREAK_IF(!fp);
fseek(fp,0,SEEK_END);
*pSize = ftell(fp);
fseek(fp,0,SEEK_SET);
pBuffer = new unsigned char[*pSize];
fread(pBuffer,sizeof(unsigned char), *pSize,fp);
fclose(fp);
} while (0);
return pBuffer;
}
////////////////////////////////////////
//
// tool functions
//
////////////////////////////////////////
unsigned char* getFileDataFromZip(const char* pszZipFilePath, const char* pszFileName, unsigned long * pSize)
{
unsigned char * pBuffer = NULL;
unzFile pFile = NULL;
*pSize = 0;
do
{
BREAK_IF(!pszZipFilePath || !pszFileName);
BREAK_IF(strlen(pszZipFilePath) == 0);
pFile = unzOpen(pszZipFilePath);
BREAK_IF(!pFile);
int nRet = unzLocateFile(pFile, pszFileName, 1);
BREAK_IF(UNZ_OK != nRet);
char szFilePathA[260];
unz_file_info FileInfo;
nRet = unzGetCurrentFileInfo(pFile, &FileInfo, szFilePathA, sizeof(szFilePathA), NULL, 0, NULL, 0);
BREAK_IF(UNZ_OK != nRet);
nRet = unzOpenCurrentFile(pFile);
BREAK_IF(UNZ_OK != nRet);
pBuffer = new unsigned char[FileInfo.uncompressed_size];
int nSize = unzReadCurrentFile(pFile, pBuffer, FileInfo.uncompressed_size);
assert(nSize == 0 || nSize == FileInfo.uncompressed_size);
*pSize = FileInfo.uncompressed_size;
unzCloseCurrentFile(pFile);
} while (0);
if (pFile)
{
unzClose(pFile);
}
return pBuffer;
}
void fullPathFromRelativePath(const char *pszRelativePath, char* fullPath)
{
// if have set the zip file path,return the relative path of zip file
if (strlen(s_ZipFilePath) != 0)
{
if (strlen(pszRelativePath) < EOS_FILE_MAX_PATH)
{
strcpy(fullPath, pszRelativePath);
}
return;
}
// get the user data path and append relative path to it
if (strlen(s_ResourcePath) == 0)
{
const char* pAppDataPath = getDataPath();
strcpy(s_ResourcePath, pAppDataPath);
}
#ifndef _TRANZDA_VM_
char *pszDriver = "";
#else
char *pszDriver = "D:/Work7";
#endif
std::string pRet;
if ((strlen(pszRelativePath) > 1 && pszRelativePath[1] == ':'))
{
pRet = pszRelativePath;
}
else if (strlen(pszRelativePath) > 0 && pszRelativePath[0] == '/')
{
pRet = pszDriver;
pRet += pszRelativePath;
}
else
{
pRet = pszDriver;
pRet += s_ResourcePath;
pRet += pszRelativePath;
}
if (strlen(pRet.c_str()) < EOS_FILE_MAX_PATH &&
strlen(pRet.c_str()) > 0)
{
strcpy(fullPath, pRet.c_str());
}
return;
}
void updateZipFilePath(const char* pResPath)
{
if (! strlen(s_ZipFilePath))
{
return;
}
std::string strTemp = s_ZipFilePath;
int nPos = std::string::npos;
// find the path need br replaced
std::string ResPath;
if (strlen(s_ResourcePath))
{
ResPath = s_ResourcePath;
}
else
{
ResPath = getDataPath();
}
// replace the resource path in s_ZipFilePath
nPos = strTemp.find(ResPath.c_str());
if (nPos != std::string::npos)
{
strTemp.replace(nPos, ResPath.length(), pResPath);
memset(s_ZipFilePath, 0, sizeof(char) * EOS_FILE_MAX_PATH);
strcpy(s_ZipFilePath, strTemp.c_str());
}
}
void setZipFilePath(const char* pZipFileName)
{
// get the full path of zip file
char fullPath[EOS_FILE_MAX_PATH] = {0};
if (strlen(s_ResourcePath))
{
strcpy(fullPath, s_ResourcePath);
}
else
{
const char* pAppDataPath = getDataPath();
strcpy(fullPath, pAppDataPath);
}
strcat(fullPath, pZipFileName);
// if the zip file not exist,use message box to warn developer
TUChar pszTmp[EOS_FILE_MAX_PATH] = {0};
TUString::StrGBToUnicode(pszTmp, (const Char*) fullPath);
Boolean bExist = EOS_IsFileExist(pszTmp);
if (!bExist)
{
std::string strErr = "zip file ";
strErr += fullPath;
strErr += " not exist!";
TUChar szText[EOS_FILE_MAX_PATH] = { 0 };
TUString::StrUtf8ToStrUnicode(szText,(Char*)strErr.c_str());
TApplication::GetCurrentApplication()->MessageBox(szText,NULL,WMB_OK);
return;
}
#ifndef _TRANZDA_VM_
char *pszDriver = "";
#else
char *pszDriver = "D:/Work7";
#endif
// record the zip file path
strcpy(s_ZipFilePath, pszDriver);
strcat(s_ZipFilePath, fullPath);
}
const char* getDataPath()
{
if (strlen(s_AppDataPath))
{
return s_AppDataPath;
}
do
{
TUChar AppID[EOS_FILE_MAX_PATH] = {0};
UInt32 nCmdType = 0;
Int32 nRet = SS_AppRequest_GetAppName(AppID, &nCmdType);
BREAK_IF(nRet < 0);
TUChar AppPath[EOS_FILE_MAX_PATH] = {0};
SS_GetApplicationPath(AppID, SS_APP_PATH_TYPE_EXECUTABLE, AppPath);
TUString::StrUnicodeToStrUtf8((Char*) s_AppDataPath, AppPath);
} while (0);
return s_AppDataPath;
}
} // end of namespace CocosDenshion