Filesystem.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "File.h"
  25. #include "Vector.h"
  26. #include "DynamicString.h"
  27. namespace crown
  28. {
  29. /// @defgroup Filesystem Filesystem
  30. /// Provides a platform-independent way to access files and directories
  31. /// on the host filesystem.
  32. ///
  33. /// Accessing files:
  34. /// Every file and every directory must be accessed through the Filesystem.
  35. /// Not a single C/C++ std file io call or other similar facilities
  36. /// should be used in any other part of the engine in order to maintain
  37. /// absolute platform independence.
  38. ///
  39. /// Filesystem maintains a root path which acts as base directory for every
  40. /// file operation; access to files outside the root path is not allowed. If
  41. /// you really need it, instantiate another filesystem whith the appropriate
  42. /// root path (e.g.)
  43. ///
  44. /// Filesystem fs("/home/foo"); // fs will use "/home/foo" as root path
  45. ///
  46. /// fs.is_file("bar.txt"); // file "bar.txt" is relative to the root path,
  47. /// // so it refers to "/home/foo/bar.txt"
  48. ///
  49. /// The filesystem will take care of the necessary path conversions.
  50. /// The root path must be an absolute path for the underlying operating system.
  51. /// Examples of valid root paths:
  52. ///
  53. /// 1) "/home/foo"
  54. /// 2) "C:\Users\Phil"
  55. ///
  56. /// The relative paths, used to access files, must follow some strict rules:
  57. ///
  58. /// a) Only unix-like pathnames (i.e. case sensitive and using '/' as separator)
  59. /// are allowed.
  60. /// b) Only relative paths are allowed: the filesystem is responsible for
  61. /// the creation of its absolute platform-specific counterpart.
  62. /// c) Filesystem forbids pathnames containing '.' and '..' to prevent access to
  63. /// files outside the filesystem's root path.
  64. /// d) Platform specific characters like '/', '\\' and ':' are forbidden as well.
  65. /// e) Symlinks, on platforms which support them, are _not_ resolved for the same
  66. /// reason of c)
  67. /// f) Although mixed-case pathnames are allowed, it is generally safer to use
  68. /// only lower-case ones for maximum compatibility.
  69. ///
  70. /// Examples of valid relative paths.
  71. ///
  72. /// 1) data/textures/grass.texture
  73. /// 2) grass.texture
  74. /// 3) foo/bar
  75. ///
  76. /// @ingroup Filesystem
  77. class Filesystem
  78. {
  79. public:
  80. Filesystem() {};
  81. virtual ~Filesystem() {};
  82. /// Opens the file at the given @a path with the given @a mode.
  83. virtual File* open(const char* path, FileOpenMode mode) = 0;
  84. /// Closes the given @a file.
  85. virtual void close(File* file) = 0;
  86. /// Returns true if @a path is a directory.
  87. virtual bool is_directory(const char* path) = 0;
  88. /// Returns true if @a path is a regular file.
  89. virtual bool is_file(const char* path) = 0;
  90. /// Creates the directory at the given @a path.
  91. virtual void create_directory(const char* path) = 0;
  92. /// Deletes the directory at the given @a path.
  93. virtual void delete_directory(const char* path) = 0;
  94. /// Creates the file at the given @a path.
  95. virtual void create_file(const char* path) = 0;
  96. /// Deletes the file at the given @a path.
  97. virtual void delete_file(const char* path) = 0;
  98. /// Returns the relative file names in the given @a path.
  99. virtual void list_files(const char* path, Vector<DynamicString>& files) = 0;
  100. /// Returns the absolute path of the given @a path based on
  101. /// the root path of the file source. If @a path is absolute,
  102. /// the given path is returned.
  103. virtual void get_absolute_path(const char* path, DynamicString& os_path) = 0;
  104. private:
  105. // Disable copying
  106. Filesystem(const Filesystem&);
  107. Filesystem& operator=(const Filesystem&);
  108. };
  109. } // namespace crown