loader to string_view

v1
Brett 2024-01-19 16:30:35 -05:00
parent 903bac9fc1
commit 3163e66879
2 changed files with 11 additions and 11 deletions

View File

@ -24,9 +24,9 @@ namespace blt::fs
char close = '>'; char close = '>';
}; };
std::string getFile(const std::string& path); std::string getFile(std::string_view path);
std::vector<std::string> getLinesFromFile(const std::string& path); std::vector<std::string> getLinesFromFile(std::string_view path);
/** /**
* Recursively include files * Recursively include files
@ -35,7 +35,7 @@ namespace blt::fs
* @param guards characters used to identify the parts that specify the file path. if empty it will assume everything after the include header * @param guards characters used to identify the parts that specify the file path. if empty it will assume everything after the include header
* @return a list of lines in all files. added together in order. * @return a list of lines in all files. added together in order.
*/ */
std::vector<std::string> recursiveInclude(const std::string& path, const std::string& include_header = "#include", std::vector<std::string> recursiveInclude(std::string_view path, const std::string& include_header = "#include",
const std::vector<include_guard>& guards = {{'<', '>'}, {'"', '"'}}); const std::vector<include_guard>& guards = {{'<', '>'}, {'"', '"'}});
static inline std::string loadBrainFuckFile(const std::string& path) static inline std::string loadBrainFuckFile(const std::string& path)
@ -53,7 +53,7 @@ namespace blt::fs
return buffer; return buffer;
} }
static inline std::string loadShaderFile(const std::string& path) static inline std::string loadShaderFile(std::string_view path)
{ {
std::stringstream stringStream; std::stringstream stringStream;

View File

@ -6,18 +6,18 @@
#include <blt/std/loader.h> #include <blt/std/loader.h>
#include <blt/std/assert.h> #include <blt/std/assert.h>
std::vector<std::string> blt::fs::getLinesFromFile(const std::string& path) std::vector<std::string> blt::fs::getLinesFromFile(std::string_view path)
{ {
std::string file = getFile(path); std::string file = getFile(path);
// we only use unix line endings here...
string::replaceAll(file, "\r", "");
// split the file into the lines, this way we can get out the #include statements. // split the file into the lines, this way we can get out the #include statements.
return string::split(file, "\n"); return string::split(file, "\n");
} }
std::vector<std::string> blt::fs::recursiveInclude(const std::string& path, const std::string& include_header, std::vector<std::string> blt::fs::recursiveInclude(std::string_view path, const std::string& include_header,
const std::vector<include_guard>& guards) const std::vector<include_guard>& guards)
{ {
std::string pathOnly = path.substr(0, path.find_last_of('/'));
auto mainLines = getLinesFromFile(path); auto mainLines = getLinesFromFile(path);
std::vector<std::string> return_lines; std::vector<std::string> return_lines;
@ -61,7 +61,7 @@ std::vector<std::string> blt::fs::recursiveInclude(const std::string& path, cons
return return_lines; return return_lines;
} }
std::string blt::fs::getFile(const std::string& path) std::string blt::fs::getFile(std::string_view path)
{ {
std::string file_contents; std::string file_contents;
std::ifstream the_file; std::ifstream the_file;
@ -72,7 +72,7 @@ std::string blt::fs::getFile(const std::string& path)
try try
{ {
// open file // open file
the_file.open(path); the_file.open(std::string(path));
std::stringstream file_stream; std::stringstream file_stream;
// read file's buffer contents into streams // read file's buffer contents into streams
file_stream << the_file.rdbuf(); file_stream << the_file.rdbuf();
@ -82,7 +82,7 @@ std::string blt::fs::getFile(const std::string& path)
file_contents = file_stream.str(); file_contents = file_stream.str();
} catch (std::ifstream::failure& e) } catch (std::ifstream::failure& e)
{ {
BLT_WARN("Unable to read file '%s'!\n", path.c_str()); BLT_WARN("Unable to read file '%s'!\n", std::string(path).c_str());
BLT_WARN("Exception: %s", e.what()); BLT_WARN("Exception: %s", e.what());
throw std::runtime_error("Failed to read file!\n"); throw std::runtime_error("Failed to read file!\n");
} }