Optimize parameter passing to avoid unnecessary copying
This commit is contained in:
+13
-13
@@ -15,7 +15,7 @@
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
files::rafile::rafile(fs::path filename)
|
||||
files::rafile::rafile(const fs::path& filename)
|
||||
: file(filename, std::ios::binary | std::ios::ate) {
|
||||
if (!file) {
|
||||
throw std::runtime_error("could not to open file "+filename.string());
|
||||
@@ -36,7 +36,7 @@ void files::rafile::read(char* buffer, std::streamsize size) {
|
||||
file.read(buffer, size);
|
||||
}
|
||||
|
||||
bool files::write_bytes(fs::path filename, const ubyte* data, size_t size) {
|
||||
bool files::write_bytes(const fs::path& filename, const ubyte* data, size_t size) {
|
||||
std::ofstream output(filename, std::ios::binary);
|
||||
if (!output.is_open())
|
||||
return false;
|
||||
@@ -45,7 +45,7 @@ bool files::write_bytes(fs::path filename, const ubyte* data, size_t size) {
|
||||
return true;
|
||||
}
|
||||
|
||||
uint files::append_bytes(fs::path filename, const ubyte* data, size_t size) {
|
||||
uint files::append_bytes(const fs::path& filename, const ubyte* data, size_t size) {
|
||||
std::ofstream output(filename, std::ios::binary | std::ios::app);
|
||||
if (!output.is_open())
|
||||
return 0;
|
||||
@@ -55,7 +55,7 @@ uint files::append_bytes(fs::path filename, const ubyte* data, size_t size) {
|
||||
return position;
|
||||
}
|
||||
|
||||
bool files::read(fs::path filename, char* data, size_t size) {
|
||||
bool files::read(const fs::path& filename, char* data, size_t size) {
|
||||
std::ifstream output(filename, std::ios::binary);
|
||||
if (!output.is_open())
|
||||
return false;
|
||||
@@ -64,7 +64,7 @@ bool files::read(fs::path filename, char* data, size_t size) {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<ubyte[]> files::read_bytes(fs::path filename, size_t& length) {
|
||||
std::unique_ptr<ubyte[]> files::read_bytes(const fs::path& filename, size_t& length) {
|
||||
std::ifstream input(filename, std::ios::binary);
|
||||
if (!input.is_open())
|
||||
return nullptr;
|
||||
@@ -78,7 +78,7 @@ std::unique_ptr<ubyte[]> files::read_bytes(fs::path filename, size_t& length) {
|
||||
return data;
|
||||
}
|
||||
|
||||
std::string files::read_string(fs::path filename) {
|
||||
std::string files::read_string(const fs::path& filename) {
|
||||
size_t size;
|
||||
std::unique_ptr<ubyte[]> bytes (read_bytes(filename, size));
|
||||
if (bytes == nullptr) {
|
||||
@@ -88,7 +88,7 @@ std::string files::read_string(fs::path filename) {
|
||||
return std::string((const char*)bytes.get(), size);
|
||||
}
|
||||
|
||||
bool files::write_string(fs::path filename, const std::string content) {
|
||||
bool files::write_string(const fs::path& filename, const std::string content) {
|
||||
std::ofstream file(filename);
|
||||
if (!file) {
|
||||
return false;
|
||||
@@ -97,16 +97,16 @@ bool files::write_string(fs::path filename, const std::string content) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool files::write_json(fs::path filename, const dynamic::Map* obj, bool nice) {
|
||||
bool files::write_json(const fs::path& filename, const dynamic::Map* obj, bool nice) {
|
||||
return files::write_string(filename, json::stringify(obj, nice, " "));
|
||||
}
|
||||
|
||||
bool files::write_binary_json(fs::path filename, const dynamic::Map* obj, bool compression) {
|
||||
bool files::write_binary_json(const fs::path& filename, const dynamic::Map* obj, bool compression) {
|
||||
auto bytes = json::to_binary(obj, compression);
|
||||
return files::write_bytes(filename, bytes.data(), bytes.size());
|
||||
}
|
||||
|
||||
std::shared_ptr<dynamic::Map> files::read_json(fs::path filename) {
|
||||
std::shared_ptr<dynamic::Map> files::read_json(const fs::path& filename) {
|
||||
std::string text = files::read_string(filename);
|
||||
try {
|
||||
return json::parse(filename.string(), text);;
|
||||
@@ -116,17 +116,17 @@ std::shared_ptr<dynamic::Map> files::read_json(fs::path filename) {
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<dynamic::Map> files::read_binary_json(fs::path file) {
|
||||
std::shared_ptr<dynamic::Map> files::read_binary_json(const fs::path& file) {
|
||||
size_t size;
|
||||
std::unique_ptr<ubyte[]> bytes (files::read_bytes(file, size));
|
||||
return json::from_binary(bytes.get(), size);
|
||||
}
|
||||
|
||||
std::shared_ptr<dynamic::Map> files::read_toml(fs::path file) {
|
||||
std::shared_ptr<dynamic::Map> files::read_toml(const fs::path& file) {
|
||||
return toml::parse(file.u8string(), files::read_string(file));
|
||||
}
|
||||
|
||||
std::vector<std::string> files::read_list(fs::path filename) {
|
||||
std::vector<std::string> files::read_list(const fs::path& filename) {
|
||||
std::ifstream file(filename);
|
||||
if (!file) {
|
||||
throw std::runtime_error("could not to open file "+filename.u8string());
|
||||
|
||||
Reference in New Issue
Block a user