1#include "DefaultIOHandler.h"
10namespace sys = std::filesystem;
15 void autoExpandEnvironmentVariables(std::string & text)
17 static std::regex env(
"%([0-9A-Za-z\\/]*)%");
19 while (std::regex_search(text, match, env)) {
20 const char * s = std::getenv(match[1].str().c_str());
21 const std::string var(s ==
nullptr ?
"" : s);
22 text.replace(match[0].first, match[0].second, var);
26 std::string expandEnvironmentVariables(
const std::string & input)
28 std::string text = input;
29 autoExpandEnvironmentVariables(text);
33 std::string getPath(Cogs::IIOHandler::FileType type,
const std::string & fileName)
35 if (type == Cogs::IIOHandler::FileType::ShaderCache) {
36 std::string path = std::string(
"%LOCALAPPDATA%") +
"\\Kongsberg Digital\\Cogs.Rendering\\ShaderCache\\" + fileName;
37 return expandEnvironmentVariables(path);
53 for (
auto & searchPath : searchPaths) {
54 const auto & path = searchPath.size() ? searchPath + std::string(fileName) : std::string(fileName);
56 file.open(path, std::ios::in);
58 if (file.is_open())
break;
60 if (!file.is_open()) {
61 fprintf(stderr,
"Failed to load \"%.*s\"\n", StringViewFormat(fileName));
65 file.seekg(0, std::ios::end);
67 const auto fileSize = file.tellg();
73 content.resize(content.size() + fileSize,
' ');
75 file.seekg(0, std::ios::beg);
77 file.read(&content[0], fileSize);
84void Cogs::DefaultIOHandler::addSearchPath(
const StringView & path)
86 searchPaths.push_back(std::string(path));
89void Cogs::DefaultIOHandler::pushSearchPaths()
91 pushedSearchPaths = searchPaths;
94void Cogs::DefaultIOHandler::popSearchPaths()
96 searchPaths = std::move(pushedSearchPaths);
99bool Cogs::DefaultIOHandler::exists(FileType type,
const StringView & fileName)
102 sys::path path(getPath(type, std::string(fileName)));
104 return sys::exists(path);
112bool Cogs::DefaultIOHandler::writeBinaryFile(FileType type,
const StringView & fileName,
const void * content,
size_t contentSize)
115 sys::path path(getPath(type, std::string(fileName)));
116 sys::create_directories(path.parent_path());
118 std::fstream file(path.string(), std::ios::binary | std::ios::out);
120 file.seekg(0, std::ios::beg);
122 file.write((
const char *)content, contentSize);
136bool Cogs::DefaultIOHandler::openBinaryFile(FileType type,
const StringView & fileName, std::vector<uint8_t> & content)
138 std::fstream file(getPath(type, std::string(fileName)), std::ios::binary | std::ios::in);
140 if (!file.is_open())
return false;
142 file.seekg(0, std::ios::end);
144 const auto fileSize = file.tellg();
150 content.resize(content.size() + fileSize,
' ');
152 file.seekg(0, std::ios::beg);
154 file.read((
char *)content.data(), fileSize);
Provides a weakly referenced view over the contents of a string.
bool openFile(const StringView &fileName, std::string &content) override
Callback method used to open files.
bool resolveFile(const StringView &source, const StringView &fileName, std::string &content) override
Callback method used to resolve include statements in shader code and get the contents to include.