-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.cpccFileSystemMini.cpp
More file actions
executable file
·583 lines (436 loc) · 17.8 KB
/
io.cpccFileSystemMini.cpp
File metadata and controls
executable file
·583 lines (436 loc) · 17.8 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/* *****************************************
* File: cpccFileSystemMini.cpp
* Version: see function getClassVersion()
* Purpose: Portable (cross-platform), light-weight, file system library
* *****************************************
* Library: Cross Platform C++ Classes (cpcc)
* Copyright: 2013 StarMessage software.
* License: Free for opensource projects.
* Commercial license for closed source projects.
* Web: http://www.StarMessageSoftware.com/cpcclibrary
* https://github.com/starmessage/cpcc
* email: sales -at- starmessage.info
* *****************************************
*/
// do not compile this if added in the xcode project files.
// the .mm must be included as well
#if defined(_WIN32) || defined (IMPORTED_BY_io_cpccFileSystemMini_mm)
#include <assert.h>
#include <cstdio> // for tempnam()
// #include <stdlib.h> // for free()
#include <iostream>
#include <fstream>
#include <errno.h>
#include <locale>
#include <codecvt>
#ifdef _WIN32
#include <io.h> // for _access on windows
#include <Shlobj.h> // for SHGetFolderPath()
#pragma comment(lib,"Shell32.lib") // instruct visual studio to link the library
#include <Shlwapi.h>
#pragma comment(lib,"Shlwapi.lib") // instruct visual studio to link the library
#include <tchar.h>
#include <sys/types.h>
#include <sys/stat.h>
#elif defined(__APPLE__)
#include <TargetConditionals.h>
#include <mach-o/dyld.h> // for _NSGetExecutablePath()
#include "io.cpccFileSystemMiniOSX.h"
#include <CoreFoundation/CoreFoundation.h>
#if TARGET_OS_IPHONE
#include <Foundation/Foundation.h>
#else
#include <Carbon/Carbon.h> // for the DialogRef
#endif
#endif
#include "io.cpccFileSystemMini.h"
#include "fs.cpccPathHelper.h"
#include "fs.cpccSystemFolders.h"
#include "fs.cpccUserFolders.h"
#if defined(cpccFileSystemMini_DoSelfTest)
#include "cpcc_SelfTest.h"
#endif
// todo: std::tmpfile() is considered better
cpcc_string cpccFileSystemMini::getTempFilename(void)
{
#ifdef TARGET_OS_IPHONE // all IOS devices
// we must get a folder inside the application’s temporary directory
// ToDo: NSTemporaryDirectory() can return nil
NSString *tempPath = NSTemporaryDirectory();
NSString *tempFile = [tempPath stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]];
return [tempFile UTF8String];
// std::string tmpFolder([NSTemporaryDirectory() UTF8String]);
#elif _WIN32
// todo: never use tmpnam
cpcc_char name[L_tmpnam_s];
if (cpcc_tmpnam_s(name, L_tmpnam_s)==0)
return name;
#else // UNIX and MacOS solution
// see also tempnam()
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/tempnam.html
// 'tempnam' is deprecated
/*
char * namePtr = tempnam(NULL, NULL);
std::string result(namePtr?namePtr:"");
if (namePtr)
free(namePtr);
return result;
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
// 'tmpnam' is deprecated:
char name[L_tmpnam];
if (std::tmpnam(name))
return name;
#pragma GCC diagnostic pop
#endif
return _T("");
}
// std::mutex _fileAppendMutex, _writeToFileMutex;
// std::mutex cpccFileSystemMini::fileAppendMutex_;
// static std::mutex fileAppendMutex_;
// main class
cpcc_string cpccFileSystemMini::getFileSystemReport(void)
{
cpcc_string report( _T("File System Report by cpccFileSystemMini\n----------------------\n"));
#if !(TARGET_OS_IPHONE)
// std::cout << "leak:Will call getAppFullPathFilename()\n";
report.append(_T("App full path filename:") + getAppFullPathFilename() + _T("\n"));
// std::cout << "leak:Will call getAppFullPath()\n";
report.append(_T("App path:") + getAppFullPath() + _T("\n"));
#endif
// std::cout << "leak:Will call getAppFilename()\n";
report.append(_T("App filename:") + getAppFilename() + _T("\n"));
// std::cout << "leak:Will call getFolder_SystemsTemp()\n";
// report.append(_T("System's Temp folder:") + getFolder_SystemsTemp() + _T("\n"));
// std::cout << "leak:Will call getFolder_UsersTemp()\n";
report.append(_T("User's Temp folder:") + cpccUserFolders::getUsersTempDir() + _T("\n"));
// std::cout << "leak:Will call getFolder_CurrentDir()\n";
report.append(_T("Current dir:") + cpccUserFolders::getCurrentDir() + _T("\n"));
// std::cout << "leak:Will call getFolder_UserHome()\n";
report.append(_T("User's home folder:") + cpccUserFolders::getUserHomeDir() + _T("\n"));
// std::cout << "leak:Will call getFolder_Desktop()\n";
report.append(_T("Desktop folder:") + cpccUserFolders::getDesktop() + _T("\n"));
// std::cout << "leak:Will call getFolder_Fonts()\n";
report.append(_T("Fonts folder:") + cpccSystemFolders::getFontsDir() + _T("\n"));
// std::cout << "leak:Will call getFolder_CommonAppData()\n";
report.append(_T("AppData path:") + cpccSystemFolders::getCommonAppData() + _T("\n"));
#if !(TARGET_OS_IPHONE)
// std::cout << "leak:Will call getFolder_UserData()\n";
report.append(_T("UserData path:") + cpccUserFolders::getUserData() + _T("\n"));
#endif
report.append(_T("End of file system report\n----------------------\n"));
return report;
}
bool cpccFileSystemMini::copyFile(const cpcc_char * sourceFile, const cpcc_char * destFileOrFolder)
{
if (folderExists(destFileOrFolder)) // if the destination is a folder, copy the file inside the folder
{
cpcc_string destFile = destFileOrFolder;
cpccPathHelper::addTrailingPathDelimiter(destFile);
destFile = destFile + cpccPathHelper::extractFilename(sourceFile);
return copyFileToaFile(sourceFile, destFile.c_str());
}
return copyFileToaFile(sourceFile, destFileOrFolder);
}
bool cpccFileSystemMini::writeTextFile(const cpcc_char* aFilename, const cpcc_char *aTxt, const bool inUTF8)
{
if (!aFilename)
return false;
cpcc_ofstream _file(aFilename);
if (!_file.good())
{
#pragma warning(suppress : 4996)
cpcc_cerr << _T("Error saving file ") << aFilename << _T(" Error message:") << strerror(errno) << _T("\n");
return false;
}
if (inUTF8) // write the BOM of UTF-8 and set locale
{
// _file << UTF8_BOM;
//_file.write(BOM_UTF8, 3);
// http://www.cplusplus.com/forum/beginner/107125/
std::locale my_utf8_locale(std::locale(), new std::codecvt_utf8<wchar_t>);
std::locale namedObject_loc = _file.imbue(my_utf8_locale); // namedObject_loc is to suppress warning C26444
// _file << helper_to_utf8(aTxt, cpcc_strlen(aTxt));
}
_file << aTxt;
_file.close();
return true;
}
bool cpccFileSystemMini::appendTextFile(const cpcc_char* aFilename, const cpcc_char *txt)
{
// when mutex is used, an exception "mutex lock failed: Invalid argument" occurs when the log add() function is called on program termination
// #define USE_MUTEX_F
#ifdef USE_MUTEX_F
// static variable
static std::mutex fileAppendMutex_;
std::lock_guard<std::mutex> autoMutex(fileAppendMutex_);
#endif
// #define USE_FOPEN_F
#ifdef USE_FOPEN_F
FILE *fp;
#pragma warning(suppress : 4996)
fp= cpcc_fopen(aFilename, _T("at")); // write append // todo: UNICODE
if (!fp) return false;
cpcc_fprintf(fp,_T("%s"),txt);
fclose(fp);
return true;
#else
// std::ofstream outfile("thefile.txt", std::ios_base::app | std::ios_base::out);
// ios::app = opens the file in append mode.
// static std::ofstream _f; // with static is hangs in WinXP
// std::ofstream _f;
cpcc_ofstream ofs;
ofs.open(aFilename, std::ios_base::app | std::ios_base::out);
if (!ofs.good())
return false;
std::locale my_utf8_locale(std::locale(), new std::codecvt_utf8<wchar_t>);
std::locale namedObject_loc = ofs.imbue(my_utf8_locale); // namedObject_loc is to suppress warning C26444
ofs << txt;
// _f << fflush;
ofs.close();
return true;
#endif
}
bool cpccFileSystemMini::createFolder(const cpcc_char * aFoldername)
{
if (folderExists(aFoldername))
return true;
#ifdef _WIN32
::CreateDirectory(aFoldername, NULL);
#elif defined(__APPLE__)
/* it will probably fail if the folder starts with tilde (~)
expand the tilde before calling the function
*/
cpccPathHelper ph;
cpcc_string finalPath(fileSystemOSX_helper::expandTilde_OSX( aFoldername));
cpcc_string parentFolder = ph.getParentFolderOf(finalPath.c_str());
int parentPermissions = fileSystemOSX_helper::getFileOrFolderPermissions_OSX(parentFolder.c_str());
//std::cout << "permissions of " << parentFolder << " : " << parentPermissions << "\n";
// http://stackoverflow.com/questions/675039/how-can-i-create-directory-tree-in-c-linux
// https://discussions.apple.com/thread/844719?start=0&tstart=0
// http://www.linuxquestions.org/questions/programming-9/mkdir-in-c-setting-wrong-permissions-613250/
// read/write/search permissions for owner and group, and with read/search permissions for others
//mode_t p775 = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH;
// full permissions for all
//mode_t p777 = S_IRWXU | S_IRWXG | S_IRWXO;
#if TARGET_OS_IPHONE
return fileSystemOSX_helper::createFolder(finalPath.c_str(), parentPermissions);
#elif TARGET_OS_MAC
return fileSystemOSX_helper::createFolder_Linux(finalPath.c_str(), parentPermissions);
#endif
#endif
return(folderExists(aFoldername));
}
/**
to create an empty file (replacing any existing one) call this as
writeToFile(aFilename, "", 0, false)
*/
size_t cpccFileSystemMini::writeToFile(const cpcc_char *aFilename, const char *buffer, const size_t bufSize, const bool appendToFile)
{
if (!buffer)
return -3;
//static std::mutex _writeToFileMutex;
//std::lock_guard<std::mutex> autoMutex(_writeToFileMutex);
#pragma warning(disable : 4996)
FILE * pFile = cpcc_fopen (aFilename, (appendToFile)? _T("ab") : _T("wb") ); // todo: unicode
if (pFile==NULL)
return -1;
size_t res = fwrite(buffer, 1, bufSize, pFile);
if( ferror( pFile ) )
res=-2;
fclose (pFile);
return res;
}
size_t cpccFileSystemMini::readFromFile(const cpcc_char *aFilename, char *buffer, const size_t bufSize)
{
#ifdef _WIN32
#pragma warning(disable : 4996)
#endif
if (!buffer)
return -3;
FILE * pFile;
pFile = cpcc_fopen(aFilename, _T("rb")); // todo: unicode
if (pFile==NULL)
return -1;
size_t res=fread(buffer, 1, bufSize, pFile);
if( ferror( pFile ) )
res=-2;
fclose (pFile);
return res;
}
bool cpccFileSystemMini::copyFileToaFile(const cpcc_char* sourceFile, const cpcc_char* destFile)
{
// You may use std::fopen, std::fread, std::fwrite, and std::fclose,
// all of which are part of the standard C++ library (#include <cstdio>, very portable)
// http://msdn.microsoft.com/en-us/library/yeby3zcb%28v=vs.90%29.aspx
char buf[4096];
size_t size;
#ifdef _WIN32
#pragma warning( disable : 4996 )
#endif
FILE* source = cpcc_fopen(sourceFile, _T("rb")); // todo: unicode
if (!source)
return false;
FILE* dest = cpcc_fopen(destFile, _T("wb")); // todo: unicode
if (!dest)
{
fclose(source);
return false;
}
bool errorOccured=false;
while ((!errorOccured) && (size = fread(buf, 1, BUFSIZ, source)))
errorOccured = ( fwrite(buf, 1, size, dest) != size );
fclose(source);
fclose(dest);
return (!errorOccured);
}
bool cpccFileSystemMini::createEmptyFile(const cpcc_char * aFilename)
{
if (!aFilename)
return false;
return (writeToFile(aFilename, "", 0, false) == 0);
// other way: std::ofstream tmpOut(aFilename);
}
cpcc_string cpccFileSystemMini::getAppFilename(void)
{
// remove the path part and leave only the filename
return cpccPathHelper::extractFilename(getAppFullPathFilename());
}
#ifndef cpccTARGET_IOS
cpcc_string cpccFileSystemMini::getAppFullPath(void)
{
return cpccPathHelper::getParentFolderOf(getAppFullPathFilename().c_str());
}
#endif
#ifdef __APPLE__
std::string getAppFullPathFilename_Apple(void)
{
char path[MAXPATHLEN+1], *pathPtr = path;
uint32_t path_len = MAXPATHLEN;
// This function returns 0 if the path was successfully copied.
// It returns -1 if the buffer is not large enough, and * bufsize is set to the size required.
int ret = _NSGetExecutablePath(pathPtr, &path_len);
if (ret == -1)
{
pathPtr = new char[path_len+1];
ret = _NSGetExecutablePath(pathPtr, &path_len);
}
if (ret == 0)
{
char resolvedPath[PATH_MAX+1];
// The path may contain symbolic links, "..", etc. but the realpath function can be used to clean those up
if (realpath(pathPtr, resolvedPath) != 0)
return resolvedPath;
return path;
}
return ".";
}
#endif
cpcc_string cpccFileSystemMini::getAppFullPathFilename(void)
{
#ifdef _WIN32
_TCHAR fullPathfileName[MAX_PATH];
if (GetModuleFileName(NULL,fullPathfileName, MAX_PATH))
return std::basic_string<TCHAR>(fullPathfileName);
#elif TARGET_OS_IPHONE
NSString *path = [NSBundle mainBundle].bundlePath;
return [path UTF8String];
#elif TARGET_OS_MAC
return getAppFullPathFilename_Apple();
/*
http://stackoverflow.com/questions/799679/programatically-retrieving-the-absolute-path-of-an-os-x-command-line-app/1024933#1024933
http://astojanov.wordpress.com/2011/11/16/mac-os-x-resolve-absolute-path-using-process-pid/
*/
/*
char fullPathfileName[4096];
ProcessSerialNumber psn;
// needs the framework ApplicationServices.framework
GetCurrentProcess(&psn); // it is deprecated
pid_t pid;
GetProcessPID (&psn, &pid); // it is deprecated
if (proc_pidpath(pid, fullPathfileName, sizeof(fullPathfileName)) > 0)
return fullPathfileName;
*/
#else
#error Error 5453: unsupported platform for getModuleFilename()
#endif
return _T("");
}
// ///////////////////////////////////////////
// class cpccPathString
// ///////////////////////////////////////////
void cpccPathString::appendPathSegment(const cpcc_char* aPathSegment)
{
assign(cpccPathHelper::pathCat(c_str(), aPathSegment));
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if defined(cpccFileSystemMini_DoSelfTest)
void cpccFileSystemMini::selfTest(void)
{
#ifdef cpccDEBUG
// "#5349a: path delimiter"
cpcc_char pDelimiter = cpccPathHelper::getPreferredPathDelimiter();
assert(((pDelimiter=='/') || (pDelimiter=='\\') ) );
//std::cout << "cpccFileSystemMini::SelfTest point1\n";
// temp path is empty string
cpcc_string tmpFolder = cpccUserFolders::getUsersTempDir();
assert(tmpFolder.length()>1 && "#5356a: cpccFileSystemMini::selfTest");
//std::cout << "cpccFileSystemMini::SelfTest point2\n";
cpccPathHelper::addTrailingPathDelimiter(tmpFolder);
if(!folderExists(tmpFolder.c_str()))
createFolder(tmpFolder);
assert(folderExists(tmpFolder.c_str()) && _T("#5356p: cpccFileSystemMini::selfTest"));
// fileExists or createEmptyFile
cpcc_string tmpFile = tmpFolder + _T("selftest-cpccFileSystemMini.txt");
// cpcc_cout << _T("\nTmpFile:") << tmpFile << _T("\n");
bool created = createEmptyFile(tmpFile.c_str());
if (!created)
std::cerr << "createEmptyFile() failed" << std::endl;
assert(fileExists(tmpFile.c_str()) && _T("#5356d: cpccFileSystemMini::selfTest"));
// std::cout << "cpccFileSystemMini::SelfTest point3\n";
createEmptyFile(tmpFile.c_str());
// std::cout << "cpccFileSystemMini::SelfTest point3.1\n";
assert(getFileSize(tmpFile.c_str())==0 && _T("#5356e: cpccFileSystemMini::selfTest"));
// std::cout << "cpccFileSystemMini::SelfTest point3.2\n";
#ifdef UNICODE
// text to test UNICODE
// const cpcc_char * fileContent= _T("kalimera sas, καλημέρα σας.");
const cpcc_char * fileContent = _T("kalimera sas!!");
#else
const cpcc_char * fileContent = _T("kalimera sas!!");
#endif
appendTextFile(tmpFile.c_str(),fileContent);
// std::cout << "cpccFileSystemMini::SelfTest point4\n";
// getFileSize
long long filesize1 = getFileSize(tmpFile.c_str()),
filesize2 = cpcc_strlen(fileContent);
assert(filesize1 == filesize2 && _T("#5356h: cpccFileSystemMini::selfTest"));
// fileExists or deleteFile
deleteFile(tmpFile.c_str());
assert(!fileExists(tmpFile.c_str()) && _T("#5356g: cpccFileSystemMini::selfTest"));
// check if the appendFile also creates the file if it does not already exist
appendTextFile(tmpFile.c_str(), "test to create a file" );
assert(fileExists(tmpFile.c_str()) && _T("#5356r: cpccFileSystemMini::selfTest"));
deleteFile(tmpFile.c_str());
// std::cout << "cpccFileSystemMini::SelfTest point5\n";
#ifdef _WIN32
assert(folderExists(_T("c:\\")) && _T("#5356h: cpccFileSystemMini::selfTest"));
assert(!folderExists(_T("c:\\non-existing-folder")) && _T("#5356k: cpccFileSystemMini::selfTest"));
#endif
#ifdef __APPLE__
assert(folderExists("/Library") && "#5356h: cpccFileSystemMini::selfTest");
assert(!folderExists("/non-existing-folder") && "#5356k: cpccFileSystemMini::selfTest");
#endif
#endif
}
#endif
// ///////////////////////////////////////////
// Selftest cpccFileSystemMini
// ///////////////////////////////////////////
#if defined(cpccFileSystemMini_DoSelfTest)
SELFTEST_BEGIN(cpccFileSystemMini_SelfTest)
cpccFileSystemMini::selfTest();
SELFTEST_END
#endif // cpccFileSystemMini_DoSelfTest
#endif // of #if defined(_WIN32) || defined (IMPORTED_BY_io_cpccFileSystemMini_mm)