Skip to content

Commit b64d312

Browse files
authored
[merge] Merge pull request #439 from inexorgame/hanni/file_wrapper_refactoring
[file|shader] Refactor file loading code
2 parents c3ad12a + 8aef142 commit b64d312

File tree

3 files changed

+38
-72
lines changed

3 files changed

+38
-72
lines changed

include/inexor/vulkan-renderer/tools/file.hpp

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,14 @@
66

77
namespace inexor::vulkan_renderer::tools {
88

9-
/// @brief A class for loading files into memory.
10-
/// @todo Refactor into an RAII wrapper.
11-
class File {
12-
private:
13-
/// The file data.
14-
std::vector<char> m_file_data;
15-
16-
/// The size of the file.
17-
std::size_t m_file_size{0};
18-
19-
public:
20-
File() = default;
21-
22-
[[nodiscard]] std::size_t file_size() const {
23-
return m_file_size;
24-
}
25-
26-
[[nodiscard]] const std::vector<char> &file_data() const {
27-
return m_file_data;
28-
}
29-
30-
/// @brief Load the entire file into memory.
31-
/// @param file_name The name of the file.
32-
/// @return ``true`` if file was loaded successfully.
33-
[[nodiscard]] bool load_file(const std::string &file_name);
34-
};
9+
/// @brief Extract the extension of a file as lowercase string.
10+
/// @param file_name the name of the file. This is allowed to include the relative or complete path
11+
/// @return The extension of the file as lowercase
12+
[[nodiscard]] std::string get_file_extension_lowercase(const std::string &file_name);
13+
14+
/// @brief Read the data of a file into memory
15+
/// @param file_name The name of the file
16+
/// @return A std::vector of type char which contains the binary data of the file
17+
[[nodiscard]] std::vector<char> read_file_binary_data(const std::string &file_name);
3518

3619
} // namespace inexor::vulkan_renderer::tools

src/vulkan-renderer/tools/file.cpp

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,49 @@
11
#include "inexor/vulkan-renderer/tools/file.hpp"
22

3-
#include <spdlog/spdlog.h>
4-
3+
#include <algorithm>
54
#include <cassert>
5+
#include <filesystem>
66
#include <fstream>
77

88
namespace inexor::vulkan_renderer::tools {
99

10-
bool File::load_file(const std::string &file_name) {
10+
std::string get_file_extension_lowercase(const std::string &file_name) {
1111
assert(!file_name.empty());
1212

13-
// Open stream at the end of the file to read it's size.
14-
std::ifstream file_to_load(file_name.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
13+
// Extract the file extension
14+
std::string file_extension = std::filesystem::path(file_name).extension().string();
15+
16+
if (file_extension.empty()) {
17+
return "";
18+
}
1519

16-
if (file_to_load.is_open()) {
17-
spdlog::debug("File {} has been opened.", file_name);
20+
// Convert file extension string to lowercase
21+
std::transform(file_extension.begin(), file_extension.end(), file_extension.begin(),
22+
[](unsigned char c) { return std::tolower(c); });
1823

19-
// Read the size of the file.
20-
const auto file_size = file_to_load.tellg();
24+
return file_extension;
25+
}
2126

22-
// Preallocate memory for the file buffer.
23-
m_file_data.resize(m_file_size);
27+
std::vector<char> read_file_binary_data(const std::string &file_name) {
2428

25-
// Reset the file read position to the beginning of the file.
26-
file_to_load.seekg(0, std::ios::beg);
29+
// Open stream at the end of the file to read it's size.
30+
std::ifstream file(file_name.c_str(), std::ios::ate | std::ios::binary | std::ios::in);
2731

28-
// Read the file data.
29-
file_to_load.read(m_file_data.data(), file_size);
32+
if (!file) {
33+
throw std::runtime_error("Error: Could not open file " + file_name + "!");
34+
}
3035

31-
m_file_size = static_cast<std::size_t>(file_size);
36+
// Read the size of the file
37+
const auto file_size = file.tellg();
3238

33-
// Close the file stream.
34-
file_to_load.close();
39+
std::vector<char> buffer(file_size);
3540

36-
spdlog::debug("File {} has been closed.", file_name.c_str());
41+
// Set the file read position to the beginning of the file
42+
file.seekg(0);
3743

38-
return true;
39-
}
44+
file.read(buffer.data(), file_size);
4045

41-
spdlog::error("Could not open shader!");
42-
return false;
46+
return buffer;
4347
}
4448

4549
} // namespace inexor::vulkan_renderer::tools

src/vulkan-renderer/wrapper/shader.cpp

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,21 @@
11
#include "inexor/vulkan-renderer/wrapper/shader.hpp"
22

33
#include "inexor/vulkan-renderer/exception.hpp"
4+
#include "inexor/vulkan-renderer/tools/file.hpp"
45
#include "inexor/vulkan-renderer/wrapper/device.hpp"
56
#include "inexor/vulkan-renderer/wrapper/make_info.hpp"
67

78
#include <spdlog/spdlog.h>
89

910
#include <cassert>
10-
#include <fstream>
1111
#include <stdexcept>
1212
#include <utility>
1313

14-
namespace {
15-
16-
std::vector<char> read_binary(const std::string &file_name) {
17-
// Open stream at the end of the file to read it's size.
18-
std::ifstream file(file_name.c_str(), std::ios::ate | std::ios::binary | std::ios::in);
19-
20-
if (!file) {
21-
throw std::runtime_error("Error: Could not open file " + file_name + "!");
22-
}
23-
24-
const auto file_size = file.tellg();
25-
std::vector<char> buffer(file_size);
26-
27-
file.seekg(0);
28-
file.read(buffer.data(), file_size);
29-
30-
return buffer;
31-
}
32-
33-
} // namespace
34-
3514
namespace inexor::vulkan_renderer::wrapper {
3615

3716
Shader::Shader(const Device &device, const VkShaderStageFlagBits type, const std::string &name,
3817
const std::string &file_name, const std::string &entry_point)
39-
: Shader(device, type, name, read_binary(file_name), entry_point) {}
18+
: Shader(device, type, name, tools::read_file_binary_data(file_name), entry_point) {}
4019

4120
Shader::Shader(const Device &device, const VkShaderStageFlagBits type, const std::string &name,
4221
const std::vector<char> &code, const std::string &entry_point)

0 commit comments

Comments
 (0)