disk_filesystem.cpp 2.9 KB

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