Added readBinaryFile

This commit is contained in:
matthias@arch 2022-10-30 01:15:48 +02:00
parent 4c7cae9d42
commit 9eb1a7fcb0
2 changed files with 44 additions and 11 deletions

View File

@ -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<typename Hash, typename Pred>
bool writeKeyValueFile(const std::string& filepath, const std::unordered_map<std::string, std::string, Hash, Pred>& content) {
bool success = false;
void writeKeyValueFile(const std::string& filepath, const std::unordered_map<std::string, std::string, Hash, Pred>& 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::hash<std::string>, std::equal_to<std::string>>(const std::string&, const std::unordered_map<std::string, std::string, std::hash<std::string>, std::equal_to<std::string>>&);
template bool writeKeyValueFile<util::string_hash, std::equal_to<>>(const std::string&, const std::unordered_map<std::string, std::string, util::string_hash, std::equal_to<>>&);
template void writeKeyValueFile<std::hash<std::string>, std::equal_to<std::string>>(const std::string&, const std::unordered_map<std::string, std::string, std::hash<std::string>, std::equal_to<std::string>>&);
template void writeKeyValueFile<util::string_hash, std::equal_to<>>(const std::string&, const std::unordered_map<std::string, std::string, util::string_hash, std::equal_to<>>&);
using pairSS = std::pair<std::string, std::string>;
@ -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<ReadKeyValueFileImplemented T>
T readKeyValueFile(const std::string& filepath, bool removeSpaces) {
T attr;
@ -78,4 +78,29 @@ namespace gz {
template mapSS readKeyValueFile<mapSS>(const std::string&, bool);
template vecSS readKeyValueFile<vecSS>(const std::string&, bool);
//
// BINARY-FILE
//
std::vector<char> readBinaryFile(const std::string& filepath) {
std::ifstream file(filepath, std::ios::ate | std::ios::binary);
std::vector<char> 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;
}
}

View File

@ -1,6 +1,7 @@
#pragma once
#include "util/string_conversion.hpp"
/* #include "util/string_conversion.hpp" */
#include "util/string.hpp"
#include <map>
#include <unordered_map>
@ -17,7 +18,7 @@ namespace gz {
* @see @ref fio_t_key_value "Key-Value filetype"
*/
template<typename Hash, typename Pred>
bool writeKeyValueFile(const std::string& filepath, const std::unordered_map<std::string, std::string, Hash, Pred>& content);
void writeKeyValueFile(const std::string& filepath, const std::unordered_map<std::string, std::string, Hash, Pred>& content);
template<typename T>
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<ReadKeyValueFileImplemented T>
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<char> readBinaryFile(const std::string& filepath);
}
/**