41 lines
1.2 KiB
C++
41 lines
1.2 KiB
C++
#pragma once
|
|
#include <fstream>
|
|
#include <log.hpp>
|
|
#include <vector>
|
|
|
|
namespace Util {
|
|
FORCE_INLINE std::vector<u8> ReadFileBinary(const std::string &path) {
|
|
std::ifstream file(path, std::ios::binary);
|
|
return {std::istreambuf_iterator{file}, {}};
|
|
}
|
|
|
|
FORCE_INLINE void WriteFileBinary(const std::vector<u8> &data, const std::string &path) {
|
|
std::ofstream file(path, std::ios::binary);
|
|
std::copy(data.begin(), data.end(), std::ostreambuf_iterator{file});
|
|
}
|
|
|
|
FORCE_INLINE void WriteFileBinary(const u8 *data, const size_t size, const std::string &path) {
|
|
FILE *out = fopen(path.c_str(), "wb");
|
|
fwrite(data, size, 1, out);
|
|
fclose(out);
|
|
}
|
|
|
|
template <size_t Size>
|
|
FORCE_INLINE void WriteFileBinary(const std::array<u8, Size> &data, const std::string &path) {
|
|
std::ofstream file(path, std::ios::binary);
|
|
std::copy(data.begin(), data.end(), std::ostreambuf_iterator{file});
|
|
}
|
|
|
|
FORCE_INLINE size_t NextPow2(size_t num) {
|
|
// Taken from "Bit Twiddling Hacks" by Sean Anderson:
|
|
// https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
|
|
--num;
|
|
num |= num >> 1;
|
|
num |= num >> 2;
|
|
num |= num >> 4;
|
|
num |= num >> 8;
|
|
num |= num >> 16;
|
|
return num + 1;
|
|
}
|
|
} // namespace Util
|