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