diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index dd2a7cc4..da7a1956 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -86,7 +86,7 @@ add_library(bald_engine ${CMAKE_CURRENT_SOURCE_DIR}/src/core/platform/opengl/buffers/opengl_index_buffer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/core/platform/opengl/buffers/opengl_vertex_array.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/core/platform/opengl/shaders/opengl_shader.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/src/core/utils/file_manager.cpp + src/core/utils/file_manager/file_manager.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/core/utils/split_string.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/core/utils/timer.cpp) diff --git a/engine/src/core/platform/opengl/shaders/opengl_shader.cpp b/engine/src/core/platform/opengl/shaders/opengl_shader.cpp index 4536686b..e606ff20 100644 --- a/engine/src/core/platform/opengl/shaders/opengl_shader.cpp +++ b/engine/src/core/platform/opengl/shaders/opengl_shader.cpp @@ -4,7 +4,7 @@ #include "opengl_shader.h" #include -#include "file_manager.h" +#include "src/core/utils/file_manager/file_manager.h" #include "pch.h" #include "glad/glad.h" @@ -101,7 +101,7 @@ namespace Bald::Platform::Graphics { vertexShader = glCreateShader(GL_VERTEX_SHADER); std::string vertexShaderSource = Utils::FileManager::ReadFile(m_VertexPath, - Utils::FileManager::Size::SMALL_FILE); + Utils::FileManager::Size::small_file); const char* vertexData = vertexShaderSource.c_str(); glShaderSource(vertexShader, 1, &vertexData, nullptr); @@ -127,7 +127,7 @@ namespace Bald::Platform::Graphics { fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); std::string fragmentShaderSource = Utils::FileManager::ReadFile(m_FragmentPath, - Utils::FileManager::Size::SMALL_FILE); + Utils::FileManager::Size::small_file); const char* fragmentData = fragmentShaderSource.c_str(); glShaderSource(fragmentShader, 1, &fragmentData, nullptr); diff --git a/engine/src/core/utils/file_manager.cpp b/engine/src/core/utils/file_manager/file_manager.cpp similarity index 69% rename from engine/src/core/utils/file_manager.cpp rename to engine/src/core/utils/file_manager/file_manager.cpp index a503498a..2bc1049e 100644 --- a/engine/src/core/utils/file_manager.cpp +++ b/engine/src/core/utils/file_manager/file_manager.cpp @@ -13,7 +13,7 @@ namespace Bald::Utils { std::string FileManager::ReadFile(const char* filePath, Size size) { - if (size == Size::SMALL_FILE) + if (size == Size::small_file) return ReadSmallFile(filePath); return ReadBigFile(filePath); } @@ -74,10 +74,32 @@ namespace Bald::Utils { return result; } -#elif WINDOWS +#else +#include std::string FileManager::ReadBigFile(const char *filePath) { - CORE_LOG_INFO("[FILE_MANAGER] Error: Windows implementation is not done yet! Using slower reading method!"); - ReadSmallFile(filePath); +// CORE_LOG_INFO("[FILE_MANAGER] Error: Windows implementation is not done yet! Using slower reading method!"); +// ReadSmallFile(filePath); + std::streampos begin,end; + std::ifstream file(filePath, std::ios::in); + char* buffer; + if(file.is_open()){ + begin = file.tellg(); + file.seekg (0, std::ios::end); + end = file.tellg(); + auto size = static_cast(end-begin); + buffer = new char[static_cast(size)]; + std::memset(buffer, '\0', static_cast(size)); + file.seekg(0, std::ios::beg); + file.read(buffer, static_cast(size)); + file.close(); + std::string result(buffer); + delete[] buffer; + return result; + } + + CORE_LOG_WARN("[FileManager] Couldn't get size of the file. Check if the file exists at path: " + static_cast(filePath)); + return "[FileManager] Couldn't get size of the file. Check if the file exists at path: " + static_cast(filePath); + } #endif diff --git a/engine/src/core/utils/file_manager.h b/engine/src/core/utils/file_manager/file_manager.h similarity index 98% rename from engine/src/core/utils/file_manager.h rename to engine/src/core/utils/file_manager/file_manager.h index b877e0d5..59adf5fd 100644 --- a/engine/src/core/utils/file_manager.h +++ b/engine/src/core/utils/file_manager/file_manager.h @@ -19,7 +19,7 @@ namespace Bald::Utils { * ENUM which determines size of file and therefor methods, which will be used to read it */ enum class Size : char { - SMALL_FILE, BIG_FILE + small_file, big_file }; public: diff --git a/engine/tests/test_file_manager.cpp b/engine/tests/test_file_manager.cpp index 49a13cdf..f4a23b82 100644 --- a/engine/tests/test_file_manager.cpp +++ b/engine/tests/test_file_manager.cpp @@ -4,32 +4,32 @@ #include "core/pch.h" #include "gtest/gtest.h" -#include "utils/file_manager.h" +#include "src/core/utils/file_manager/file_manager.h" TEST(FileManager, GoodSmallFileOpening) { //NOLINT - std::string file_result = Bald::Utils::FileManager::ReadFile("../engine/test_file_manager.txt", Bald::Utils::FileManager::Size::SMALL_FILE); + std::string file_result = Bald::Utils::FileManager::ReadFile("../engine/test_file_manager.txt", Bald::Utils::FileManager::Size::small_file); EXPECT_EQ("plik testowy\ndo odczytu", file_result); } TEST(FileManager, GoodBigFileOpening) { //NOLINT - std::string file_result = Bald::Utils::FileManager::ReadFile("../engine/test_file_manager.txt", Bald::Utils::FileManager::Size::BIG_FILE); + std::string file_result = Bald::Utils::FileManager::ReadFile("../engine/test_file_manager.txt", Bald::Utils::FileManager::Size::big_file); EXPECT_EQ("plik testowy\ndo odczytu", file_result); } TEST(FileManager, WrongSmallFileOpening) { //NOLINT - std::string file_result = Bald::Utils::FileManager::ReadFile("no_such_file.cpp", Bald::Utils::FileManager::Size::SMALL_FILE); + std::string file_result = Bald::Utils::FileManager::ReadFile("no_such_file.cpp", Bald::Utils::FileManager::Size::small_file); EXPECT_EQ("[FileManager] Couldn't open the file at path: no_such_file.cpp", file_result); } TEST(FileManager, WrongBigFileOpening) { //NOLINT - std::string file_result = Bald::Utils::FileManager::ReadFile("no_such_file.cpp", Bald::Utils::FileManager::Size::BIG_FILE); + std::string file_result = Bald::Utils::FileManager::ReadFile("no_such_file.cpp", Bald::Utils::FileManager::Size::big_file); EXPECT_EQ("[FileManager] Couldn't get size of the file. Check if the file exists at path: no_such_file.cpp", file_result); }