BsFileSystem.h 5.0 KB

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