diff options
Diffstat (limited to 'src/3rdParty/efsw/FileSystem.cpp')
-rwxr-xr-x | src/3rdParty/efsw/FileSystem.cpp | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/src/3rdParty/efsw/FileSystem.cpp b/src/3rdParty/efsw/FileSystem.cpp new file mode 100755 index 0000000..867f120 --- /dev/null +++ b/src/3rdParty/efsw/FileSystem.cpp | |||
@@ -0,0 +1,118 @@ | |||
1 | #include <efsw/FileSystem.hpp> | ||
2 | #include <efsw/platform/platformimpl.hpp> | ||
3 | |||
4 | #if EFSW_OS == EFSW_OS_MACOSX | ||
5 | #include <CoreFoundation/CoreFoundation.h> | ||
6 | #endif | ||
7 | |||
8 | namespace efsw { | ||
9 | |||
10 | bool FileSystem::isDirectory( const std::string& path ) { | ||
11 | return Platform::FileSystem::isDirectory( path ); | ||
12 | } | ||
13 | |||
14 | FileInfoMap FileSystem::filesInfoFromPath( std::string path ) { | ||
15 | dirAddSlashAtEnd( path ); | ||
16 | |||
17 | return Platform::FileSystem::filesInfoFromPath( path ); | ||
18 | } | ||
19 | |||
20 | char FileSystem::getOSSlash() { | ||
21 | return Platform::FileSystem::getOSSlash(); | ||
22 | } | ||
23 | |||
24 | bool FileSystem::slashAtEnd( std::string& dir ) { | ||
25 | return ( dir.size() && dir[dir.size() - 1] == getOSSlash() ); | ||
26 | } | ||
27 | |||
28 | void FileSystem::dirAddSlashAtEnd( std::string& dir ) { | ||
29 | if ( dir.size() > 1 && dir[dir.size() - 1] != getOSSlash() ) { | ||
30 | dir.push_back( getOSSlash() ); | ||
31 | } | ||
32 | } | ||
33 | |||
34 | void FileSystem::dirRemoveSlashAtEnd( std::string& dir ) { | ||
35 | if ( dir.size() > 1 && dir[dir.size() - 1] == getOSSlash() ) { | ||
36 | dir.erase( dir.size() - 1 ); | ||
37 | } | ||
38 | } | ||
39 | |||
40 | std::string FileSystem::fileNameFromPath( std::string filepath ) { | ||
41 | dirRemoveSlashAtEnd( filepath ); | ||
42 | |||
43 | size_t pos = filepath.find_last_of( getOSSlash() ); | ||
44 | |||
45 | if ( pos != std::string::npos ) { | ||
46 | return filepath.substr( pos + 1 ); | ||
47 | } | ||
48 | |||
49 | return filepath; | ||
50 | } | ||
51 | |||
52 | std::string FileSystem::pathRemoveFileName( std::string filepath ) { | ||
53 | dirRemoveSlashAtEnd( filepath ); | ||
54 | |||
55 | size_t pos = filepath.find_last_of( getOSSlash() ); | ||
56 | |||
57 | if ( pos != std::string::npos ) { | ||
58 | return filepath.substr( 0, pos + 1 ); | ||
59 | } | ||
60 | |||
61 | return filepath; | ||
62 | } | ||
63 | |||
64 | std::string FileSystem::getLinkRealPath( std::string dir, std::string& curPath ) { | ||
65 | FileSystem::dirRemoveSlashAtEnd( dir ); | ||
66 | FileInfo fi( dir, true ); | ||
67 | |||
68 | /// Check with lstat and see if it's a link | ||
69 | if ( fi.isLink() ) { | ||
70 | /// get the real path of the link | ||
71 | std::string link( fi.linksTo() ); | ||
72 | |||
73 | /// get the current path of the directory without the link dir path | ||
74 | curPath = FileSystem::pathRemoveFileName( dir ); | ||
75 | |||
76 | /// ensure that ends with the os directory slash | ||
77 | FileSystem::dirAddSlashAtEnd( link ); | ||
78 | |||
79 | return link; | ||
80 | } | ||
81 | |||
82 | /// if it's not a link return nothing | ||
83 | return ""; | ||
84 | } | ||
85 | |||
86 | std::string FileSystem::precomposeFileName( const std::string& name ) { | ||
87 | #if EFSW_OS == EFSW_OS_MACOSX | ||
88 | CFStringRef cfStringRef = | ||
89 | CFStringCreateWithCString( kCFAllocatorDefault, name.c_str(), kCFStringEncodingUTF8 ); | ||
90 | CFMutableStringRef cfMutable = CFStringCreateMutableCopy( NULL, 0, cfStringRef ); | ||
91 | |||
92 | CFStringNormalize( cfMutable, kCFStringNormalizationFormC ); | ||
93 | |||
94 | char c_str[255 + 1]; | ||
95 | CFStringGetCString( cfMutable, c_str, sizeof( c_str ) - 1, kCFStringEncodingUTF8 ); | ||
96 | |||
97 | CFRelease( cfStringRef ); | ||
98 | CFRelease( cfMutable ); | ||
99 | |||
100 | return std::string( c_str ); | ||
101 | #else | ||
102 | return name; | ||
103 | #endif | ||
104 | } | ||
105 | |||
106 | bool FileSystem::isRemoteFS( const std::string& directory ) { | ||
107 | return Platform::FileSystem::isRemoteFS( directory ); | ||
108 | } | ||
109 | |||
110 | bool FileSystem::changeWorkingDirectory( const std::string& directory ) { | ||
111 | return Platform::FileSystem::changeWorkingDirectory( directory ); | ||
112 | } | ||
113 | |||
114 | std::string FileSystem::getCurrentWorkingDirectory() { | ||
115 | return Platform::FileSystem::getCurrentWorkingDirectory(); | ||
116 | } | ||
117 | |||
118 | } // namespace efsw | ||