1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #if defined(WIN32)
24
25 // Disable symbol overrides so that we can use system headers.
26 #define FORBIDDEN_SYMBOL_ALLOW_ALL
27
28 #include "backends/fs/windows/windows-fs.h"
29 #include "backends/fs/stdiostream.h"
30
31 // F_OK, R_OK and W_OK are not defined under MSVC, so we define them here
32 // For more information on the modes used by MSVC, check:
33 // http://msdn2.microsoft.com/en-us/library/1w06ktdy(VS.80).aspx
34 #ifndef F_OK
35 #define F_OK 0
36 #endif
37
38 #ifndef R_OK
39 #define R_OK 4
40 #endif
41
42 #ifndef W_OK
43 #define W_OK 2
44 #endif
45
exists() const46 bool WindowsFilesystemNode::exists() const {
47 return _access(_path.c_str(), F_OK) == 0;
48 }
49
isReadable() const50 bool WindowsFilesystemNode::isReadable() const {
51 return _access(_path.c_str(), R_OK) == 0;
52 }
53
isWritable() const54 bool WindowsFilesystemNode::isWritable() const {
55 return _access(_path.c_str(), W_OK) == 0;
56 }
57
addFile(AbstractFSList & list,ListMode mode,const char * base,bool hidden,WIN32_FIND_DATA * find_data)58 void WindowsFilesystemNode::addFile(AbstractFSList &list, ListMode mode, const char *base, bool hidden, WIN32_FIND_DATA* find_data) {
59 WindowsFilesystemNode entry;
60 char *asciiName = toAscii(find_data->cFileName);
61 bool isDirectory;
62
63 // Skip local directory (.) and parent (..)
64 if (!strcmp(asciiName, ".") || !strcmp(asciiName, ".."))
65 return;
66
67 // Skip hidden files if asked
68 if ((find_data->dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) && !hidden)
69 return;
70
71 isDirectory = (find_data->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? true : false);
72
73 if ((!isDirectory && mode == Common::FSNode::kListDirectoriesOnly) ||
74 (isDirectory && mode == Common::FSNode::kListFilesOnly))
75 return;
76
77 entry._isDirectory = isDirectory;
78 entry._displayName = asciiName;
79 entry._path = base;
80 entry._path += asciiName;
81 if (entry._isDirectory)
82 entry._path += "\\";
83 entry._isValid = true;
84 entry._isPseudoRoot = false;
85
86 list.push_back(new WindowsFilesystemNode(entry));
87 }
88
toAscii(TCHAR * str)89 char* WindowsFilesystemNode::toAscii(TCHAR *str) {
90 #ifndef UNICODE
91 return (char *)str;
92 #else
93 static char asciiString[MAX_PATH];
94 WideCharToMultiByte(CP_ACP, 0, str, _tcslen(str) + 1, asciiString, sizeof(asciiString), NULL, NULL);
95 return asciiString;
96 #endif
97 }
98
toUnicode(const char * str)99 const TCHAR* WindowsFilesystemNode::toUnicode(const char *str) {
100 #ifndef UNICODE
101 return (const TCHAR *)str;
102 #else
103 static TCHAR unicodeString[MAX_PATH];
104 MultiByteToWideChar(CP_ACP, 0, str, strlen(str) + 1, unicodeString, sizeof(unicodeString) / sizeof(TCHAR));
105 return unicodeString;
106 #endif
107 }
108
WindowsFilesystemNode()109 WindowsFilesystemNode::WindowsFilesystemNode() {
110 _isDirectory = true;
111 #ifndef _WIN32_WCE
112 // Create a virtual root directory for standard Windows system
113 _isValid = false;
114 _path = "";
115 _isPseudoRoot = true;
116 #else
117 _displayName = "Root";
118 // No need to create a pseudo root directory on Windows CE
119 _isValid = true;
120 _path = "\\";
121 _isPseudoRoot = false;
122 #endif
123 }
124
WindowsFilesystemNode(const Common::String & p,const bool currentDir)125 WindowsFilesystemNode::WindowsFilesystemNode(const Common::String &p, const bool currentDir) {
126 if (currentDir) {
127 char path[MAX_PATH];
128 GetCurrentDirectory(MAX_PATH, path);
129 _path = path;
130 } else {
131 assert(p.size() > 0);
132 _path = p;
133 }
134
135 _displayName = lastPathComponent(_path, '\\');
136
137 // Check whether it is a directory, and whether the file actually exists
138 DWORD fileAttribs = GetFileAttributes(toUnicode(_path.c_str()));
139
140 if (fileAttribs == INVALID_FILE_ATTRIBUTES) {
141 _isDirectory = false;
142 _isValid = false;
143 } else {
144 _isDirectory = ((fileAttribs & FILE_ATTRIBUTE_DIRECTORY) != 0);
145 _isValid = true;
146 // Add a trailing slash, if necessary.
147 if (_isDirectory && _path.lastChar() != '\\') {
148 _path += '\\';
149 }
150 }
151 _isPseudoRoot = false;
152 }
153
getChild(const Common::String & n) const154 AbstractFSNode *WindowsFilesystemNode::getChild(const Common::String &n) const {
155 assert(_isDirectory);
156
157 // Make sure the string contains no slashes
158 assert(!n.contains('/'));
159
160 Common::String newPath(_path);
161 if (_path.lastChar() != '\\')
162 newPath += '\\';
163 newPath += n;
164
165 return new WindowsFilesystemNode(newPath, false);
166 }
167
getChildren(AbstractFSList & myList,ListMode mode,bool hidden) const168 bool WindowsFilesystemNode::getChildren(AbstractFSList &myList, ListMode mode, bool hidden) const {
169 assert(_isDirectory);
170
171 if (_isPseudoRoot) {
172 #ifndef _WIN32_WCE
173 // Drives enumeration
174 TCHAR drive_buffer[100];
175 GetLogicalDriveStrings(sizeof(drive_buffer) / sizeof(TCHAR), drive_buffer);
176
177 for (TCHAR *current_drive = drive_buffer; *current_drive;
178 current_drive += _tcslen(current_drive) + 1) {
179 WindowsFilesystemNode entry;
180 char drive_name[2];
181
182 drive_name[0] = toAscii(current_drive)[0];
183 drive_name[1] = '\0';
184 entry._displayName = drive_name;
185 entry._isDirectory = true;
186 entry._isValid = true;
187 entry._isPseudoRoot = false;
188 entry._path = toAscii(current_drive);
189 myList.push_back(new WindowsFilesystemNode(entry));
190 }
191 #endif
192 }
193 else {
194 // Files enumeration
195 WIN32_FIND_DATA desc;
196 HANDLE handle;
197 char searchPath[MAX_PATH + 10];
198
199 sprintf(searchPath, "%s*", _path.c_str());
200
201 handle = FindFirstFile(toUnicode(searchPath), &desc);
202
203 if (handle == INVALID_HANDLE_VALUE)
204 return false;
205
206 addFile(myList, mode, _path.c_str(), hidden, &desc);
207
208 while (FindNextFile(handle, &desc))
209 addFile(myList, mode, _path.c_str(), hidden, &desc);
210
211 FindClose(handle);
212 }
213
214 return true;
215 }
216
getParent() const217 AbstractFSNode *WindowsFilesystemNode::getParent() const {
218 assert(_isValid || _isPseudoRoot);
219
220 if (_isPseudoRoot)
221 return 0;
222
223 WindowsFilesystemNode *p = new WindowsFilesystemNode();
224 if (_path.size() > 3) {
225 const char *start = _path.c_str();
226 const char *end = lastPathComponent(_path, '\\');
227
228 p = new WindowsFilesystemNode();
229 p->_path = Common::String(start, end - start);
230 p->_isValid = true;
231 p->_isDirectory = true;
232 p->_displayName = lastPathComponent(p->_path, '\\');
233 p->_isPseudoRoot = false;
234 }
235
236 return p;
237 }
238
createReadStream()239 Common::SeekableReadStream *WindowsFilesystemNode::createReadStream() {
240 return StdioStream::makeFromPath(getPath(), false);
241 }
242
createWriteStream()243 Common::WriteStream *WindowsFilesystemNode::createWriteStream() {
244 return StdioStream::makeFromPath(getPath(), true);
245 }
246
create(bool isDirectoryFlag)247 bool WindowsFilesystemNode::create(bool isDirectoryFlag) {
248 bool success;
249
250 if (isDirectoryFlag) {
251 success = CreateDirectory(toUnicode(_path.c_str()), NULL) != 0;
252 } else {
253 success = CreateFile(toUnicode(_path.c_str()), GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL) != INVALID_HANDLE_VALUE;
254 }
255
256 if (success) {
257 //this piece is copied from constructor, it checks that file exists and detects whether it's a directory
258 DWORD fileAttribs = GetFileAttributes(toUnicode(_path.c_str()));
259 if (fileAttribs != INVALID_FILE_ATTRIBUTES) {
260 _isDirectory = ((fileAttribs & FILE_ATTRIBUTE_DIRECTORY) != 0);
261 _isValid = true;
262 // Add a trailing slash, if necessary.
263 if (_isDirectory && _path.lastChar() != '\\') {
264 _path += '\\';
265 }
266
267 if (_isDirectory != isDirectoryFlag) warning("failed to create %s: got %s", isDirectoryFlag ? "directory" : "file", _isDirectory ? "directory" : "file");
268 return _isDirectory == isDirectoryFlag;
269 }
270
271 warning("WindowsFilesystemNode: Create%s() was a success, but GetFileAttributes() indicates there is no such %s",
272 isDirectoryFlag ? "Directory" : "File", isDirectoryFlag ? "directory" : "file");
273 return false;
274 }
275
276 return false;
277 }
278
279 #endif //#ifdef WIN32
280