aboutsummaryrefslogtreecommitdiff
path: root/src/3rdParty/efsw/FileSystem.cpp
blob: 867f120e8e58abe31eb68139625f7c0a96a78184 (plain)
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
#include <efsw/FileSystem.hpp>
#include <efsw/platform/platformimpl.hpp>

#if EFSW_OS == EFSW_OS_MACOSX
#include <CoreFoundation/CoreFoundation.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 );

	char c_str[255 + 1];
	CFStringGetCString( cfMutable, c_str, sizeof( c_str ) - 1, kCFStringEncodingUTF8 );

	CFRelease( cfStringRef );
	CFRelease( cfMutable );

	return std::string( c_str );
#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();
}

} // namespace efsw