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