Filesystem.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Util/String.h>
  7. #include <AnKi/Util/Function.h>
  8. namespace anki {
  9. /// @addtogroup util_file
  10. /// @{
  11. /// Return true if a file exists
  12. Bool fileExists(const CString& filename);
  13. /// Get path extension.
  14. void getFilepathExtension(const CString& filename, String& out);
  15. /// Get path filename.
  16. /// On path/to/file.ext return file.ext
  17. void getFilepathFilename(const CString& filename, String& out);
  18. /// Get base path.
  19. /// On path/to/file.ext return path/to
  20. void getParentFilepath(const CString& filename, String& out);
  21. /// Return true if directory exists?
  22. Bool directoryExists(const CString& dir);
  23. class WalkDirectoryArgs
  24. {
  25. public:
  26. CString m_path;
  27. Bool m_isDirectory = false;
  28. Bool m_stopSearch = false;
  29. };
  30. /// @internal
  31. Error walkDirectoryTreeInternal(CString dir, const Function<Error(WalkDirectoryArgs& args)>& callback);
  32. /// Walk a directory tree.
  33. /// @param dir The dir to walk.
  34. /// @param func A lambda. See code example on how to use it.
  35. /// Example:
  36. /// @code
  37. /// walkDirectoryTree("./path/to", [&, this](WalkDirectoryArgs& args) {
  38. /// ...
  39. /// return Error::kNone;
  40. /// });
  41. /// @endcode
  42. template<typename TFunc>
  43. Error walkDirectoryTree(CString dir, TFunc func)
  44. {
  45. Function<Error(WalkDirectoryArgs & args)> f(func);
  46. const Error err = walkDirectoryTreeInternal(dir, f);
  47. return err;
  48. }
  49. /// Equivalent to: rm -rf dir
  50. /// @param dir The directory to remove.
  51. Error removeDirectory(const CString& dir);
  52. /// Remove a file.
  53. Error removeFile(const CString& filename);
  54. /// Equivalent to: mkdir dir
  55. Error createDirectory(const CString& dir);
  56. /// Get the home directory.
  57. Error getHomeDirectory(String& out);
  58. /// Get the temp directory.
  59. Error getTempDirectory(String& out);
  60. /// Get the time the file was last modified.
  61. Error getFileModificationTime(CString filename, U32& year, U32& month, U32& day, U32& hour, U32& min, U32& second);
  62. /// Get the path+filename of the currently running executable.
  63. Error getApplicationPath(String& path);
  64. /// A convenience class to delete a file when it goes out of scope. It tries multiple times because of Windows
  65. /// antivirus sometimes keeping a lock to the file.
  66. class CleanupFile
  67. {
  68. public:
  69. String m_fileToDelete;
  70. U32 m_tries = 3 * 1000; ///< Number of times to try delete the file.
  71. Second m_seepTimeBeforeNextTry = 1.0_ms; ///< Time before the next try.
  72. CleanupFile(CString filename)
  73. : m_fileToDelete(filename)
  74. {
  75. }
  76. /// Deletes the file.
  77. ~CleanupFile();
  78. };
  79. /// @}
  80. } // end namespace anki