| 1 | /* |
| 2 | Copyright (C) 2001-2006, William Joseph. |
| 3 | All Rights Reserved. |
| 4 | |
| 5 | This file is part of GtkRadiant. |
| 6 | |
| 7 | GtkRadiant is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | GtkRadiant 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 GtkRadiant; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #if !defined( INCLUDED_IFILESYSTEM_H ) |
| 23 | #define INCLUDED_IFILESYSTEM_H |
| 24 | |
| 25 | #include <cstddef> |
| 26 | #include "generic/constant.h" |
| 27 | #include "generic/callback.h" |
| 28 | |
| 29 | typedef Callback<void(const char*)> ArchiveNameCallback; |
| 30 | typedef Callback<void(const char*)> FileNameCallback; |
| 31 | |
| 32 | class ArchiveFile; |
| 33 | class ArchiveTextFile; |
| 34 | class Archive; |
| 35 | |
| 36 | class ModuleObserver; |
| 37 | |
| 38 | typedef struct _GSList GSList; |
| 39 | |
| 40 | /// The Virtual File System. |
| 41 | class VirtualFileSystem |
| 42 | { |
| 43 | public: |
| 44 | INTEGER_CONSTANT( Version, 1 ); |
| 45 | STRING_CONSTANT( Name, "VFS" ); |
| 46 | |
| 47 | /// \brief Adds a root search \p path. |
| 48 | /// Called before \c initialise. |
| 49 | virtual void initDirectory( const char *path ) = 0; |
| 50 | /// \brief Initialises the filesystem. |
| 51 | /// Called after all root search paths have been added. |
| 52 | virtual void initialise() = 0; |
| 53 | /// \brief Clear the filesystem if supported |
| 54 | virtual void clear() = 0; |
| 55 | /// \brief Reload the filesystem if supported |
| 56 | virtual void refresh() = 0; |
| 57 | /// \brief Shuts down the filesystem. |
| 58 | virtual void shutdown() = 0; |
| 59 | |
| 60 | /// \brief Returns the file identified by \p filename opened in binary mode, or 0 if not found. |
| 61 | /// The caller must \c release() the file returned if it is not 0. |
| 62 | virtual ArchiveFile* openFile( const char* filename ) = 0; |
| 63 | /// \brief Returns the file identified by \p filename opened in text mode, or 0 if not found. |
| 64 | /// The caller must \c release() the file returned if it is not 0. |
| 65 | virtual ArchiveTextFile* openTextFile( const char* filename ) = 0; |
| 66 | |
| 67 | /// \brief Opens the file identified by \p filename and reads it into \p buffer, or sets *\p buffer to 0 if not found. |
| 68 | /// Returns the size of the buffer allocated, or undefined value if *\p buffer is 0; |
| 69 | /// The caller must free the allocated buffer by calling \c freeFile |
| 70 | /// \deprecated Deprecated - use \c openFile. |
| 71 | virtual std::size_t loadFile( const char *filename, void **buffer ) = 0; |
| 72 | /// \brief Frees the buffer returned by \c loadFile. |
| 73 | /// \deprecated Deprecated. |
| 74 | virtual void freeFile( void *p ) = 0; |
| 75 | |
| 76 | /// \brief Calls \p callback for each directory under \p basedir. |
| 77 | virtual void forEachDirectory( const char* basedir, const FileNameCallback& callback, std::size_t depth = 1 ) = 0; |
| 78 | /// \brief Calls \p callback for each file under \p basedir matching \p extension. |
| 79 | /// Use "*" as \p extension to match all file extensions. |
| 80 | virtual void forEachFile( const char* basedir, const char* extension, const FileNameCallback& callback, std::size_t depth = 1 ) = 0; |
| 81 | |
| 82 | /// \brief Returns a list containing the relative names of all the directories under \p basedir. |
| 83 | /// The caller must free the returned list by calling \c clearFileDirList; |
| 84 | /// \deprecated Deprecated - use \c forEachDirectory. |
| 85 | virtual GSList* getDirList( const char *basedir ) = 0; |
| 86 | /// \brief Returns a list containing the relative names of the files under \p basedir (\p extension can be "*" for all files). |
| 87 | /// The caller must free the returned list by calling \c clearFileDirList. |
| 88 | /// \deprecated Deprecated - use \c forEachFile. |
| 89 | virtual GSList* getFileList( const char *basedir, const char *extension ) = 0; |
| 90 | /// \brief Frees the \p list returned from \c getDirList or \c getFileList. |
| 91 | /// \deprecated Deprecated. |
| 92 | virtual void clearFileDirList( GSList **list ) = 0; |
| 93 | |
| 94 | /// \brief Returns the absolute filename for a relative \p name, or "" if not found. |
| 95 | virtual const char* findFile( const char* name ) = 0; |
| 96 | /// \brief Returns the filesystem root for an absolute \p name, or "" if not found. |
| 97 | /// This can be used to convert an absolute name to a relative name. |
| 98 | virtual const char* findRoot( const char* name ) = 0; |
| 99 | |
| 100 | /// \brief Attach an \p observer whose realise() and unrealise() methods will be called when the filesystem is initialised or shut down. |
| 101 | virtual void attach( ModuleObserver& observer ) = 0; |
| 102 | /// \brief Detach an \p observer previously-attached by calling \c attach. |
| 103 | virtual void detach( ModuleObserver& observer ) = 0; |
| 104 | |
| 105 | virtual Archive* getArchive( const char* archiveName, bool pakonly = true ) = 0; |
| 106 | virtual void forEachArchive( const ArchiveNameCallback& callback, bool pakonly = true, bool reverse = false ) = 0; |
| 107 | }; |
| 108 | |
| 109 | #include "modulesystem.h" |
| 110 | |
| 111 | template<typename Type> |
| 112 | class GlobalModule; |
| 113 | typedef GlobalModule<VirtualFileSystem> GlobalFileSystemModule; |
| 114 | |
| 115 | template<typename Type> |
| 116 | class GlobalModuleRef; |
| 117 | typedef GlobalModuleRef<VirtualFileSystem> GlobalFileSystemModuleRef; |
| 118 | |
| 119 | inline VirtualFileSystem& GlobalFileSystem(){ |
| 120 | return GlobalFileSystemModule::getTable(); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /// \deprecated Use \c openFile. |
| 125 | inline int vfsLoadFile( const char* filename, void** buffer, int index = 0 ){ |
| 126 | return static_cast<int>( GlobalFileSystem().loadFile( filename, buffer ) ); |
| 127 | } |
| 128 | |
| 129 | /// \deprecated Deprecated. |
| 130 | inline void vfsFreeFile( void* p ){ |
| 131 | GlobalFileSystem().freeFile( p ); |
| 132 | } |
| 133 | |
| 134 | #endif |