ITAFileSystemUtils.cpp 4.55 KB
Newer Older
js908001's avatar
js908001 committed
1
#include <ITAFileSystemUtils.h>
Jonas Stienen's avatar
Jonas Stienen committed
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

#if defined(WIN32) || defined(PLATFORM_X64)
#include <windows.h>
#endif

#include <sys/stat.h>

bool doesPathExist(const std::string& sPath) {
	// Inspiration by FOX
#if !defined(WIN32) && !defined(PLATFORM_X64)
	struct stat status;
	return !sPath.empty() && (::stat(sPath.c_str(), &status)==0);
#else
	return !sPath.empty() && (GetFileAttributesA(sPath.c_str()) != 0xFFFFFFFF);
#endif
}

bool doesFileExist(const std::string& sPath) {
	return doesPathExist(sPath) && isFile(sPath);
}

bool doesDirectoryExist(const std::string& sPath) {
	return doesPathExist(sPath) && isDirectory(sPath);
}

bool isFile(const std::string& sPath) {
	// Inspiration by FOX
#if !defined(WIN32) && !defined(PLATFORM_X64)
	struct stat status;
	return !sPath.empty() && (::stat(sPath.c_str(), &status)==0) && S_ISREG(status.st_mode);
#else
	DWORD atts;
	return !sPath.empty() && ((atts=GetFileAttributesA(sPath.c_str())) != 0xFFFFFFFF) && !(atts & FILE_ATTRIBUTE_DIRECTORY);
#endif
}

bool isDirectory(const std::string& sPath) {
#if !defined(WIN32) && !defined(PLATFORM_X64)
	struct stat status;
	return !sPath.empty() && (::stat(sPath.c_str(), &status)==0) && S_ISDIR(status.st_mode);
#else
	DWORD atts;
	return !sPath.empty() && ((atts=GetFileAttributesA(sPath.c_str())) != 0xFFFFFFFF) && (atts & FILE_ATTRIBUTE_DIRECTORY);
#endif
}

bool makeDirectory(const std::string& sPath) {
#if !defined(WIN32) && !defined(PLATFORM_X64)
	// TODO: Mode?
	return mkdir(sPath.c_str(), 0)==0;
#else
	return CreateDirectory(sPath.c_str(), NULL) != 0;
#endif
}

std::string getFilenameSuffix(const std::string& sFilename) {
	int i = (int) sFilename.find_last_of('.');
	return ((i == -1) || (i==((int) sFilename.length()-1)) ? "" : sFilename.substr(i+1, (int) sFilename.length()-i-1));
}

std::string stripFilenameSuffix(const std::string& sFilename) {
	size_t i = sFilename.find_last_of('.');
	return ((i == -1) || (i==(sFilename.length()-1)) ? "" : sFilename.substr(0, i));
}

std::string getFilenameFromPath(const std::string& sPath) {
	size_t i = 0;
	if (sPath.find(PATH_SEPARATOR) != std::string::npos) {
		i = sPath.find_last_of(PATH_SEPARATOR) + 1;
		return sPath.substr(i, (sPath.length()-1));
	} else {
		return sPath;
	}

}

std::string getDirectoryFromPath(const std::string& sPath) {
	size_t i = sPath.find_last_of(PATH_SEPARATOR);
	return ((i == -1) || (i==(sPath.length()-1)) ? "" : sPath.substr(0, i));
}

int64_t getFileSize(const std::string& sFilename) {
	if (sFilename.empty()) 
		return -1;
#ifdef LINUX
	// Linux
	struct stat64 statinfo;
	if (stat64(sFilename.c_str(), &statinfo) != 0)
		return -1;
	return (int64_t) statinfo.st_size;
#endif
#ifdef WIN32
	// Windows
	struct _stat64 statinfo;
	if (_stat64(sFilename.c_str(), &statinfo) != 0) 
		return -1;
	return (int64_t) statinfo.st_size;
#endif

	// LINUX oder WIN32 nicht definiert
	return -1;
}

std::string combinePath(std::string p1, std::string p2, std::string p3, std::string p4) {
	std::string s = p1;
	if (!p2.empty()) {
		if (!s.empty()) s += PATH_SEPARATOR;
		s += p2;
	}

	if (!p3.empty()) {
		if (!s.empty()) s += PATH_SEPARATOR;
		s += p3;
	}

	if (!p4.empty()) {
		if (!s.empty()) s += PATH_SEPARATOR;
		s += p4;
	}

	return correctPath(s);
}

std::string correctPath(const std::string& sPath) {
	std::string s(sPath);
	if (PATH_SEPARATOR == '/') {
		for (std::string::size_type i=0; i<s.length(); i++)
			// Backslashes in Slashes
			if (s[i] == '\\') s[i] = '/';
	} else {
		for (std::string::size_type i=0; i<s.length(); i++)
			// Backslashes in Slashes
			if (s[i] == '/') s[i] = '\\';
	}

	// Doppelte Slashes/Backslashes entfernen
	std::string t = std::string(2, PATH_SEPARATOR);
	size_t pos = 0;
	while ( (pos = s.find(t, pos)) != std::string::npos ) {
		s.replace( pos, 2, std::string(1,PATH_SEPARATOR) );
        pos++;
	}

	return s;
}

bool listFilesInDirectory(const std::string& sPath, std::vector<std::string>& vsFilenames) {
	vsFilenames.clear();
	std::string sCorectedPath = correctPath(sPath);

	if (!isDirectory(sCorectedPath))
		return false;

	std::string sFilename;
	// Inspiration by FOX
#if !defined(WIN32) && !defined(PLATFORM_X64)
	// Linux implementation
	
#else
	// Windows implementation
	WIN32_FIND_DATA oData;
	HANDLE h;

	std::string sPathWildcard = sCorectedPath + std::string("\\*");
	h = FindFirstFile(sPathWildcard.c_str(), &oData);
	
	if (h == INVALID_HANDLE_VALUE) 
		return false;

	do {
		sFilename = oData.cFileName;
		if (!(oData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
			vsFilenames.push_back(sFilename);
	} while (FindNextFile(h, &oData) != 0);

	DWORD dwError = GetLastError();

	FindClose(h);

#endif

	return true;
}