From 9eb1a7fcb0de30434a59c47cd83ba13862619690 Mon Sep 17 00:00:00 2001 From: "matthias@arch" Date: Sun, 30 Oct 2022 01:15:48 +0200 Subject: [PATCH] Added readBinaryFile --- src/file_io.cpp | 39 ++++++++++++++++++++++++++++++++------- src/file_io.hpp | 16 ++++++++++++---- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/file_io.cpp b/src/file_io.cpp index 3881f44..2c17ed7 100644 --- a/src/file_io.cpp +++ b/src/file_io.cpp @@ -1,4 +1,5 @@ #include "file_io.hpp" + #include "exceptions.hpp" #include "util/string.hpp" @@ -10,9 +11,11 @@ namespace gz { +// +// KEY-VALUE FILE +// template - bool writeKeyValueFile(const std::string& filepath, const std::unordered_map& content) { - bool success = false; + void writeKeyValueFile(const std::string& filepath, const std::unordered_map& content) { std::ofstream file(filepath.c_str()); if (file.is_open()) { file << "# Written by writeKeyValueFile" << std::endl; @@ -20,15 +23,13 @@ namespace gz { file << line.first << " = " << line.second << std::endl; } file.close(); - success = true; } else { throw FileIOError("Could not open file: '" + filepath + "'", "writeKeyValueFile"); } - return success; } - template bool writeKeyValueFile, std::equal_to>(const std::string&, const std::unordered_map, std::equal_to>&); - template bool writeKeyValueFile>(const std::string&, const std::unordered_map>&); + template void writeKeyValueFile, std::equal_to>(const std::string&, const std::unordered_map, std::equal_to>&); + template void writeKeyValueFile>(const std::string&, const std::unordered_map>&); using pairSS = std::pair; @@ -40,7 +41,6 @@ namespace gz { inline void insert(umapSS& t, pairSS&& p) { t.emplace(p); } inline void insert(mapSS& t, pairSS&& p) { t.emplace(p); } - template T readKeyValueFile(const std::string& filepath, bool removeSpaces) { T attr; @@ -78,4 +78,29 @@ namespace gz { template mapSS readKeyValueFile(const std::string&, bool); template vecSS readKeyValueFile(const std::string&, bool); + + +// +// BINARY-FILE +// + std::vector readBinaryFile(const std::string& filepath) { + std::ifstream file(filepath, std::ios::ate | std::ios::binary); + + std::vector buffer; + if (file.is_open()) { + size_t fileSize = file.tellg(); + buffer.resize(fileSize); + file.seekg(0); + file.read(buffer.data(), fileSize); + + } + else { + throw FileIOError("Could not open file: '" + filepath + "'", "readBinaryFile"); + } + file.close(); + + return buffer; + } + + } diff --git a/src/file_io.hpp b/src/file_io.hpp index 4199f3f..361dc01 100644 --- a/src/file_io.hpp +++ b/src/file_io.hpp @@ -1,6 +1,7 @@ #pragma once -#include "util/string_conversion.hpp" +/* #include "util/string_conversion.hpp" */ +#include "util/string.hpp" #include #include @@ -17,7 +18,7 @@ namespace gz { * @see @ref fio_t_key_value "Key-Value filetype" */ template - bool writeKeyValueFile(const std::string& filepath, const std::unordered_map& content); + void writeKeyValueFile(const std::string& filepath, const std::unordered_map& content); template concept ReadKeyValueFileImplemented = @@ -27,11 +28,18 @@ namespace gz { /** * @brief Read a file that contains key = value pairs - * @throws FileIOError + * @throws FileIOError if the file can not be opened * @see @ref fio_t_key_value "Key-Value filetype" */ template - T readKeyValueFile(const std::string& filepath, bool removeSpaces=false); + [[nodiscard]] T readKeyValueFile(const std::string& filepath, bool removeSpaces=false); + + + /** + * @brief Read a binary file and return the 8-bit words in a vector + * @throws FileIOError if the file can not be opened + */ + [[nodiscard]] std::vector readBinaryFile(const std::string& filepath); } /**