Filesystem.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (C) 2009-2021, 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, StringAuto& out);
  15. /// Get path filename.
  16. /// On path/to/file.ext return file.ext
  17. void getFilepathFilename(const CString& filename, StringAuto& out);
  18. /// Get base path.
  19. /// On path/to/file.ext return path/to
  20. void getParentFilepath(const CString& filename, StringAuto& out);
  21. /// Return true if directory exists?
  22. Bool directoryExists(const CString& dir);
  23. /// @internal
  24. ANKI_USE_RESULT Error walkDirectoryTreeInternal(const CString& dir,
  25. const Function<Error(const CString&, Bool)>& callback);
  26. /// Walk a directory tree.
  27. /// @param dir The dir to walk.
  28. /// @param alloc An allocator for temp allocations.
  29. /// @param func A lambda. See code example on how to use it.
  30. /// Example:
  31. /// @code
  32. /// walkDirectoryTree("./path/to", alloc, [&, this](CString path, Bool isDir) {
  33. /// ...
  34. /// return Error::NONE;
  35. /// });
  36. /// @endcode
  37. template<typename TFunc>
  38. ANKI_USE_RESULT Error walkDirectoryTree(const CString& dir, GenericMemoryPoolAllocator<U8> alloc, TFunc func)
  39. {
  40. Function<Error(const CString&, Bool)> f(alloc, func);
  41. const Error err = walkDirectoryTreeInternal(dir, f);
  42. f.destroy(alloc);
  43. return err;
  44. }
  45. /// Equivalent to: rm -rf dir
  46. /// @param dir The directory to remove.
  47. /// @param alloc A temp allocator that this function requires.
  48. ANKI_USE_RESULT Error removeDirectory(const CString& dir, GenericMemoryPoolAllocator<U8> alloc);
  49. /// Equivalent to: mkdir dir
  50. ANKI_USE_RESULT Error createDirectory(const CString& dir);
  51. /// Get the home directory.
  52. ANKI_USE_RESULT Error getHomeDirectory(StringAuto& out);
  53. /// Get the temp directory.
  54. ANKI_USE_RESULT Error getTempDirectory(StringAuto& out);
  55. /// Get the time the file was last modified.
  56. ANKI_USE_RESULT Error getFileModificationTime(CString filename, U32& year, U32& month, U32& day, U32& hour, U32& min,
  57. U32& second);
  58. /// @}
  59. } // end namespace anki