From 878f5a49f771d9808adad287f217defeee2caf7a Mon Sep 17 00:00:00 2001 From: Paul Gilbert Date: Sat, 31 Jul 2010 07:52:54 +0000 Subject: SWORD25: Implemented ScummVM version of BS_FileSystemUtil interface svn-id: r53186 --- engines/sword25/kernel/filesystemutil.cpp | 131 ++++++++++++ engines/sword25/kernel/filesystemutil.h | 10 +- engines/sword25/kernel/filesystemutil_win32.cpp | 256 ------------------------ 3 files changed, 138 insertions(+), 259 deletions(-) create mode 100755 engines/sword25/kernel/filesystemutil.cpp delete mode 100755 engines/sword25/kernel/filesystemutil_win32.cpp (limited to 'engines/sword25/kernel') diff --git a/engines/sword25/kernel/filesystemutil.cpp b/engines/sword25/kernel/filesystemutil.cpp new file mode 100755 index 0000000000..307758c7d0 --- /dev/null +++ b/engines/sword25/kernel/filesystemutil.cpp @@ -0,0 +1,131 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program 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. + + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +// ----------------------------------------------------------------------------- +// Includes +// ----------------------------------------------------------------------------- + +#include "common/config-manager.h" +#include "common/fs.h" +#include "common/savefile.h" +#include "common/system.h" +#include "sword25/kernel/filesystemutil.h" + +namespace Sword25 { + +#define BS_LOG_PREFIX "FILESYSTEMUTIL" + +// ----------------------------------------------------------------------------- +// Constants and utility functions +// ----------------------------------------------------------------------------- + +Common::String GetAbsolutePath(const Common::String &Path) { + Common::FSNode node(Path); + + if (!node.exists()) { + // An error has occurred finding the node + // We can do nothing at this pointer than return an empty string + BS_LOG_ERRORLN("A call to GetAbsolutePath failed."); + return ""; + } + + // Return the result + return node.getPath(); +} + +// ----------------------------------------------------------------------------- +// Class definitions +// ----------------------------------------------------------------------------- + +class BS_FileSystemUtilScummVM : public BS_FileSystemUtil { +public: + virtual Common::String GetUserdataDirectory() { + Common::String path = ConfMan.get("savepath"); + + if (path.empty()) { + BS_LOG_ERRORLN("No save path has been defined"); + return ""; + } + + // Return the path + return path; + } + + virtual Common::String GetPathSeparator() { + return Common::String("//"); + } + + virtual int64 GetFileSize(const Common::String &Filename) { + Common::FSNode node(Filename); + + // If the file does not exist, return -1 as a result + if (!node.exists()) + return -1; + + // Get the size of the file and return it + Common::File f; + f.open(node); + uint32 size = f.size(); + f.close(); + + return size; + } + + virtual time_t GetFileTime(const Common::String &Filename) { + // TODO: There isn't any way in ScummVM to get a file's modified date/time. We will need to check + // what code makes use of it. If it's only the save game code, for example, we may be able to + // encode the date/time inside the savegame files themselves. + return 0; + } + + virtual bool FileExists(const Common::String &Filename) { + Common::File f; + return f.exists(Filename); + } + + virtual bool CreateDirectory(const Common::String &DirectoryName) { + // ScummVM doesn't support creating folders, so this is only a stub + BS_LOG_ERRORLN("CreateDirectory method called"); + return false; + } + + virtual Common::StringArray GetFilesInDirectory(const Common::String &Directory) { + Common::SaveFileManager *sfm = g_system->getSavefileManager(); + Common::StringArray filenames = sfm->listSavefiles("*"); + sort(filenames.begin(), filenames.end()); + return filenames; + } +}; + +// ----------------------------------------------------------------------------- +// Singleton method of parent class +// ----------------------------------------------------------------------------- + +BS_FileSystemUtil &BS_FileSystemUtil::GetInstance() { + static BS_FileSystemUtilScummVM Instance; + return Instance; +} + +} // End of namespace Sword25 diff --git a/engines/sword25/kernel/filesystemutil.h b/engines/sword25/kernel/filesystemutil.h index 2c650e2af2..922139acd7 100755 --- a/engines/sword25/kernel/filesystemutil.h +++ b/engines/sword25/kernel/filesystemutil.h @@ -37,15 +37,17 @@ // ----------------------------------------------------------------------------- #include "common/str.h" +#include "common/str-array.h" #include "sword25/kernel/common.h" #include "sword25/kernel/bs_stdint.h" +namespace Sword25 { + // ----------------------------------------------------------------------------- // Class definitions // ----------------------------------------------------------------------------- -class BS_FileSystemUtil -{ +class BS_FileSystemUtil { public: static BS_FileSystemUtil &GetInstance(); virtual ~BS_FileSystemUtil() {}; @@ -66,7 +68,7 @@ public: * @return Returns the size of the specified file. If the size could not be * determined, or the file does not exist, returns -1 */ - virtual uint64_t GetFileSize(const Common::String &Filename) = 0; + virtual int64 GetFileSize(const Common::String &Filename) = 0; /** * @param Filename The path to a file. * @return Returns the timestamp of the specified file. On error it returns 0 @@ -94,4 +96,6 @@ public: virtual Common::StringArray GetFilesInDirectory(const Common::String &Path) = 0; }; +} // End of namespace Sword25 + #endif diff --git a/engines/sword25/kernel/filesystemutil_win32.cpp b/engines/sword25/kernel/filesystemutil_win32.cpp deleted file mode 100755 index c1f8eed74d..0000000000 --- a/engines/sword25/kernel/filesystemutil_win32.cpp +++ /dev/null @@ -1,256 +0,0 @@ -// ----------------------------------------------------------------------------- -// 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 -#include -#include - -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(×tamp, &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 GetFilesInDirectory(const std::string & Directory) - { - vector 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; -} -- cgit v1.2.3