Added readBinaryFile
This commit is contained in:
parent
4c7cae9d42
commit
9eb1a7fcb0
@ -1,4 +1,5 @@
|
|||||||
#include "file_io.hpp"
|
#include "file_io.hpp"
|
||||||
|
|
||||||
#include "exceptions.hpp"
|
#include "exceptions.hpp"
|
||||||
#include "util/string.hpp"
|
#include "util/string.hpp"
|
||||||
|
|
||||||
@ -10,9 +11,11 @@
|
|||||||
|
|
||||||
namespace gz {
|
namespace gz {
|
||||||
|
|
||||||
|
//
|
||||||
|
// KEY-VALUE FILE
|
||||||
|
//
|
||||||
template<typename Hash, typename Pred>
|
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) {
|
||||||
bool success = false;
|
|
||||||
std::ofstream file(filepath.c_str());
|
std::ofstream file(filepath.c_str());
|
||||||
if (file.is_open()) {
|
if (file.is_open()) {
|
||||||
file << "# Written by writeKeyValueFile" << std::endl;
|
file << "# Written by writeKeyValueFile" << std::endl;
|
||||||
@ -20,15 +23,13 @@ namespace gz {
|
|||||||
file << line.first << " = " << line.second << std::endl;
|
file << line.first << " = " << line.second << std::endl;
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
success = true;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw FileIOError("Could not open file: '" + filepath + "'", "writeKeyValueFile");
|
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 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 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<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>;
|
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(umapSS& t, pairSS&& p) { t.emplace(p); }
|
||||||
inline void insert(mapSS& t, pairSS&& p) { t.emplace(p); }
|
inline void insert(mapSS& t, pairSS&& p) { t.emplace(p); }
|
||||||
|
|
||||||
|
|
||||||
template<ReadKeyValueFileImplemented T>
|
template<ReadKeyValueFileImplemented T>
|
||||||
T readKeyValueFile(const std::string& filepath, bool removeSpaces) {
|
T readKeyValueFile(const std::string& filepath, bool removeSpaces) {
|
||||||
T attr;
|
T attr;
|
||||||
@ -78,4 +78,29 @@ namespace gz {
|
|||||||
template mapSS readKeyValueFile<mapSS>(const std::string&, bool);
|
template mapSS readKeyValueFile<mapSS>(const std::string&, bool);
|
||||||
template vecSS readKeyValueFile<vecSS>(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "util/string_conversion.hpp"
|
/* #include "util/string_conversion.hpp" */
|
||||||
|
#include "util/string.hpp"
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
@ -17,7 +18,7 @@ namespace gz {
|
|||||||
* @see @ref fio_t_key_value "Key-Value filetype"
|
* @see @ref fio_t_key_value "Key-Value filetype"
|
||||||
*/
|
*/
|
||||||
template<typename Hash, typename Pred>
|
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>
|
template<typename T>
|
||||||
concept ReadKeyValueFileImplemented =
|
concept ReadKeyValueFileImplemented =
|
||||||
@ -27,11 +28,18 @@ namespace gz {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Read a file that contains key = value pairs
|
* @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"
|
* @see @ref fio_t_key_value "Key-Value filetype"
|
||||||
*/
|
*/
|
||||||
template<ReadKeyValueFileImplemented T>
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user