-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamLineWriter.h
87 lines (81 loc) · 2.42 KB
/
StreamLineWriter.h
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
85
86
87
#pragma once
#include "FileStream.h"
#include <string>
#include <Windows.h>
class StreamLineWriter
{
public:
static constexpr auto kEndOfLine = "\r\n";
public:
enum class Encoding
{
UTF8
};
StreamLineWriter(const std::wstring& filepath, bool append)
: StreamLineWriter(filepath, Encoding::UTF8, append) { }
StreamLineWriter(const std::wstring& filepath, Encoding encoding, bool append)
: filestream_(FileStream(filepath, append
? FileStream::Mode::Append
: FileStream::Mode::Truncate)), encoding_(encoding) { }
StreamLineWriter(const StreamLineWriter&) = delete;
StreamLineWriter(StreamLineWriter&& other)
: filestream_(std::move(other.filestream_)), encoding_(other.encoding_) { }
StreamLineWriter& operator=(StreamLineWriter&& other)
{
if (this != &other)
{
Close();
filestream_ = std::move(other.filestream_);
encoding_ = other.encoding_;
}
return *this;
}
~StreamLineWriter()
{
Close();
}
BOOL autoflush() const { return autoflush_; };
void set_autoflush(BOOL autoflush) { autoflush_ = autoflush; };
void Write(const std::wstring& line);
void WriteLine(const std::wstring& line);
void Close()
{
filestream_.Close();
}
private:
FileStream filestream_;
Encoding encoding_;
BOOL autoflush_{};
private:
void WriteEOL()
{
filestream_.Write(reinterpret_cast<PBYTE>(const_cast<char*>(kEndOfLine)), 2);
}
};
inline void StreamLineWriter::Write(const std::wstring& line)
{
if (line.size() > 0)
{
if (encoding_ == Encoding::UTF8)
{
auto cbMultiByte = WideCharToMultiByte(CP_UTF8, 0, line.c_str(), -1, NULL, 0, NULL, NULL);
auto bytes = reinterpret_cast<LPSTR>(HeapAlloc(GetProcessHeap(), 0, cbMultiByte));
cbMultiByte = WideCharToMultiByte(CP_UTF8, 0, line.c_str(), -1, bytes, cbMultiByte, NULL, NULL);
filestream_.Write(reinterpret_cast<PBYTE>(bytes), cbMultiByte - 1);
HeapFree(GetProcessHeap(), 0, bytes);
}
else
{
throw std::runtime_error("StreamLineWriter::Write() error: the selected encoding is not supported.");
}
}
}
inline void StreamLineWriter::WriteLine(const std::wstring& line)
{
Write(line);
WriteEOL();
if (autoflush_)
{
filestream_.Flush();
}
}