-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPlatformFuncs.cpp
More file actions
310 lines (264 loc) · 9.05 KB
/
PlatformFuncs.cpp
File metadata and controls
310 lines (264 loc) · 9.05 KB
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
//---------------------------------------------------------------------------
#include "PlatformFuncs.h"
#include <stdio.h>
#include "Constants.h"
#ifdef linux
//Linux defines
#include <stdlib.h>
#include <string.h>
#include <string>
#else
//Windows defines
#include <io.h>
#include "Messages.h"
#include <windows.h>
#include <shellapi.h>
#endif
using namespace std;
////////////////////////////////////////////////////////////////////////////
// TarballSetup
////////////////////////////////////////////////////////////////////////////
#ifdef linux
//Linux version
bool TarballSetup(string sAppPath) {
return true;
}
#else
//Windows version
bool TarballSetup(string sAppPath) {
string sPath = sAppPath + "gzip.exe";
int iAvailable = access(sPath.c_str(), 0); //0 signifies checking for existence
if (0 == iAvailable) {
sPath = sAppPath + "tar.exe";
if (0 == iAvailable)
return true;
else
return false;
} else
return false;
}
#endif
////////////////////////////////////////////////////////////////////////////
// ZipFile
////////////////////////////////////////////////////////////////////////////
void ZipFile(string sFileName, string sAppPath) {
FILE *filetest; //only this kind of file can be tested for existence of a
//file without creating
//Does the file exist? If not, exit without error
filetest = fopen(sFileName.c_str(), "r");
if (filetest == NULL) return;
else fclose(filetest);
// gzip flags - -f is force, which will overwrite
string sArgs = " -f -q \"" + sFileName + "\"";
#ifdef linux
LaunchProcess("gzip", sArgs, "");
#else
LaunchProcess("gzip", sArgs, sAppPath);
/// @todo check error
#endif
}
////////////////////////////////////////////////////////////////////////////
// UnzipFile
////////////////////////////////////////////////////////////////////////////
void UnzipFile(string sFileName) {
FILE *filetest; //only this kind of file can be tested for existence of a
//file without creating
//Does the file exist? If not, exit without error
filetest = fopen(sFileName.c_str(), "r");
if (filetest == NULL) return;
else fclose(filetest);
//gzip flags - -f is force, which will overwrite existing files, -q is
//quiet, which suppresses warnings, and -d is the uncompress flag
string sArgs = "-f -q -d \"" + sFileName + "\"";
LaunchProcess("gzip", sArgs, "");
}
////////////////////////////////////////////////////////////////////////////
// AddFileToTarball
////////////////////////////////////////////////////////////////////////////
void AddFileToTarball(string sTarball, string sFileToAdd, string sAppPath) {
FILE *filetest; //only this kind of file can be tested for existence of a
//file without creating
string sFlags;
//Does the file to add exist? If not, exit without error
filetest = fopen(sFileToAdd.c_str(), "r");
if (filetest == NULL) return;
else fclose(filetest);
//Make sure we feed only forward slashes to tar
ReplaceAll(sTarball, "\\", "/");
ReplaceAll(sFileToAdd, "\\", "/");
//Set up the parameters for tar based on whether or not the tarball already
//exists
filetest = fopen(sTarball.c_str(), "r");
if (filetest == NULL) {
//The tarball doesn't exist - set up tar's flags to create
//strcpy(cFlags, "--create --no-recursion --file "); //-c means create - -f for file name
sFlags = " -cf ";
} else {
//The tarball does exist - set up tar's flags to create
//strcpy(cFlags, "--append --file "); //-r means append - -f for file name
sFlags = " -rf ";
fclose(filetest);
}
string sArgs = sFlags + "\"" + sTarball + "\" \"" + sFileToAdd + "\"";
#ifdef linux
LaunchProcess("tar", sArgs, "");
#else
LaunchProcess("tar", sArgs, sAppPath);
/// @todo check error
#endif
}
////////////////////////////////////////////////////////////////////////////
// AddFileToTarball
////////////////////////////////////////////////////////////////////////////
void AddFileToNewTarball(string sTarball, string sFileToAdd, string sAppPath) {
FILE *filetest; //only this kind of file can be tested for existence of a
//file without creating
//Make sure we feed only forward slashes to tar
ReplaceAll(sTarball, "\\", "/");
ReplaceAll(sFileToAdd, "\\", "/");
//Does the file to add exist? If not, exit without error
filetest = fopen(sFileToAdd.c_str(), "r");
if (filetest == NULL) return;
else fclose(filetest);
//Set up the parameters for tar to create the tarball
string sArgs = " -cf\"" + sTarball + "\" \"" + sFileToAdd + "\"";
#ifdef linux
LaunchProcess("tar", sArgs, "");
#else
LaunchProcess("tar", sArgs, sAppPath);
/// @todo check error
#endif
}
////////////////////////////////////////////////////////////////////////////
// DeleteThisFile
////////////////////////////////////////////////////////////////////////////
#ifdef linux
//Linux version
void DeleteThisFile(string sFile) {
//unlink(sFile.c_str());
string sCmd;
sCmd = "rm -f " + sFile;
system(sCmd.c_str());
/// @todo check error
}
#else
//Windows version
void DeleteThisFile(string sFile) {
//DeleteFile(cFile);
//While using the above code is preferable, for some reason Java crashes when
//loading the DLL. So we have to do things the hard way - through the shell.
//This next part I'm copying from http://www.bridgespublishing.com/articles/
//issues/9806/File_operations.htm
// Create a char array and fill it with nulls. This is because pFrom and
//pTo have to be double-null terminated, so this will make sure of it.
char src[MAX_PATH], dst[MAX_PATH];
memset(src, 0, sizeof(src));
memset(dst, 0, sizeof(dst));
// Create a SHFILOPSTRUCT and zero it, too.
SHFILEOPSTRUCT fos;
memset(&fos, 0, sizeof(fos));
//Set the fields of SHFILOPSTRUCT
strcpy(src, sFile.c_str());
fos.hwnd = NULL; //handle of parent window
fos.wFunc = FO_DELETE; //operation to perform
fos.pFrom = src; //pFrom - file to work on
fos.pTo = dst; //pTo - destination
fos.fFlags = FOF_SILENT | FOF_NOCONFIRMATION; //no dialog box and no asking
//the user
//Now perform the operation
SHFileOperation(&fos);
}
#endif
////////////////////////////////////////////////////////////////////////////
// DoesFileExist
////////////////////////////////////////////////////////////////////////////
#ifdef linux
//Linux version
bool DoesFileExist(string sFile) {
FILE *filetest;
filetest = fopen(sFile.c_str(), "r");
if (filetest == NULL) return false;
else fclose(filetest);
return true;
}
#else
//Windows version
bool DoesFileExist(string sFile) {
int iCode = access(sFile.c_str(), 0); //0 signifies checking for existence
if (0 == iCode)
return true;
else
return false;
}
#endif
////////////////////////////////////////////////////////////////////////////
// LaunchProcess
////////////////////////////////////////////////////////////////////////////
#ifdef linux
//Linux version
void LaunchProcess(string sFile, string sArgs, string sDir) {
string sCmd;
if (sDir.length() > 0)
sCmd = sDir + "/" + sFile + " " + sArgs;
else
sCmd = sFile + " " + sArgs;
system(sCmd.c_str());
}
#else
//Windows version
void LaunchProcess(string sFile, string sArgs, string sDir) {
SHELLEXECUTEINFO *params; //the data structure for running ShellExecuteEx
params = new SHELLEXECUTEINFO;
params->cbSize = sizeof(SHELLEXECUTEINFO); //size of this structure
params->fMask = SEE_MASK_NOCLOSEPROCESS; //flag to allow us to find out when
//the process terminates
params->lpVerb = 0; //what the executable should do - open (run)
params->lpFile = sFile.c_str(); //path to executable
params->lpParameters = sArgs.c_str(); //parameters passed
params->lpDirectory = sDir.c_str();
params->nShow = SW_HIDE;
//params->nShow = SW_SHOW;
//Run the executable
if (ShellExecuteEx(params)) {
//int iVal = (int)params->hInstApp;
//if (SE_ERR_FNF == iVal) {
// int i = 0;
//} else if (SE_ERR_PNF == iVal) {
// int i = 0;
//} else if (SE_ERR_ACCESSDENIED == iVal) {
// int i = 0;
//} else if (SE_ERR_OOM == iVal) {
// int i = 0;
//} else if (SE_ERR_DLLNOTFOUND == iVal) {
// int i = 0;
//} else if (SE_ERR_SHARE == iVal) {
// int i = 0;
//} else if (SE_ERR_ASSOCINCOMPLETE == iVal) {
// int i = 0;
//} else if (SE_ERR_DDETIMEOUT == iVal) {
// int i = 0;
//} else if (SE_ERR_DDEFAIL == iVal) {
// int i = 0;
//} else if (SE_ERR_DDEBUSY == iVal) {
// int i = 0;
//} else if (SE_ERR_NOASSOC == iVal) {
// int i = 0;
//}
//DWORD dw = GetLastError();
//force the process to complete before moving on
WaitForSingleObject(params->hProcess, INFINITE);
}
delete params;
}
#endif
////////////////////////////////////////////////////////////////////////////
// ReplaceAll
////////////////////////////////////////////////////////////////////////////
void ReplaceAll( std::string &source, const std::string &find,
const std::string& replace ) {
size_t j;
while ((j = source.find( find )) != std::string::npos)
{
source.replace( j, find.length(), replace );
}
}