Future proofed file read/write
This commit is contained in:
1 parent
699e2a4784
commit
2fc2170763
20 files changed
+690
-607
No files matched your search
@@ -0,0 +1,57 @@
|
||||
#ifndef GERMANAIRLINESVA_FILE_HELPERS_H
|
||||
#define GERMANAIRLINESVA_FILE_HELPERS_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace germanairlinesva
|
||||
{
|
||||
namespace file
|
||||
{
|
||||
static inline const std::string readString(std::ifstream &in,
|
||||
std::uint8_t length)
|
||||
{
|
||||
std::vector<char> value(length + 1);
|
||||
in.read(value.data(), length);
|
||||
return std::string(std::move(value.data()));
|
||||
}
|
||||
|
||||
static inline const std::string readString(std::ifstream &in)
|
||||
{
|
||||
std::uint8_t length;
|
||||
in.read(reinterpret_cast<char *>(&length), 1);
|
||||
std::vector<char> value(length + 1);
|
||||
in.read(value.data(), length);
|
||||
return std::string(std::move(value.data()));
|
||||
}
|
||||
|
||||
template <typename T> static inline const T read(std::ifstream &in)
|
||||
{
|
||||
T value;
|
||||
in.read(reinterpret_cast<char *>(&value), sizeof(value));
|
||||
return value;
|
||||
}
|
||||
|
||||
static inline void writeString(std::ofstream &out, std::string value)
|
||||
{
|
||||
std::uint8_t length = value.length();
|
||||
out.write(reinterpret_cast<char *>(&length), sizeof(length));
|
||||
out.write(value.data(), length);
|
||||
}
|
||||
|
||||
static inline void
|
||||
writeString(std::ofstream &out, std::string value, std::uint8_t length)
|
||||
{
|
||||
out.write(value.data(), length);
|
||||
}
|
||||
|
||||
template <typename T> static inline void write(std::ofstream &out, T value)
|
||||
{
|
||||
out.write(reinterpret_cast<char *>(&value), sizeof(value));
|
||||
}
|
||||
} // namespace file
|
||||
} // namespace germanairlinesva
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user