aboutsummaryrefslogtreecommitdiff
path: root/engines/sword25/kernel/filesystemutil_win32.cpp
blob: c1f8eed74d76e7b9d43972fe91f6523bc2e1f26a (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
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
// -----------------------------------------------------------------------------
// This file is part of Broken Sword 2.5
// Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsd�rfer
//
// Broken Sword 2.5 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 2 of the License, or
// (at your option) any later version.
//
// Broken Sword 2.5 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 Broken Sword 2.5; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
// Includes
// -----------------------------------------------------------------------------

#include "sword25/kernel/filesystemutil.h"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <ShlObj.h>
#include <memory.h>

using namespace std;

#define BS_LOG_PREFIX "FILESYSTEMUTILWIN32"

// -----------------------------------------------------------------------------
// Konstanten und Hilfsfunktionen
// -----------------------------------------------------------------------------

namespace
{
	const char * DIRECTORY_NAME = "Broken Sword 2.5";

	// -------------------------------------------------------------------------

	string GetAbsolutePath(const string & Path)
	{
		char Buffer[MAX_PATH];
		if (!::GetFullPathNameA(Path.c_str(), MAX_PATH, Buffer, 0))
		{
			// Bei der Ausf�hrung von GetFullPathNameA() ist ein Fehler aufgetreten.
			// Wir k�nnen an dieser Stelle nichts andere machen, als einen leeren String zur�ckzugeben.
			BS_LOG_ERRORLN("A call to GetFullPathNameA() failed.");
			return "";
		}

		// Ergebnis zur�ckgeben.
		return string(Buffer);
	}
}

// -----------------------------------------------------------------------------
// Klassendefinition
// -----------------------------------------------------------------------------

class BS_FileSystemUtilWin32 : public BS_FileSystemUtil
{
public:
	virtual string GetUserdataDirectory()
	{
		// Die C++ Dateisystemfunktionen k�nnen leider nicht mit Unicode-Dateinamen umgehen.
		// F�r uns ist das problematisch, wenn wir in das %APPDATA%-Verzeichnis eines Benutzers zugreifen wollen,
		// dessen Name Unicode-Zeichen enth�lt, die sich mit der aktuellen Codepage nicht als ANSI-String darstellen 
		// lassen.
		// Wir behelfen uns damit, dass wir das Verzeichnis als Unicode-String abfragen, in die kurze
		// Verzeichnisdarstellung konvertieren und das Ergebnis in einen ANSI-String konvertieren.
		// Kurze Dateinamen sollten keine Unicode-Zeichen enthalten, ich habe allerdings keine offizielle Aussage dazu
		// gefunden.

		WCHAR PathBuffer[MAX_PATH];

		// Das %APPDATA%-Verzeichnis erfragen.
		if (::SHGetSpecialFolderPathW(0, PathBuffer, CSIDL_APPDATA, FALSE) == FALSE)
		{
			BS_LOG_ERRORLN("SHGetSpecialFolderPathW() failed");
			return "";
		}

		// Die kurze Variante des Verzeichnisses erfragen.
		if (::GetShortPathNameW(PathBuffer, PathBuffer, MAX_PATH) == 0)
		{
			BS_LOG_ERRORLN("GetShortPathNameW() failed");
			return "";
		}

		// Die Verzeichnisangabe in einen ANSI-String konvertieren.
		char AnsiPathBuffer[MAX_PATH];
		BOOL UsedDefaultChar = FALSE;
		if (::WideCharToMultiByte(CP_ACP, 0, PathBuffer, -1, AnsiPathBuffer, MAX_PATH, 0, &UsedDefaultChar) == 0)
		{
			BS_LOG_ERRORLN("WideCharToMultiByte() failed");
			return "";
		}

		// Falls bei der Konvertierung ein zum Einsatz kam, ist das Ergebnis nicht eindeutig und damit nicht
		// verwendbar.
		if (UsedDefaultChar)
		{
			BS_LOG_ERRORLN("Conversion from unicode to ANSI is ambiguous.");
			return "";
		}

		// Verzeichnis zur�ckgeben.
		return string(AnsiPathBuffer) + "\\" + DIRECTORY_NAME;
	}

	virtual string GetPathSeparator()
	{
		return string("\\");
	}

	virtual uint64_t GetFileSize(const std::string & Filename)
	{
		WIN32_FILE_ATTRIBUTE_DATA fileAttributeData;
		// Dateiattribute einlesen.
		if (::GetFileAttributesExA(Filename.c_str(), GetFileExInfoStandard, &fileAttributeData) != 0)
		{
			// Die Dateigr��e wird von Windows in zwei 32-Bit Zahlen angegeben. Diese werden an dieser Stelle in eine 64-Bit Zahl umgewandelt.
			uint64_t fileSize = fileAttributeData.nFileSizeHigh;
			fileSize <<= 32;
			fileSize |= fileAttributeData.nFileSizeLow;
			return fileSize;
		}
		else
		{
			return -1;
		}	
	}

	virtual time_t GetFileTime(const std::string & Filename)
	{
		WIN32_FILE_ATTRIBUTE_DATA fileAttributeData;
		if (::GetFileAttributesExA(Filename.c_str(), GetFileExInfoStandard, &fileAttributeData) != 0)
		{
			__int64 timestamp;
			memcpy(&timestamp, &fileAttributeData.ftLastWriteTime, sizeof(FILETIME));
			return (timestamp - 0x19DB1DED53E8000) / 10000000; 
		}
		else
		{
			return 0;
		}
	}

	virtual bool FileExists(const std::string & Filename)
	{
		return ::GetFileAttributesA(Filename.c_str()) != INVALID_FILE_ATTRIBUTES;
	}

	// Windows.h enth�lt ein Makro mit dem Namen CreateDirectory. Dieses muss entfernt werden bevor die Definition von
	// unserem CreateDirectory() folgt.
	#ifdef CreateDirectory
		#undef CreateDirectory
	#endif

	virtual bool CreateDirectory(const string & DirectoryName)
	{
		return CreateDirectoryRecursive(GetAbsolutePath(DirectoryName));
	}

	virtual vector<string> GetFilesInDirectory(const std::string & Directory)
	{
		vector<string> Result;

		// Suchstring erstellen, dabei muss der leere String (aktuelles Verzeichnis) gesondert behandelt werden.
		string SearchPattern;
		if (Directory.empty())
			SearchPattern = "*";
		else
			SearchPattern = Directory + "\\*";

		// Die erste Datei suchen.
		WIN32_FIND_DATAA FindData;
		HANDLE FindHandle = ::FindFirstFileA(SearchPattern.c_str(), &FindData);

		while (FindHandle != INVALID_HANDLE_VALUE)
		{
			// Verzeichnisse ignorieren.
			// Beim erstellen des Ergebnispfades muss wieder der Sonderfall der leeren Verzeichnisangabe ber�cksichtigt werden.
			if (!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
				Result.push_back(FindData.cFileName);

			// Solange weitermachen, bis keine Dateien mehr gefunden werden.
			if (FindNextFileA(FindHandle, &FindData) == 0)
			{
				FindClose(&FindHandle);
				break;
			}
		}

		return Result;
	}

private:
	bool CreateDirectoryRecursive(string & DirectoryName)
	{
		// 
		// http://www.codeguru.com/Cpp/W-P/files/article.php/c4439/
		// (c) Assaf Tzur-El
		//

		DWORD attr;
		int pos;
		bool result = true;

		// Check for trailing slash:
		pos = DirectoryName.find_last_of("\\");
		if (DirectoryName.length() == pos + 1)  // last character is "\"
		{
			DirectoryName.resize(pos);
		}

		// Look for existing object:
		attr = ::GetFileAttributesA(DirectoryName.c_str());
		if (0xFFFFFFFF == attr)  // doesn't exist yet - create it!
		{
			pos = DirectoryName.find_last_of("\\");
			if (0 < pos)
			{
				// Create parent dirs:
				result = CreateDirectoryRecursive(DirectoryName.substr(0, pos));
			}
			// Create node:
			result = result && ::CreateDirectoryA(DirectoryName.c_str(), NULL);
		}
		else if (!(FILE_ATTRIBUTE_DIRECTORY & attr))
		{  // object already exists, but is not a dir
			::SetLastError(ERROR_FILE_EXISTS);
			result = false;
		}

		return result;
	}
};

// -----------------------------------------------------------------------------
// Singleton-Methode der Elternklasse
// Hiermit wird sichergestellt, dass wenn immer diese Datei kompiliert wird,
// die Singleton-Methode der Oberklasse diese Klasse instanziiert.
// Unterscheidung zwischen den Plattformen wird so nur durch Linken gegen andere
// Dateien realisiert.
// -----------------------------------------------------------------------------

BS_FileSystemUtil & BS_FileSystemUtil::GetInstance()
{
	static BS_FileSystemUtilWin32 Instance;
	return Instance;
}