IFileSystem.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef IRR_I_FILE_SYSTEM_H_INCLUDED
  5. #define IRR_I_FILE_SYSTEM_H_INCLUDED
  6. #include "IReferenceCounted.h"
  7. #include "IXMLReader.h"
  8. #include "IXMLWriter.h"
  9. #include "IFileArchive.h"
  10. namespace irr
  11. {
  12. namespace video
  13. {
  14. class IVideoDriver;
  15. } // end namespace video
  16. namespace io
  17. {
  18. class IReadFile;
  19. class IWriteFile;
  20. class IFileList;
  21. class IAttributes;
  22. //! The FileSystem manages files and archives and provides access to them.
  23. /** It manages where files are, so that modules which use the the IO do not
  24. need to know where every file is located. A file could be in a .zip-Archive or
  25. as file on disk, using the IFileSystem makes no difference to this. */
  26. class IFileSystem : public virtual IReferenceCounted
  27. {
  28. public:
  29. //! Opens a file for read access.
  30. /** \param filename: Name of file to open.
  31. \return Pointer to the created file interface.
  32. The returned pointer should be dropped when no longer needed.
  33. See IReferenceCounted::drop() for more information. */
  34. virtual IReadFile* createAndOpenFile(const path& filename) =0;
  35. //! Creates an IReadFile interface for accessing memory like a file.
  36. /** This allows you to use a pointer to memory where an IReadFile is requested.
  37. \param memory: A pointer to the start of the file in memory
  38. \param len: The length of the memory in bytes
  39. \param fileName: The name given to this file
  40. \param deleteMemoryWhenDropped: True if the memory should be deleted
  41. along with the IReadFile when it is dropped.
  42. \return Pointer to the created file interface.
  43. The returned pointer should be dropped when no longer needed.
  44. See IReferenceCounted::drop() for more information.
  45. */
  46. virtual IReadFile* createMemoryReadFile(const void* memory, s32 len, const path& fileName, bool deleteMemoryWhenDropped=false) =0;
  47. //! Creates an IReadFile interface for accessing files inside files.
  48. /** This is useful e.g. for archives.
  49. \param fileName: The name given to this file
  50. \param alreadyOpenedFile: Pointer to the enclosing file
  51. \param pos: Start of the file inside alreadyOpenedFile
  52. \param areaSize: The length of the file
  53. \return A pointer to the created file interface.
  54. The returned pointer should be dropped when no longer needed.
  55. See IReferenceCounted::drop() for more information.
  56. */
  57. virtual IReadFile* createLimitReadFile(const path& fileName,
  58. IReadFile* alreadyOpenedFile, long pos, long areaSize) =0;
  59. //! Creates an IWriteFile interface for accessing memory like a file.
  60. /** This allows you to use a pointer to memory where an IWriteFile is requested.
  61. You are responsible for allocating enough memory.
  62. \param memory: A pointer to the start of the file in memory (allocated by you)
  63. \param len: The length of the memory in bytes
  64. \param fileName: The name given to this file
  65. \param deleteMemoryWhenDropped: True if the memory should be deleted
  66. along with the IWriteFile when it is dropped.
  67. \return Pointer to the created file interface.
  68. The returned pointer should be dropped when no longer needed.
  69. See IReferenceCounted::drop() for more information.
  70. */
  71. virtual IWriteFile* createMemoryWriteFile(void* memory, s32 len, const path& fileName, bool deleteMemoryWhenDropped=false) =0;
  72. //! Opens a file for write access.
  73. /** \param filename: Name of file to open.
  74. \param append: If the file already exist, all write operations are
  75. appended to the file.
  76. \return Pointer to the created file interface. 0 is returned, if the
  77. file could not created or opened for writing.
  78. The returned pointer should be dropped when no longer needed.
  79. See IReferenceCounted::drop() for more information. */
  80. virtual IWriteFile* createAndWriteFile(const path& filename, bool append=false) =0;
  81. //! Adds an archive to the file system.
  82. /** After calling this, the Irrlicht Engine will also search and open
  83. files directly from this archive. This is useful for hiding data from
  84. the end user, speeding up file access and making it possible to access
  85. for example Quake3 .pk3 files, which are just renamed .zip files. By
  86. default Irrlicht supports ZIP, PAK, TAR, PNK, and directories as
  87. archives. You can provide your own archive types by implementing
  88. IArchiveLoader and passing an instance to addArchiveLoader.
  89. Irrlicht supports AES-encrypted zip files, and the advanced compression
  90. techniques lzma and bzip2.
  91. \param filename: Filename of the archive to add to the file system.
  92. \param ignoreCase: If set to true, files in the archive can be accessed without
  93. writing all letters in the right case.
  94. \param ignorePaths: If set to true, files in the added archive can be accessed
  95. without its complete path.
  96. \param archiveType: If no specific E_FILE_ARCHIVE_TYPE is selected then
  97. the type of archive will depend on the extension of the file name. If
  98. you use a different extension then you can use this parameter to force
  99. a specific type of archive.
  100. \param password An optional password, which is used in case of encrypted archives.
  101. \param retArchive A pointer that will be set to the archive that is added.
  102. \return True if the archive was added successfully, false if not. */
  103. virtual bool addFileArchive(const path& filename, bool ignoreCase=true,
  104. bool ignorePaths=true,
  105. E_FILE_ARCHIVE_TYPE archiveType=EFAT_UNKNOWN,
  106. const core::stringc& password="",
  107. IFileArchive** retArchive=0) =0;
  108. //! Adds an archive to the file system.
  109. /** After calling this, the Irrlicht Engine will also search and open
  110. files directly from this archive. This is useful for hiding data from
  111. the end user, speeding up file access and making it possible to access
  112. for example Quake3 .pk3 files, which are just renamed .zip files. By
  113. default Irrlicht supports ZIP, PAK, TAR, PNK, and directories as
  114. archives. You can provide your own archive types by implementing
  115. IArchiveLoader and passing an instance to addArchiveLoader.
  116. Irrlicht supports AES-encrypted zip files, and the advanced compression
  117. techniques lzma and bzip2.
  118. If you want to add a directory as an archive, prefix its name with a
  119. slash in order to let Irrlicht recognize it as a folder mount (mypath/).
  120. Using this technique one can build up a search order, because archives
  121. are read first, and can be used more easily with relative filenames.
  122. \param file: Archive to add to the file system.
  123. \param ignoreCase: If set to true, files in the archive can be accessed without
  124. writing all letters in the right case.
  125. \param ignorePaths: If set to true, files in the added archive can be accessed
  126. without its complete path.
  127. \param archiveType: If no specific E_FILE_ARCHIVE_TYPE is selected then
  128. the type of archive will depend on the extension of the file name. If
  129. you use a different extension then you can use this parameter to force
  130. a specific type of archive.
  131. \param password An optional password, which is used in case of encrypted archives.
  132. \param retArchive A pointer that will be set to the archive that is added.
  133. \return True if the archive was added successfully, false if not. */
  134. virtual bool addFileArchive(IReadFile* file, bool ignoreCase=true,
  135. bool ignorePaths=true,
  136. E_FILE_ARCHIVE_TYPE archiveType=EFAT_UNKNOWN,
  137. const core::stringc& password="",
  138. IFileArchive** retArchive=0) =0;
  139. //! Adds an archive to the file system.
  140. /** \param archive: The archive to add to the file system.
  141. \return True if the archive was added successfully, false if not. */
  142. virtual bool addFileArchive(IFileArchive* archive) =0;
  143. //! Get the number of archives currently attached to the file system
  144. virtual u32 getFileArchiveCount() const =0;
  145. //! Removes an archive from the file system.
  146. /** This will close the archive and free any file handles, but will not
  147. close resources which have already been loaded and are now cached, for
  148. example textures and meshes.
  149. \param index: The index of the archive to remove
  150. \return True on success, false on failure */
  151. virtual bool removeFileArchive(u32 index) =0;
  152. //! Removes an archive from the file system.
  153. /** This will close the archive and free any file handles, but will not
  154. close resources which have already been loaded and are now cached, for
  155. example textures and meshes. Note that a relative filename might be
  156. interpreted differently on each call, depending on the current working
  157. directory. In case you want to remove an archive that was added using
  158. a relative path name, you have to change to the same working directory
  159. again. This means, that the filename given on creation is not an
  160. identifier for the archive, but just a usual filename that is used for
  161. locating the archive to work with.
  162. \param filename The archive pointed to by the name will be removed
  163. \return True on success, false on failure */
  164. virtual bool removeFileArchive(const path& filename) =0;
  165. //! Removes an archive from the file system.
  166. /** This will close the archive and free any file handles, but will not
  167. close resources which have already been loaded and are now cached, for
  168. example textures and meshes.
  169. \param archive The archive to remove.
  170. \return True on success, false on failure */
  171. virtual bool removeFileArchive(const IFileArchive* archive) =0;
  172. //! Changes the search order of attached archives.
  173. /**
  174. \param sourceIndex: The index of the archive to change
  175. \param relative: The relative change in position, archives with a lower index are searched first */
  176. virtual bool moveFileArchive(u32 sourceIndex, s32 relative) =0;
  177. //! Get the archive at a given index.
  178. virtual IFileArchive* getFileArchive(u32 index) =0;
  179. //! Adds an external archive loader to the engine.
  180. /** Use this function to add support for new archive types to the
  181. engine, for example proprietary or encrypted file storage. */
  182. virtual void addArchiveLoader(IArchiveLoader* loader) =0;
  183. //! Gets the number of archive loaders currently added
  184. virtual u32 getArchiveLoaderCount() const = 0;
  185. //! Retrieve the given archive loader
  186. /** \param index The index of the loader to retrieve. This parameter is an 0-based
  187. array index.
  188. \return A pointer to the specified loader, 0 if the index is incorrect. */
  189. virtual IArchiveLoader* getArchiveLoader(u32 index) const = 0;
  190. //! Adds a zip archive to the file system.
  191. /** \deprecated This function is provided for compatibility
  192. with older versions of Irrlicht and may be removed in Irrlicht 1.9,
  193. you should use addFileArchive instead.
  194. After calling this, the Irrlicht Engine will search and open files directly from this archive too.
  195. This is useful for hiding data from the end user, speeding up file access and making it possible to
  196. access for example Quake3 .pk3 files, which are no different than .zip files.
  197. \param filename: Filename of the zip archive to add to the file system.
  198. \param ignoreCase: If set to true, files in the archive can be accessed without
  199. writing all letters in the right case.
  200. \param ignorePaths: If set to true, files in the added archive can be accessed
  201. without its complete path.
  202. \return True if the archive was added successfully, false if not. */
  203. IRR_DEPRECATED virtual bool addZipFileArchive(const c8* filename, bool ignoreCase=true, bool ignorePaths=true)
  204. {
  205. return addFileArchive(filename, ignoreCase, ignorePaths, EFAT_ZIP);
  206. }
  207. //! Adds an unzipped archive (or basedirectory with subdirectories..) to the file system.
  208. /** \deprecated This function is provided for compatibility
  209. with older versions of Irrlicht and may be removed in Irrlicht 1.9,
  210. you should use addFileArchive instead.
  211. Useful for handling data which will be in a zip file
  212. \param filename: Filename of the unzipped zip archive base directory to add to the file system.
  213. \param ignoreCase: If set to true, files in the archive can be accessed without
  214. writing all letters in the right case.
  215. \param ignorePaths: If set to true, files in the added archive can be accessed
  216. without its complete path.
  217. \return True if the archive was added successful, false if not. */
  218. IRR_DEPRECATED virtual bool addFolderFileArchive(const c8* filename, bool ignoreCase=true, bool ignorePaths=true)
  219. {
  220. return addFileArchive(filename, ignoreCase, ignorePaths, EFAT_FOLDER);
  221. }
  222. //! Adds a pak archive to the file system.
  223. /** \deprecated This function is provided for compatibility
  224. with older versions of Irrlicht and may be removed in Irrlicht 1.9,
  225. you should use addFileArchive instead.
  226. After calling this, the Irrlicht Engine will search and open files directly from this archive too.
  227. This is useful for hiding data from the end user, speeding up file access and making it possible to
  228. access for example Quake2/KingPin/Hexen2 .pak files
  229. \param filename: Filename of the pak archive to add to the file system.
  230. \param ignoreCase: If set to true, files in the archive can be accessed without
  231. writing all letters in the right case.
  232. \param ignorePaths: If set to true, files in the added archive can be accessed
  233. without its complete path.(should not use with Quake2 paks
  234. \return True if the archive was added successful, false if not. */
  235. IRR_DEPRECATED virtual bool addPakFileArchive(const c8* filename, bool ignoreCase=true, bool ignorePaths=true)
  236. {
  237. return addFileArchive(filename, ignoreCase, ignorePaths, EFAT_PAK);
  238. }
  239. //! Get the current working directory.
  240. /** \return Current working directory as a string. */
  241. virtual const path& getWorkingDirectory() =0;
  242. //! Changes the current working directory.
  243. /** \param newDirectory: A string specifying the new working directory.
  244. The string is operating system dependent. Under Windows it has
  245. the form "<drive>:\<directory>\<sudirectory>\<..>". An example would be: "C:\Windows\"
  246. \return True if successful, otherwise false. */
  247. virtual bool changeWorkingDirectoryTo(const path& newDirectory) =0;
  248. //! Converts a relative path to an absolute (unique) path, resolving symbolic links if required
  249. /** \param filename Possibly relative file or directory name to query.
  250. \result Absolute filename which points to the same file. */
  251. virtual path getAbsolutePath(const path& filename) const =0;
  252. //! Get the directory a file is located in.
  253. /** \param filename: The file to get the directory from.
  254. \return String containing the directory of the file. */
  255. virtual path getFileDir(const path& filename) const =0;
  256. //! Get the base part of a filename, i.e. the name without the directory part.
  257. /** If no directory is prefixed, the full name is returned.
  258. \param filename: The file to get the basename from
  259. \param keepExtension True if filename with extension is returned otherwise everything
  260. after the final '.' is removed as well. */
  261. virtual path getFileBasename(const path& filename, bool keepExtension=true) const =0;
  262. //! flatten a path and file name for example: "/you/me/../." becomes "/you"
  263. virtual path& flattenFilename(path& directory, const path& root="/") const =0;
  264. //! Get the relative filename, relative to the given directory
  265. virtual path getRelativeFilename(const path& filename, const path& directory) const =0;
  266. //! Creates a list of files and directories in the current working directory and returns it.
  267. /** \return a Pointer to the created IFileList is returned. After the list has been used
  268. it has to be deleted using its IFileList::drop() method.
  269. See IReferenceCounted::drop() for more information. */
  270. virtual IFileList* createFileList() =0;
  271. //! Creates an empty filelist
  272. /** \return a Pointer to the created IFileList is returned. After the list has been used
  273. it has to be deleted using its IFileList::drop() method.
  274. See IReferenceCounted::drop() for more information. */
  275. virtual IFileList* createEmptyFileList(const io::path& path, bool ignoreCase, bool ignorePaths) =0;
  276. //! Set the active type of file system.
  277. virtual EFileSystemType setFileListSystem(EFileSystemType listType) =0;
  278. //! Determines if a file exists and could be opened.
  279. /** \param filename is the string identifying the file which should be tested for existence.
  280. \return True if file exists, and false if it does not exist or an error occurred. */
  281. virtual bool existFile(const path& filename) const =0;
  282. //! Creates a XML Reader from a file which returns all parsed strings as wide characters (wchar_t*).
  283. /** Use createXMLReaderUTF8() if you prefer char* instead of wchar_t*. See IIrrXMLReader for
  284. more information on how to use the parser.
  285. \return 0, if file could not be opened, otherwise a pointer to the created
  286. IXMLReader is returned. After use, the reader
  287. has to be deleted using its IXMLReader::drop() method.
  288. See IReferenceCounted::drop() for more information. */
  289. virtual IXMLReader* createXMLReader(const path& filename) =0;
  290. //! Creates a XML Reader from a file which returns all parsed strings as wide characters (wchar_t*).
  291. /** Use createXMLReaderUTF8() if you prefer char* instead of wchar_t*. See IIrrXMLReader for
  292. more information on how to use the parser.
  293. \return 0, if file could not be opened, otherwise a pointer to the created
  294. IXMLReader is returned. After use, the reader
  295. has to be deleted using its IXMLReader::drop() method.
  296. See IReferenceCounted::drop() for more information. */
  297. virtual IXMLReader* createXMLReader(IReadFile* file) =0;
  298. //! Creates a XML Reader from a file which returns all parsed strings as ASCII/UTF-8 characters (char*).
  299. /** Use createXMLReader() if you prefer wchar_t* instead of char*. See IIrrXMLReader for
  300. more information on how to use the parser.
  301. \return 0, if file could not be opened, otherwise a pointer to the created
  302. IXMLReader is returned. After use, the reader
  303. has to be deleted using its IXMLReaderUTF8::drop() method.
  304. See IReferenceCounted::drop() for more information. */
  305. virtual IXMLReaderUTF8* createXMLReaderUTF8(const path& filename) =0;
  306. //! Creates a XML Reader from a file which returns all parsed strings as ASCII/UTF-8 characters (char*).
  307. /** Use createXMLReader() if you prefer wchar_t* instead of char*. See IIrrXMLReader for
  308. more information on how to use the parser.
  309. \return 0, if file could not be opened, otherwise a pointer to the created
  310. IXMLReader is returned. After use, the reader
  311. has to be deleted using its IXMLReaderUTF8::drop() method.
  312. See IReferenceCounted::drop() for more information. */
  313. virtual IXMLReaderUTF8* createXMLReaderUTF8(IReadFile* file) =0;
  314. //! Creates a XML Writer from a file which will write ASCII/UTF-8 characters (char*).
  315. /** \return 0, if file could not be opened, otherwise a pointer to the created
  316. IXMLWriter is returned. After use, the reader
  317. has to be deleted using its IXMLWriter::drop() method.
  318. See IReferenceCounted::drop() for more information. */
  319. virtual IXMLWriterUTF8* createXMLWriterUTF8(const path& filename) =0;
  320. //! Creates a XML Writer from a file which will write ASCII/UTF-8 characters (char*).
  321. /** \return 0, if file could not be opened, otherwise a pointer to the created
  322. IXMLWriter is returned. After use, the reader
  323. has to be deleted using its IXMLWriter::drop() method.
  324. See IReferenceCounted::drop() for more information. */
  325. virtual IXMLWriterUTF8* createXMLWriterUTF8(IWriteFile* file) =0;
  326. //! Creates a XML Writer from a file.
  327. /** \return 0, if file could not be opened, otherwise a pointer to the created
  328. IXMLWriter is returned. After use, the reader
  329. has to be deleted using its IXMLWriter::drop() method.
  330. See IReferenceCounted::drop() for more information. */
  331. virtual IXMLWriter* createXMLWriter(const path& filename) =0;
  332. //! Creates a XML Writer from a file.
  333. /** \return 0, if file could not be opened, otherwise a pointer to the created
  334. IXMLWriter is returned. After use, the reader
  335. has to be deleted using its IXMLWriter::drop() method.
  336. See IReferenceCounted::drop() for more information. */
  337. virtual IXMLWriter* createXMLWriter(IWriteFile* file) =0;
  338. //! Creates a new empty collection of attributes, usable for serialization and more.
  339. /** \param driver: Video driver to be used to load textures when specified as attribute values.
  340. Can be null to prevent automatic texture loading by attributes.
  341. \return Pointer to the created object.
  342. If you no longer need the object, you should call IAttributes::drop().
  343. See IReferenceCounted::drop() for more information. */
  344. virtual IAttributes* createEmptyAttributes(video::IVideoDriver* driver=0) =0;
  345. };
  346. } // end namespace io
  347. } // end namespace irr
  348. #endif