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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
#include "coding/file_writer.hpp"
#include "coding/internal/file_data.hpp"
FileWriter::FileWriter(FileWriter && rhs)
: m_pFileData(move(rhs.m_pFileData)), m_bTruncOnClose(rhs.m_bTruncOnClose)
{
}
FileWriter::FileWriter(string const & fileName, FileWriter::Op op, bool bTruncOnClose)
: m_pFileData(new fdata_t(fileName, static_cast<fdata_t::Op>(op))), m_bTruncOnClose(bTruncOnClose)
{
}
FileWriter::~FileWriter()
{
if (m_pFileData)
{
Flush();
if (m_bTruncOnClose)
m_pFileData->Truncate(Pos());
}
}
uint64_t FileWriter::Pos() const
{
return m_pFileData->Pos();
}
void FileWriter::Seek(uint64_t pos)
{
ASSERT_GREATER_OR_EQUAL(pos, 0, ());
m_pFileData->Seek(pos);
}
void FileWriter::Write(void const * p, size_t size)
{
m_pFileData->Write(p, size);
}
void FileWriter::WritePaddingByEnd(size_t factor) { WritePadding(Size(), factor); }
void FileWriter::WritePaddingByPos(size_t factor) { WritePadding(Pos(), factor); }
string const & FileWriter::GetName() const
{
return m_pFileData->GetName();
}
uint64_t FileWriter::Size() const
{
return m_pFileData->Size();
}
void FileWriter::Flush()
{
m_pFileData->Flush();
}
void FileWriter::Reserve(uint64_t size)
{
if (size > 0)
{
m_pFileData->Seek(size-1);
uint8_t b = 0;
m_pFileData->Write(&b, 1);
}
}
void FileWriter::DeleteFileX(string const & fName)
{
(void)my::DeleteFileX(fName);
}
void FileWriter::WritePadding(uint64_t offset, uint64_t factor)
{
ASSERT(factor > 1, ());
uint64_t const padding = ((offset + factor - 1) / factor) * factor - offset;
if (!padding)
return;
vector<uint8_t> buffer(padding);
Write(buffer.data(), buffer.size());
}
|