disk_filesystem.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "disk_filesystem.h"
  6. #include "string_utils.h"
  7. #include "temp_allocator.h"
  8. #include "disk_file.h"
  9. #include "vector.h"
  10. namespace crown
  11. {
  12. DiskFilesystem::DiskFilesystem()
  13. {
  14. os::getcwd(_root_path, MAX_PATH_LENGTH);
  15. }
  16. DiskFilesystem::DiskFilesystem(const char* root_path)
  17. {
  18. CE_ASSERT_NOT_NULL(root_path);
  19. strncpy(_root_path, root_path, MAX_PATH_LENGTH);
  20. }
  21. File* DiskFilesystem::open(const char* path, FileOpenMode mode)
  22. {
  23. CE_ASSERT_NOT_NULL(path);
  24. TempAllocator256 alloc;
  25. DynamicString abs_path(alloc);
  26. get_absolute_path(path, abs_path);
  27. return CE_NEW(default_allocator(), DiskFile)(mode, abs_path.c_str());
  28. }
  29. void DiskFilesystem::close(File* file)
  30. {
  31. CE_ASSERT_NOT_NULL(file);
  32. CE_DELETE(default_allocator(), file);
  33. }
  34. bool DiskFilesystem::exists(const char* path)
  35. {
  36. CE_ASSERT_NOT_NULL(path);
  37. TempAllocator256 alloc;
  38. DynamicString abs_path(alloc);
  39. get_absolute_path(path, abs_path);
  40. return os::exists(abs_path.c_str());
  41. }
  42. bool DiskFilesystem::is_directory(const char* path)
  43. {
  44. CE_ASSERT_NOT_NULL(path);
  45. TempAllocator256 alloc;
  46. DynamicString abs_path(alloc);
  47. get_absolute_path(path, abs_path);
  48. return os::is_directory(abs_path.c_str());
  49. }
  50. bool DiskFilesystem::is_file(const char* path)
  51. {
  52. CE_ASSERT_NOT_NULL(path);
  53. TempAllocator256 alloc;
  54. DynamicString abs_path(alloc);
  55. get_absolute_path(path, abs_path);
  56. return os::is_file(abs_path.c_str());
  57. }
  58. void DiskFilesystem::create_directory(const char* path)
  59. {
  60. CE_ASSERT_NOT_NULL(path);
  61. TempAllocator256 alloc;
  62. DynamicString abs_path(alloc);
  63. get_absolute_path(path, abs_path);
  64. if (!os::exists(abs_path.c_str()))
  65. os::create_directory(abs_path.c_str());
  66. }
  67. void DiskFilesystem::delete_directory(const char* path)
  68. {
  69. CE_ASSERT_NOT_NULL(path);
  70. TempAllocator256 alloc;
  71. DynamicString abs_path(alloc);
  72. get_absolute_path(path, abs_path);
  73. os::delete_directory(abs_path.c_str());
  74. }
  75. void DiskFilesystem::create_file(const char* path)
  76. {
  77. CE_ASSERT_NOT_NULL(path);
  78. TempAllocator256 alloc;
  79. DynamicString abs_path(alloc);
  80. get_absolute_path(path, abs_path);
  81. os::create_file(abs_path.c_str());
  82. }
  83. void DiskFilesystem::delete_file(const char* path)
  84. {
  85. CE_ASSERT_NOT_NULL(path);
  86. TempAllocator256 alloc;
  87. DynamicString abs_path(alloc);
  88. get_absolute_path(path, abs_path);
  89. os::delete_file(abs_path.c_str());
  90. }
  91. void DiskFilesystem::list_files(const char* path, Vector<DynamicString>& files)
  92. {
  93. CE_ASSERT_NOT_NULL(path);
  94. TempAllocator256 alloc;
  95. DynamicString abs_path(alloc);
  96. get_absolute_path(path, abs_path);
  97. os::list_files(abs_path.c_str(), files);
  98. }
  99. void DiskFilesystem::get_absolute_path(const char* path, DynamicString& os_path)
  100. {
  101. if (os::is_absolute_path(path))
  102. {
  103. os_path = path;
  104. return;
  105. }
  106. os_path += _root_path;
  107. os_path += PATH_SEPARATOR;
  108. os_path += path;
  109. }
  110. } // namespace crown