-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileWriter.h
More file actions
65 lines (50 loc) · 1.63 KB
/
FileWriter.h
File metadata and controls
65 lines (50 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#pragma once
#include <filesystem>
#include <fstream>
#include <string_view>
#include "swap.h"
#include "types.h"
class FileWriter
{
private:
std::ofstream filestream;
public:
FileWriter(std::filesystem::path filepath) : filestream(filepath, std::ios::binary) {}
template <typename T>
inline void writeBE(T data)
{
static_assert(std::is_arithmetic<T>::value,
"function only makes sense with arithmetic types");
Common::swap<sizeof(data)>(reinterpret_cast<u8*>(&data));
write(data);
}
template <typename T>
inline void write(T data)
{
static_assert(std::is_arithmetic<T>::value,
"function only makes sense with arithmetic types");
filestream.write(reinterpret_cast<const char*>(&data), sizeof(data));
}
inline void writeString(const std::string& data)
{
filestream.write(data.c_str(), data.size());
filestream << '\0'; // Null terminated
}
inline void write(const char* data, size_t size) { filestream.write(data, size); }
inline size_t position() { return static_cast<size_t>(filestream.tellp()); }
inline void seek(size_t position) { filestream.seekp(static_cast<size_t>(position), std::ios::beg); }
inline void padToAlignment(size_t alignment)
{
if (alignment == 0)
{
return;
}
auto pos = position();
auto count = (~(alignment - 1U) & (alignment + pos) - 1U) - pos;
u8 b = 0;
for (; count > 0; --count)
{
filestream.write(reinterpret_cast<const char*>(&b), sizeof(b));
}
}
};