forked from gecube/opencaesar3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oc3_filepath.cpp
590 lines (486 loc) · 12.7 KB
/
oc3_filepath.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
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
584
585
586
587
588
589
// This file is part of openCaesar3.
//
// openCaesar3 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// openCaesar3 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with openCaesar3. If not, see <http://www.gnu.org/licenses/>.
#include "oc3_filepath.hpp"
#include "oc3_filesystem.hpp"
#include "oc3_filelist.hpp"
#ifdef _WIN32
#include <Windows.h>
#include <io.h>
#elif __GNUC__
#ifdef __linux__
#include <sys/io.h>
#include <linux/limits.h>
#endif
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <libgen.h>
#endif
namespace io
{
const char* FilePath::anyFile = "*.*";
const char* FilePath::firstEntry = ".";
const char* FilePath::secondEntry = "..";
class FilePath::Impl
{
public:
std::string path;
};
void FilePath::splitToDirPathExt( FilePath* path,
FilePath* filename,
FilePath* extension )
{
std::string name = _d->path;;
int i = name.size();
int extpos = i;
// search for path separator or beginning
while ( i >= 0 )
{
if ( name[i] == '.' )
{
extpos = i;
if( extension )
{
*extension = FilePath( name.substr( extpos + 1, name.size() - (extpos + 1) ) );
}
}
else
if ( name[i] == '/' || name[i] == '\\' )
{
if ( filename )
{
*filename = FilePath( name.substr( i + 1, extpos - (i + 1) ) );
}
if ( path )
{
std::string rp = StringHelper::replace( name.substr( 0, i + 1 ), "\\", "/" ) ;
*path = FilePath( rp );
}
return;
}
i -= 1;
}
if ( filename )
{
*filename = FilePath( name.substr( 0, extpos ) );
}
}
void FilePath::_OsDelete( const FilePath& pathToDelete )
{
#ifdef _WIN32
DeleteFile( pathToDelete._d->path.c_str() );
#else
::remove( pathToDelete._d->path.c_str() );
#endif
}
FilePath FilePath::addEndSlash() const
{
std::string pathTo = _d->path;
if( pathTo.size() == 0 )
return FilePath( "" );
if( (*pathTo.rbegin()) != '/' && (*pathTo.rbegin()) != '\\' )
{
pathTo.append( "/" );
}
return pathTo;
}
FilePath FilePath::removeBeginSlash() const
{
std::string pathTo = _d->path;
_OC3_DEBUG_BREAK_IF( pathTo.size() == 0 );
if( pathTo.size() == 0 )
return FilePath( "" );
wchar_t endsym = *pathTo.begin();
if( endsym == '/' || endsym == '\\' )
pathTo.erase( 0 );
return pathTo;
}
FilePath FilePath::removeEndSlash() const
{
std::string pathTo = _d->path;
_OC3_DEBUG_BREAK_IF( pathTo.size() == 0 );
if( pathTo.size() == 0 )
return "";
if( (*pathTo.rbegin()) == '/' || (*pathTo.rbegin()) == '\\' )
{
pathTo.erase( pathTo.rbegin().base() );
}
return pathTo;
}
void FileDir::_OsCreate( const FileDir& dirName )
{
#ifdef _WIN32
CreateDirectory( removeEndSlash().toString().c_str(), NULL );
#else
::mkdir( dirName.toString().c_str(), S_IRWXU|S_IRWXG|S_IRWXO );
#endif
}
void FileDir::create()
{
if( isExist() )
return;
_OsCreate( *this );
}
bool FilePath::isExist() const
{
return FileSystem::instance().existFile( *this );
}
bool FilePath::isFolder() const
{
# ifdef _WIN32
WIN32_FILE_ATTRIBUTE_DATA fad;
if( ::GetFileAttributesEx( _d->path.c_str(), ::GetFileExInfoStandard, &fad )== 0 )
return false;
return (fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY;
# else
struct stat path_stat;
if ( ::stat( toString().c_str(), &path_stat) != 0 )
return false;
return S_ISDIR(path_stat.st_mode);
# endif
}
std::string FilePath::getExtension() const
{
if( isFolder() )
{
return "";
}
std::string::size_type index = _d->path.find_last_of( '.' );
if( index != std::string::npos )
{
return _d->path.substr( index, 0xff );
}
return "";
}
FilePath FilePath::getUpDir() const
{
if( !_d->path.size() )
return "";
FilePath pathToAny = removeEndSlash();
std::string::size_type index = pathToAny._d->path.find_last_of( "/" );
if( index != std::string::npos )
{
return FilePath( pathToAny._d->path.substr( 0, index ) );
}
_OC3_DEBUG_BREAK_IF( !isExist() );
return "";
}
FilePath FileDir::find( const FilePath& fileName ) const
{
_OC3_DEBUG_BREAK_IF( !isExist() );
if( !fileName.toString().size() )
{
return "";
}
if( fileName.isExist() )
{
return fileName;
}
FilePath finalPath( addEndSlash().toString() + fileName.toString() );
if( finalPath.isExist() )
{
return finalPath;
}
return fileName;
}
void FilePath::_OsRename( const FilePath& newName )
{
::rename( toString().c_str(), newName.toString().c_str() );
}
void FilePath::rename( const FilePath& pathNew )
{
_OC3_DEBUG_BREAK_IF( !isExist() );
*this = pathNew;
}
FilePath::FilePath( const std::string& nPath ) : _d( new Impl )
{
_d->path = StringHelper::replace( nPath, "\\", "/" );
}
FilePath::FilePath( const FilePath& nPath ) : _d( new Impl )
{
_d->path = nPath._d->path;
}
FilePath::FilePath( const char* nPath ) : _d( new Impl )
{
_d->path = StringHelper::replace( nPath, "\\", "/" );
}
FilePath::FilePath() : _d( new Impl )
{
_d->path = "";
}
const std::string& FilePath::toString() const
{
return _d->path;
}
std::string FilePath::removeExtension() const
{
std::string::size_type index = _d->path.find_last_of( '.' );
if( index != std::string::npos )
{
return _d->path.substr( 0, index );
}
return toString();
}
FilePath::~FilePath()
{
}
FilePath FilePath::getAbsolutePath() const
{
#if defined(_WIN32)
char *p=0;
char fpath[_MAX_PATH];
p = _fullpath(fpath, _d->path.c_str(), _MAX_PATH);
std::string tmp(p);
StringHelper::replace( tmp, "\\", "/");
return tmp;
#else
char* p=0;
char fpath[4096];
fpath[0]=0;
p = realpath( _d->path.c_str(), fpath);
if (!p)
{
// content in fpath is unclear at this point
if (!fpath[0]) // seems like fpath wasn't altered, use our best guess
{
FilePath tmp(_d->path);
return flattenFilename(tmp);
}
else
return FilePath(fpath);
}
if( *(_d->path.rbegin())=='/')
return FilePath( std::string(p) + "/" );
else
return FilePath( std::string(p) );
#endif
}
//! flatten a path and file name for example: "/you/me/../." becomes "/you"
FilePath FilePath::flattenFilename( const FilePath& root ) const
{
std::string directory = addEndSlash().toString();
directory = StringHelper::replace( directory, "\\", "/" );
FilePath dir;
FilePath subdir;
int lastpos = 0;
std::string::size_type pos = 0;
bool lastWasRealDir=false;
while( ( pos = directory.find( '/', lastpos) ) != std::string::npos )
{
subdir = FilePath( directory.substr(lastpos, pos - lastpos + 1) );
if( subdir.toString() == "../" )
{
if (lastWasRealDir)
{
dir = dir.getUpDir();
dir = dir.getUpDir();
lastWasRealDir=( dir.toString().size()!=0);
}
else
{
dir = FilePath( dir.toString() + subdir.toString() );
lastWasRealDir=false;
}
}
else if( subdir.toString() == "/")
{
dir = root;
}
else if( subdir.toString() != "./" )
{
dir = FilePath( dir.toString() + subdir.toString() );
lastWasRealDir=true;
}
lastpos = pos + 1;
}
return dir;
}
//! Get the relative filename, relative to the given directory
FilePath FilePath::getRelativePathTo( const FilePath& directory ) const
{
if ( toString().empty() || directory.toString().empty() )
return *this;
FilePath path1, file, ext;
getAbsolutePath().splitToDirPathExt( &path1, &file, &ext );
FilePath path2(directory.getAbsolutePath());
StringArray list1, list2;
list1 = StringHelper::split( path1.toString(), "/\\", 2);
list2 = StringHelper::split( path2.toString(), "/\\", 2);
unsigned int i=0;
unsigned int it1=0;
unsigned int it2=0;
#if defined (_WIN32)
char partition1 = 0, partition2 = 0;
FilePath prefix1, prefix2;
if ( it1 > 0 )
prefix1 = list1[ it1 ];
if ( it2 > 0 )
prefix2 = list2[ it2 ];
if ( prefix1.toString().size() > 1 && prefix1.toString()[1] == ':' )
{
partition1 = StringHelper::localeLower( prefix1.toString()[0] );
}
if ( prefix2.toString().size() > 1 && prefix2.toString()[1] == ':' )
{
partition2 = StringHelper::localeLower( prefix2.toString()[0] );
}
// must have the same prefix or we can't resolve it to a relative filename
if ( partition1 != partition2 )
{
return *this;
}
#endif
for (; i<list1.size() && i<list2.size()
#if defined (_WIN32)
&& ( StringHelper::isEquale( list1[ it1 ], list2[ it2 ], StringHelper::equaleIgnoreCase ) )
#else
&& ( list1[ it1 ]== list2[ it2 ] )
#endif
; ++i)
{
++it1;
++it2;
}
path1="";
for (; i<list2.size(); ++i)
{
path1 = path1.toString() + "../";
}
while( it1 != list1.size() )
{
path1 = path1.toString() + list1[ it1 ] + "/";
it1++;
}
path1 = path1.toString() + file.toString();
if( ext.toString().size() )
{
path1 = path1.toString() + "." + ext.toString();
}
return path1;
}
//! returns the base part of a filename, i.e. all except for the directory
//! part. If no directory path is prefixed, the full name is returned.
FilePath FilePath::getBasename(bool keepExtension) const
{
// find last forward or backslash
std::string::size_type lastSlash = toString().find_last_of('/');
// get number of chars after last dot
std::string::size_type end = 0;
if( !keepExtension )
{
// take care to search only after last slash to check only for
// dots in the filename
end = toString().find_last_of('.');
if( end == std::string::npos || end < lastSlash)
end=0;
else
end = toString().size()-end;
}
if( lastSlash != std::string::npos )
{
return FilePath( toString().substr(lastSlash+1, toString().size()-lastSlash-1-end) );
}
else if (end != 0)
{
return FilePath( toString().substr(0, toString().size()-end) );
}
else
{
return *this;
}
}
//! returns the directory part of a filename, i.e. all until the first
//! slash or backslash, excluding it. If no directory path is prefixed, a '.'
//! is returned.
FilePath FilePath::getFileDir() const
{
// find last forward or backslash
std::string::size_type lastSlash = toString().find_last_of( '/' );
if( lastSlash != std::string::npos )
{
return FilePath( toString().substr(0, lastSlash) );
}
else
return ".";
}
bool FilePath::operator==( const FilePath& other ) const
{
return toString() == other.toString();
}
bool FilePath::operator==( const std::string& other ) const
{
return toString() == other;
}
FilePath& FilePath::operator=( const FilePath& other )
{
_d->path = other._d->path;
return *this;
}
FileList FileDir::getEntries() const
{
FileSystem& fs = FileSystem::instance();
FileDir saveDir( fs.getWorkingDirectory() );
FileDir changeDd = *this;
fs.changeWorkingDirectoryTo( changeDd );
FileList fList( changeDd.toString(), false, false );
fList = fs.getFileList();
fs.changeWorkingDirectoryTo( saveDir );
return fList;
}
FileDir::FileDir( const FilePath& pathTo ) : FilePath( pathTo )
{
}
FileDir::FileDir( const std::string& nPath ) : FilePath( nPath )
{
}
FileDir::FileDir( const FileDir& nPath ) : FilePath( nPath.toString() )
{
}
FilePath FileDir::getFilePath( const FilePath& fileName )
{
std::string ret = addEndSlash().toString();
ret.append( fileName.removeBeginSlash().toString() );
return FilePath( ret );
}
bool FileDir::changeCurrentDir( const FilePath& dirName )
{
return FileSystem::instance().changeWorkingDirectoryTo( dirName );
}
FileDir FileDir::getCurrentDir()
{
return FileSystem::instance().getWorkingDirectory();
}
FileDir FileDir::getApplicationDir()
{
#ifdef WIN32
unsigned int pathSize=512;
ByteArray tmpPath;
tmpPath.resize( pathSize );
GetModuleFileName( 0, tmpPath.data(), pathSize);
FilePath tmp = tmpPath.data();
tmp = tmp.getUpDir();
//delete tmpPath;
return tmp;
#elif defined(__linux__)
char exe_path[PATH_MAX] = {0};
char * dir_path;
sprintf(exe_path, "/proc/%d/exe", getpid());
readlink(exe_path, exe_path, sizeof(exe_path));
dir_path = dirname(exe_path);
return FilePath(dir_path);
#endif // __GNUC__
return FilePath( "" );
}
} //end namespace io