Filesystem.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "Stream.h"
  25. namespace crown
  26. {
  27. struct FilesystemEntry
  28. {
  29. enum Type
  30. {
  31. DIRECTORY = 0, ///< The entry is a directory
  32. FILE, ///< The entry is a file
  33. MEMORY, ///< The entry is a memory file (i.e. does not exist on the disk)
  34. UNKNOWN ///< The entry type is unknown
  35. };
  36. FilesystemEntry() : type(UNKNOWN) {}
  37. Type type; ///< Type of the entry
  38. char os_path[512]; ///< OS-specific path (use only for debug)
  39. char relative_path[512]; ///< Relative path of the entry
  40. };
  41. /// Filesystem.
  42. ///
  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 can be really anything: platform-specific/absolute/relative/whatever.
  64. /// Examples of valid root paths.
  65. ///
  66. /// 1) "/home/foo"
  67. /// 2) "C:\Users\Phil"
  68. /// 3) "\abc$\/..)?/"
  69. ///
  70. /// The relative paths must follow some strict rules:
  71. ///
  72. /// a) Only unix-like pathnames (i.e. case sensitive and using '/' as separator)
  73. /// are allowed.
  74. /// b) Only relative paths are allowed: the filesystem is responsible for
  75. /// the creation of its absolute platform-specific counterpart.
  76. /// c) Filesystem forbids pathnames containing '.' and '..' to prevent access to
  77. /// files outside the filesystem's root path.
  78. /// d) Platform specific characters like '/', '\\' and ':' are forbidden as well.
  79. /// e) Symlinks, on platforms which support them, are _not_ resolved for the same
  80. /// reason of c)
  81. /// f) Although mixed-case pathnames are allowed, it is generally safer to use
  82. /// only lower-case ones for maximum compatibility.
  83. ///
  84. /// Examples of valid relative paths.
  85. ///
  86. /// data/textures/grass.texture
  87. /// grass.texture
  88. /// foo/bar
  89. class Filesystem
  90. {
  91. public:
  92. Filesystem(const char* root_path);
  93. ~Filesystem();
  94. /// Returns the root path of the filesystem
  95. const char* root_path() const;
  96. /// TODO
  97. bool get_info(const char* base_path, const char* relative_path, FilesystemEntry& info);
  98. /// Returns whether the @relative_path exists on disk
  99. bool exists(const char* relative_path);
  100. /// Returns whether @relative_path is a regular file
  101. bool is_file(const char* relative_path);
  102. /// Returns whether @relative_path if a directory
  103. bool is_dir(const char* relative_path);
  104. /// Creates a regular file named @relative_path
  105. bool create_file(const char* relative_path);
  106. /// Creates a directory named @relative_path
  107. bool create_dir(const char* relative_path);
  108. /// Deletes the regular file @relative_path
  109. bool delete_file(const char* relative_path);
  110. /// Deletes the directory @relative_path
  111. bool delete_dir(const char* relative_path);
  112. Stream* open(const char* relative_path, StreamOpenMode mode);
  113. void close(Stream* stream);
  114. private:
  115. const char* build_os_path(const char* basePath, const char* relative_path);
  116. private:
  117. const char* m_root_path;
  118. // Disable copying
  119. Filesystem(const Filesystem&);
  120. Filesystem& operator=(const Filesystem&);
  121. };
  122. } // namespace crown