1 /*
2 BStone: A Source port of
3 Blake Stone: Aliens of Gold and Blake Stone: Planet Strike
4 
5 Copyright (c) 1992-2013 Apogee Entertainment, LLC
6 Copyright (c) 2013-2015 Boris I. Bendovsky (bibendovsky@hotmail.com)
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the
20 Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 */
23 
24 
25 //
26 // A text writer for a stream.
27 //
28 
29 
30 #include "bstone_text_writer.h"
31 
32 
33 namespace bstone
34 {
35 
36 
TextWriter()37 TextWriter::TextWriter()
38     :
39     stream_{}
40 {
41 }
42 
TextWriter(IStream * stream)43 TextWriter::TextWriter(
44     IStream* stream)
45     :
46     TextWriter{}
47 {
48     static_cast<void>(open(stream));
49 }
50 
51 
open(IStream * stream)52 bool TextWriter::open(
53     IStream* stream)
54 {
55     if (!stream)
56     {
57         return false;
58     }
59 
60     stream_ = stream;
61 
62     return true;
63 }
64 
close()65 void TextWriter::close()
66 {
67     stream_ = nullptr;
68 }
69 
is_initialized() const70 bool TextWriter::is_initialized() const
71 {
72     return stream_ != nullptr;
73 }
74 
write(const std::string & string)75 bool TextWriter::write(
76     const std::string& string)
77 {
78     if (!is_initialized())
79     {
80         return false;
81     }
82 
83 
84     if (string.empty())
85     {
86         return true;
87     }
88 
89     return stream_->write(string.data(), static_cast<int>(string.length()));
90 }
91 
92 
93 } // bstone
94