Filesystem.h 2.2 KB

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