Filesystem.h 5.2 KB

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