Filesystem.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #include "Filesystem.h"
  24. #include "Log.h"
  25. #include "OS.h"
  26. #include "DiskFile.h"
  27. #include "Memory.h"
  28. namespace crown
  29. {
  30. //-----------------------------------------------------------------------------
  31. Filesystem::Filesystem(const char* root_path)
  32. {
  33. CE_ASSERT(root_path != NULL, "Root path must be != NULL");
  34. CE_ASSERT(os::is_absolute_path(root_path), "Root path must be absolute");
  35. string::strncpy(m_root_path, root_path, MAX_PATH_LENGTH);
  36. }
  37. //-----------------------------------------------------------------------------
  38. Filesystem::~Filesystem()
  39. {
  40. }
  41. //-----------------------------------------------------------------------------
  42. const char* Filesystem::root_path() const
  43. {
  44. return m_root_path;
  45. }
  46. //-----------------------------------------------------------------------------
  47. const char* Filesystem::build_os_path(const char* base_path, const char* relative_path)
  48. {
  49. static char os_path[MAX_PATH_LENGTH];
  50. string::strncpy(os_path, base_path, MAX_PATH_LENGTH);
  51. size_t base_path_len = string::strlen(base_path);
  52. os_path[base_path_len] = PATH_SEPARATOR;
  53. os_path[base_path_len + 1] = '\0';
  54. string::strcat(os_path, relative_path);
  55. // FIXME FIXME FIXME Replace Crown-specific path separator with OS-speficic one
  56. for (size_t j = 0; j < string::strlen(os_path); j++)
  57. {
  58. if (os_path[j] == '/')
  59. {
  60. os_path[j] = PATH_SEPARATOR;
  61. }
  62. }
  63. return os_path;
  64. }
  65. //-----------------------------------------------------------------------------
  66. bool Filesystem::get_info(const char* relative_path, FilesystemEntry& info)
  67. {
  68. // Entering OS-DEPENDENT-PATH-MODE
  69. // (i.e. os_path is of the form: C:\foo\relative_path or /foo/relative_path)
  70. const char* os_path = build_os_path(m_root_path, relative_path);
  71. string::strncpy(info.os_path, os_path, MAX_PATH_LENGTH);
  72. string::strncpy(info.relative_path, relative_path, MAX_PATH_LENGTH);
  73. if (os::is_reg(os_path))
  74. {
  75. info.type = FilesystemEntry::FILE;
  76. return true;
  77. }
  78. else if (os::is_dir(os_path))
  79. {
  80. info.type = FilesystemEntry::DIRECTORY;
  81. return true;
  82. }
  83. info.type = FilesystemEntry::UNKNOWN;
  84. return false;
  85. }
  86. //-----------------------------------------------------------------------------
  87. bool Filesystem::exists(const char* relative_path)
  88. {
  89. FilesystemEntry dummy;
  90. return get_info(relative_path, dummy);
  91. }
  92. //-----------------------------------------------------------------------------
  93. bool Filesystem::is_file(const char* relative_path)
  94. {
  95. FilesystemEntry info;
  96. if (get_info(relative_path, info))
  97. {
  98. return info.type == FilesystemEntry::FILE;
  99. }
  100. return false;
  101. }
  102. //-----------------------------------------------------------------------------
  103. bool Filesystem::is_dir(const char* relative_path)
  104. {
  105. FilesystemEntry info;
  106. if (get_info(relative_path, info))
  107. {
  108. return info.type == FilesystemEntry::DIRECTORY;
  109. }
  110. return false;
  111. }
  112. //-----------------------------------------------------------------------------
  113. bool Filesystem::create_file(const char* relative_path)
  114. {
  115. const char* os_path = build_os_path(m_root_path, relative_path);
  116. return os::mknod(os_path);
  117. }
  118. //-----------------------------------------------------------------------------
  119. bool Filesystem::create_dir(const char* relative_path)
  120. {
  121. const char* os_path = build_os_path(m_root_path, relative_path);
  122. return os::mkdir(os_path);
  123. }
  124. //-----------------------------------------------------------------------------
  125. bool Filesystem::delete_file(const char* relative_path)
  126. {
  127. const char* os_path = build_os_path(m_root_path, relative_path);
  128. return os::unlink(os_path);
  129. }
  130. //-----------------------------------------------------------------------------
  131. bool Filesystem::delete_dir(const char* relative_path)
  132. {
  133. const char* os_path = build_os_path(m_root_path, relative_path);
  134. return os::rmdir(os_path);
  135. }
  136. //-----------------------------------------------------------------------------
  137. const char* Filesystem::os_path(const char* relative_path)
  138. {
  139. static char os_path[MAX_PATH_LENGTH];
  140. FilesystemEntry entry;
  141. get_info(relative_path, entry);
  142. string::strncpy(os_path, entry.os_path, MAX_PATH_LENGTH);
  143. return os_path;
  144. }
  145. //-----------------------------------------------------------------------------
  146. DiskFile* Filesystem::open(const char* relative_path, FileOpenMode mode)
  147. {
  148. CE_ASSERT(exists(relative_path), "File does not exist: %s", relative_path);
  149. CE_ASSERT(is_file(relative_path), "File is not a regular file: %s", relative_path);
  150. return CE_NEW(m_allocator, DiskFile)(mode, os_path(relative_path));
  151. }
  152. //-----------------------------------------------------------------------------
  153. void Filesystem::close(DiskFile* stream)
  154. {
  155. CE_DELETE(m_allocator, stream);
  156. }
  157. } // namespace crown