1 /**
2 * @file OStream.cpp
3 * @brief Implementations of wrapper classes for C++ standard output streams.
4 * @author Akiya Jouraku
5 *
6 * <!--------------------------------------------------------------------------
7 * This file is part of libSBML. Please visit http://sbml.org for more
8 * information about SBML, and the latest version of libSBML.
9 *
10 * Copyright (C) 2020 jointly by the following organizations:
11 * 1. California Institute of Technology, Pasadena, CA, USA
12 * 2. University of Heidelberg, Heidelberg, Germany
13 * 3. University College London, London, UK
14 *
15 * Copyright (C) 2019 jointly by the following organizations:
16 * 1. California Institute of Technology, Pasadena, CA, USA
17 * 2. University of Heidelberg, Heidelberg, Germany
18 *
19 * Copyright (C) 2013-2018 jointly by the following organizations:
20 * 1. California Institute of Technology, Pasadena, CA, USA
21 * 2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
22 * 3. University of Heidelberg, Heidelberg, Germany
23 *
24 * Copyright (C) 2009-2013 jointly by the following organizations:
25 * 1. California Institute of Technology, Pasadena, CA, USA
26 * 2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
27 *
28 * Copyright (C) 2006-2008 by the California Institute of Technology,
29 * Pasadena, CA, USA
30 *
31 * Copyright (C) 2002-2005 jointly by the following organizations:
32 * 1. California Institute of Technology, Pasadena, CA, USA
33 * 2. Japan Science and Technology Agency, Japan
34 *
35 * This library is free software; you can redistribute it and/or modify it
36 * under the terms of the GNU Lesser General Public License as published by
37 * the Free Software Foundation. A copy of the license agreement is provided
38 * in the file named "LICENSE.txt" included with this software distribution
39 * and also available online as http://sbml.org/software/libsbml/license.html
40 * ---------------------------------------------------------------------- -->*/
41
42 #include "OStream.h"
43
44 /**
45 * Creates a new OStream object with one of standard output stream objects.
46 *
47 * @param sot a value from the StdOSType enumeration indicating
48 * the type of stream to create.
49 */
OStream(StdOSType sot)50 OStream::OStream (StdOSType sot)
51 {
52 switch (sot) {
53 case COUT:
54 Stream = &std::cout;
55 break;
56 case CERR:
57 Stream = &std::cerr;
58 break;
59 case CLOG:
60 Stream = &std::clog;
61 break;
62 default:
63 Stream = &std::cout;
64 }
65 }
66
67 /**
68 * Destructor.
69 */
~OStream()70 OStream::~OStream ()
71 {
72 }
73
74
75 /**
76 * Returns the stream object.
77 * <p>
78 * @return the stream object
79 */
80 std::ostream*
get_ostream()81 OStream::get_ostream ()
82 {
83 return Stream;
84 }
85
86
87 /**
88 * Writes an end-of-line character on this tream.
89 */
90 void
endl()91 OStream::endl ()
92 {
93 std::endl(*Stream);
94 }
95
96
97 /**
98 * Creates a new OFStream object for a file.
99 * <p>
100 * This opens the given file @p filename with the @p is_append flag
101 * (default is <code>false</code>), and creates an OFStream object
102 * instance that associates the file's content with an OStream object.
103 * <p>
104 * @param filename the name of the file to open
105 * @param is_append whether to open the file for appending (default:
106 * <code>false</code>, meaning overwrite the content instead)
107 */
OFStream(const std::string & filename,bool is_append)108 OFStream::OFStream (const std::string& filename, bool is_append)
109 {
110 if (is_append) {
111 Stream = new std::ofstream(filename.c_str(),std::ios_base::app);
112 }
113 else {
114 Stream = new std::ofstream(filename.c_str(),std::ios_base::out);
115 }
116 }
117
118
119 /**
120 * Destructor.
121 */
~OFStream()122 OFStream::~OFStream ()
123 {
124 delete Stream;
125 }
126
127
128 /**
129 * Opens a file and associates this stream object with it.
130 * <p>
131 * This method opens a given file @p filename with the given
132 * @p is_append flag (whose default value is <code>false</code>),
133 * and associates <i>this</i> stream object with the file's content.
134 * <p>
135 * @param filename the name of the file to open
136 * @param is_append whether to open the file for appending (default:
137 * <code>false</code>, meaning overwrite the content instead)
138 */
139 void
open(const std::string & filename,bool is_append)140 OFStream::open (const std::string& filename, bool is_append)
141 {
142 if (is_append) {
143 static_cast<std::ofstream*>(Stream)->open(filename.c_str(),std::ios_base::app);
144 }
145 else {
146 static_cast<std::ofstream*>(Stream)->open(filename.c_str(),std::ios_base::out);
147 }
148 }
149
150
151 /**
152 * Closes the file currently associated with this stream object.
153 */
154 void
close()155 OFStream::close ()
156 {
157 static_cast<std::ofstream*>(Stream)->close();
158 }
159
160
161 /**
162 * Returns <code>true</code> if this stream object is currently
163 * associated with a file.
164 * <p>
165 * @return <code>true</code> if the stream object is currently
166 * associated with a file, <code>false</code> otherwise
167 */
168 bool
is_open()169 OFStream::is_open ()
170 {
171 return static_cast<std::ofstream*>(Stream)->is_open();
172 }
173
174
175 /**
176 * Creates a new OStringStream object
177 */
OStringStream()178 OStringStream::OStringStream ()
179 {
180 Stream = new std::ostringstream();
181 }
182
183
184 /**
185 * Returns the copy of the string object currently assosiated
186 * with this <code>ostringstream</code> buffer.
187 * <p>
188 * @return a copy of the string object for this stream
189 */
190 std::string
str()191 OStringStream::str ()
192 {
193 return static_cast<std::ostringstream*>(Stream)->str();
194 }
195
196
197 /**
198 * Sets string @p s to the string object currently assosiated with
199 * this stream buffer.
200 * <p>
201 * @param s the string to write to this stream
202 */
203 void
str(const std::string & s)204 OStringStream::str (const std::string& s)
205 {
206 static_cast<std::ostringstream*>(Stream)->str(s.c_str());
207 }
208
209
210 /**
211 * Destructor.
212 */
~OStringStream()213 OStringStream::~OStringStream ()
214 {
215 delete Stream;
216 }
217
218