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
|
#include <cstring>
#include <efsw/FileSystem.hpp>
#include <efsw/platform/platformimpl.hpp>
#include <climits>
#if EFSW_OS == EFSW_OS_MACOSX
#include <CoreFoundation/CoreFoundation.h>
#endif
#if EFSW_OS == EFSW_OS_WIN
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#endif
namespace efsw {
bool FileSystem::isDirectory( const std::string& path ) {
return Platform::FileSystem::isDirectory( path );
}
FileInfoMap FileSystem::filesInfoFromPath( std::string path ) {
dirAddSlashAtEnd( path );
return Platform::FileSystem::filesInfoFromPath( path );
}
char FileSystem::getOSSlash() {
return Platform::FileSystem::getOSSlash();
}
bool FileSystem::slashAtEnd( std::string& dir ) {
return ( dir.size() && dir[dir.size() - 1] == getOSSlash() );
}
void FileSystem::dirAddSlashAtEnd( std::string& dir ) {
if ( dir.size() >= 1 && dir[dir.size() - 1] != getOSSlash() ) {
dir.push_back( getOSSlash() );
}
}
void FileSystem::dirRemoveSlashAtEnd( std::string& dir ) {
if ( dir.size() >= 1 && dir[dir.size() - 1] == getOSSlash() ) {
dir.erase( dir.size() - 1 );
}
}
std::string FileSystem::fileNameFromPath( std::string filepath ) {
dirRemoveSlashAtEnd( filepath );
size_t pos = filepath.find_last_of( getOSSlash() );
if ( pos != std::string::npos ) {
return filepath.substr( pos + 1 );
}
return filepath;
}
std::string FileSystem::pathRemoveFileName( std::string filepath ) {
dirRemoveSlashAtEnd( filepath );
size_t pos = filepath.find_last_of( getOSSlash() );
if ( pos != std::string::npos ) {
return filepath.substr( 0, pos + 1 );
}
return filepath;
}
std::string FileSystem::getLinkRealPath( std::string dir, std::string& curPath ) {
FileSystem::dirRemoveSlashAtEnd( dir );
FileInfo fi( dir, true );
/// Check with lstat and see if it's a link
if ( fi.isLink() ) {
/// get the real path of the link
std::string link( fi.linksTo() );
/// get the current path of the directory without the link dir path
curPath = FileSystem::pathRemoveFileName( dir );
/// ensure that ends with the os directory slash
FileSystem::dirAddSlashAtEnd( link );
return link;
}
/// if it's not a link return nothing
return "";
}
std::string FileSystem::precomposeFileName( const std::string& name ) {
#if EFSW_OS == EFSW_OS_MACOSX
CFStringRef cfStringRef =
CFStringCreateWithCString( kCFAllocatorDefault, name.c_str(), kCFStringEncodingUTF8 );
CFMutableStringRef cfMutable = CFStringCreateMutableCopy( NULL, 0, cfStringRef );
CFStringNormalize( cfMutable, kCFStringNormalizationFormC );
const char* c_str = CFStringGetCStringPtr( cfMutable, kCFStringEncodingUTF8 );
if ( c_str != NULL ) {
std::string result( c_str );
CFRelease( cfStringRef );
CFRelease( cfMutable );
return result;
}
CFIndex length = CFStringGetLength( cfMutable );
CFIndex maxSize = CFStringGetMaximumSizeForEncoding( length, kCFStringEncodingUTF8 );
if ( maxSize == kCFNotFound ) {
CFRelease( cfStringRef );
CFRelease( cfMutable );
return std::string();
}
std::string result( maxSize + 1, '\0' );
if ( CFStringGetCString( cfMutable, &result[0], result.size(), kCFStringEncodingUTF8 ) ) {
result.resize( std::strlen( result.c_str() ) );
CFRelease( cfStringRef );
CFRelease( cfMutable );
} else {
result.clear();
}
return result;
#else
return name;
#endif
}
bool FileSystem::isRemoteFS( const std::string& directory ) {
return Platform::FileSystem::isRemoteFS( directory );
}
bool FileSystem::changeWorkingDirectory( const std::string& directory ) {
return Platform::FileSystem::changeWorkingDirectory( directory );
}
std::string FileSystem::getCurrentWorkingDirectory() {
return Platform::FileSystem::getCurrentWorkingDirectory();
}
std::string FileSystem::getRealPath( const std::string& path ) {
std::string realPath;
#if defined( EFSW_PLATFORM_POSIX )
char dir[PATH_MAX];
realpath( path.c_str(), &dir[0] );
realPath = std::string( dir );
#elif EFSW_OS == EFSW_OS_WIN
wchar_t dir[_MAX_PATH + 1];
GetFullPathNameW( String::fromUtf8( path ).toWideString().c_str(), _MAX_PATH, &dir[0],
nullptr );
realPath = String( dir ).toUtf8();
#else
#warning FileSystem::getRealPath() not implemented on this platform.
#endif
return realPath;
}
} // namespace efsw
|