BsFileSystem.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisitesUtil.h"
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup Filesystem
  8. * @{
  9. */
  10. /** Utility class for dealing with files. */
  11. class BS_UTILITY_EXPORT FileSystem
  12. {
  13. public:
  14. /**
  15. * Opens a file and returns a data stream capable of reading or writing to that file.
  16. *
  17. * @param[in] fullPath Full path to a file.
  18. * @param[in] readOnly (optional) If true, returned stream will only be readable.
  19. */
  20. static SPtr<DataStream> openFile(const Path& fullPath, bool readOnly = true);
  21. /**
  22. * Opens a file and returns a data stream capable of reading and writing to that file. If file doesn't exist new
  23. * one will be created.
  24. *
  25. * @param[in] fullPath Full path to a file.
  26. */
  27. static SPtr<DataStream> createAndOpenFile(const Path& fullPath);
  28. /**
  29. * Returns the size of a file in bytes.
  30. *
  31. * @param[in] fullPath Full path to a file.
  32. */
  33. static UINT64 getFileSize(const Path& fullPath);
  34. /**
  35. * Deletes a file or a folder at the specified path.
  36. *
  37. * @param[in] fullPath Full path to a file or a folder..
  38. * @param[in] recursively (optional) If true, folders will have their contents deleted as well. Otherwise an
  39. * exception will be thrown for non-empty folders.
  40. */
  41. static void remove(const Path& fullPath, bool recursively = true);
  42. /**
  43. * Moves a file or a folder from one to another path. This can also be used as a rename operation.
  44. *
  45. * @param[in] oldPath Full path to the old file/folder.
  46. * @param[in] newPath Full path to the new file/folder.
  47. * @param[in] overwriteExisting (optional) If true, any existing file/folder at the new location will be
  48. * overwritten, otherwise an exception will be thrown if a file/folder already exists.
  49. */
  50. static void move(const Path& oldPath, const Path& newPath, bool overwriteExisting = true);
  51. /**
  52. * Makes a copy of a file or a folder in the specified path.
  53. *
  54. * @param[in] oldPath Full path to the old file/folder.
  55. * @param[in] newPath Full path to the new file/folder.
  56. * @param[in] overwriteExisting (optional) If true, any existing file/folder at the new location will be
  57. * overwritten, otherwise an exception will be thrown if a file/folder already exists.
  58. */
  59. static void copy(const Path& oldPath, const Path& newPath, bool overwriteExisting = true);
  60. /**
  61. * Creates a folder at the specified path.
  62. *
  63. * @param[in] fullPath Full path to a full folder to create.
  64. */
  65. static void createDir(const Path& fullPath);
  66. /**
  67. * Returns true if a file or a folder exists at the specified path.
  68. *
  69. * @param[in] fullPath Full path to a file or folder.
  70. */
  71. static bool exists(const Path& fullPath);
  72. /**
  73. * Returns true if a file exists at the specified path.
  74. *
  75. * @param[in] fullPath Full path to a file or folder.
  76. */
  77. static bool isFile(const Path& fullPath);
  78. /**
  79. * Returns true if a folder exists at the specified path.
  80. *
  81. * @param[in] fullPath Full path to a file or folder.
  82. */
  83. static bool isDirectory(const Path& fullPath);
  84. /**
  85. * Returns all files or folders located in the specified folder.
  86. *
  87. * @param[in] dirPath Full path to the folder to retrieve children files/folders from.
  88. * @param[out] files Full paths to all files located directly in specified folder.
  89. * @param[out] directories Full paths to all folders located directly in specified folder.
  90. */
  91. static void getChildren(const Path& dirPath, Vector<Path>& files, Vector<Path>& directories);
  92. /**
  93. * Iterates over all files and directories in the specified folder and calls the provided callback when a
  94. * file/folder is iterated over.
  95. *
  96. * @param[in] dirPath Directory over which to iterate
  97. * @param[in] fileCallback Callback to call whenever a file is found. If callback returns false iteration stops. Can be null.
  98. * @param[in] dirCallback Callback to call whenever a directory is found. If callback returns false iteration stops. Can be null.
  99. * @param[in] recursive If false then only the direct children of the provided folder will be iterated over,
  100. * and if true then child directories will be recursively visited as well.
  101. * @return True if iteration finished iterating over all files/folders, or false if it was
  102. * interrupted by a callback returning false.
  103. */
  104. static bool iterate(const Path& dirPath, std::function<bool(const Path&)> fileCallback,
  105. std::function<bool(const Path&)> dirCallback = nullptr, bool recursive = true);
  106. /**
  107. * Returns the last modified time of a file or a folder at the specified path.
  108. *
  109. * @param[in] fullPath Full path to a file or a folder.
  110. */
  111. static std::time_t getLastModifiedTime(const Path& fullPath);
  112. /** Returns the path to the currently working directory. */
  113. static Path getWorkingDirectoryPath();
  114. /** Returns the path to a directory where temporary files may be stored. */
  115. static Path getTempDirectoryPath();
  116. private:
  117. /** Copy a single file. Internal function used by copy(). */
  118. static void copyFile(const Path& oldPath, const Path& newPath);
  119. /** Remove a single file. Internal function used by remove(). */
  120. static void removeFile(const Path& path);
  121. /** Move a single file. Internal function used by move(). */
  122. static void moveFile(const Path& oldPath, const Path& newPath);
  123. };
  124. /** @} */
  125. }