forked from kbinani/libvsq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamWriter.hpp
More file actions
84 lines (73 loc) · 2.05 KB
/
StreamWriter.hpp
File metadata and controls
84 lines (73 loc) · 2.05 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* StreamWriter.hpp
* Copyright © 2012 kbinani
*
* This file is part of libvsq.
*
* libvsq is free software; you can redistribute it and/or
* modify it under the terms of the BSD License.
*
* libvsq is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef __StreamWriter_hpp__
#define __StreamWriter_hpp__
#include "vsqglobal.hpp"
#include "TextOutputStream.hpp"
#include "OutputStream.hpp"
#include "FileOutputStream.hpp"
#include <fstream>
VSQ_BEGIN_NAMESPACE
/**
* @brief ファイルへの出力を行う TextOutputStream の実装
*/
class StreamWriter : public TextOutputStream{
private:
VSQ_NS::OutputStream *stream;
VSQ_NS::OutputStream *deleteInDestructor;
public:
/**
* @brief 出力先のファイルパスを指定して初期化する
* @param filePath 出力先のファイルパス
*/
explicit StreamWriter(const std::string &filePath) {
try {
stream = new FileOutputStream(filePath);
} catch(OutputStream::IOException) {
throw TextOutputStream::IOException();
}
deleteInDestructor = stream;
}
/**
* @brief Initialize writer by stream.
* @param stream A stream. This stream is automatically closed
* when 'close' is called (however, not to be deleted)
*/
explicit StreamWriter(OutputStream *stream) {
this->stream = stream;
this->deleteInDestructor = 0;
}
~StreamWriter(){
close();
}
void close(){
if (stream) {
stream->close();
stream = 0;
}
if (deleteInDestructor) {
delete deleteInDestructor;
deleteInDestructor = 0;
}
}
void write(const std::string &text) {
if (stream) stream->write(text.c_str(), 0, text.length());
}
void writeLine(const std::string &text) {
write(text);
if (stream) stream->write(0x0A);
}
};
VSQ_END_NAMESPACE
#endif