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 #ifndef COMMON_FILE_H 24 #define COMMON_FILE_H 25 26 #include "common/scummsys.h" 27 #include "common/fs.h" 28 #include "common/noncopyable.h" 29 #include "common/str.h" 30 #include "common/stream.h" 31 32 namespace Common { 33 34 class Archive; 35 36 /** 37 * TODO: vital to document this core class properly!!! For both users and implementors 38 */ 39 class File : public SeekableReadStream, public NonCopyable { 40 protected: 41 /** File handle to the actual file; 0 if no file is open. */ 42 SeekableReadStream *_handle; 43 44 /** The name of this file, kept for debugging purposes. */ 45 String _name; 46 47 public: 48 File(); 49 virtual ~File(); 50 51 /** 52 * Checks if a given file exists in any of the current default paths, 53 * as defined by SearchMan. 54 * 55 * @param filename the file to check for 56 * @return true if the file exists, false otherwise 57 */ 58 static bool exists(const String &filename); 59 60 /** 61 * Try to open the file with the given filename, by searching SearchMan. 62 * @note Must not be called if this file already is open (i.e. if isOpen returns true). 63 * 64 * @param filename the name of the file to open 65 * @return true if file was opened successfully, false otherwise 66 */ 67 virtual bool open(const String &filename); 68 69 /** 70 * Try to open the file with the given filename from within the given archive. 71 * @note Must not be called if this file already is open (i.e. if isOpen returns true). 72 * 73 * @param filename the name of the file to open 74 * @param archive the archive in which to search for the file 75 * @return true if file was opened successfully, false otherwise 76 */ 77 virtual bool open(const String &filename, Archive &archive); 78 79 /** 80 * Try to open the file corresponding to the give node. Will check whether the 81 * node actually refers to an existing file (and not a directory), and handle 82 * those cases gracefully. 83 * @note Must not be called if this file already is open (i.e. if isOpen returns true). 84 * 85 * @param node the node to consider. 86 * @return true if file was opened successfully, false otherwise 87 */ 88 virtual bool open(const FSNode &node); 89 90 /** 91 * Try to 'open' the given stream. That is, we just wrap around it, and if stream 92 * is a NULL pointer, we gracefully treat this as if opening failed. 93 * @note Must not be called if this file already is open (i.e. if isOpen returns true). 94 * 95 * @param stream a pointer to a SeekableReadStream, or 0 96 * @param name a string describing the 'file' corresponding to stream 97 * @return true if stream was non-zero, false otherwise 98 */ 99 virtual bool open(SeekableReadStream *stream, const String &name); 100 101 /** 102 * Close the file, if open. 103 */ 104 virtual void close(); 105 106 /** 107 * Checks if the object opened a file successfully. 108 * 109 * @return: true if any file is opened, false otherwise. 110 */ 111 bool isOpen() const; 112 113 /** 114 * Returns the filename of the opened file for debugging purposes. 115 * 116 * @return: the filename 117 */ getName()118 const char *getName() const { return _name.c_str(); } 119 120 bool err() const; // implement abstract Stream method 121 void clearErr(); // implement abstract Stream method 122 bool eos() const; // implement abstract SeekableReadStream method 123 124 int32 pos() const; // implement abstract SeekableReadStream method 125 int32 size() const; // implement abstract SeekableReadStream method 126 bool seek(int32 offs, int whence = SEEK_SET); // implement abstract SeekableReadStream method 127 uint32 read(void *dataPtr, uint32 dataSize); // implement abstract SeekableReadStream method 128 }; 129 130 131 /** 132 * TODO: document this class 133 * 134 * Some design ideas: 135 * - automatically drop all files into dumps/ dir? Might not be desired in all cases 136 */ 137 class DumpFile : public WriteStream, public NonCopyable { 138 protected: 139 /** File handle to the actual file; 0 if no file is open. */ 140 WriteStream *_handle; 141 142 public: 143 DumpFile(); 144 virtual ~DumpFile(); 145 146 virtual bool open(const String &filename, bool createPath = false); 147 virtual bool open(const FSNode &node); 148 149 virtual void close(); 150 151 /** 152 * Checks if the object opened a file successfully. 153 * 154 * @return: true if any file is opened, false otherwise. 155 */ 156 bool isOpen() const; 157 158 bool err() const; 159 void clearErr(); 160 161 virtual uint32 write(const void *dataPtr, uint32 dataSize); 162 163 virtual bool flush(); 164 165 virtual int32 pos() const; 166 }; 167 168 } // End of namespace Common 169 170 #endif 171