Lolly 1.4.28
Loading...
Searching...
No Matches
tm_ostream.cpp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : tm_ostream.cpp
4 * DESCRIPTION: Output stream class
5 * COPYRIGHT : (C) 2009-2013 David MICHEL, Joris van der Hoeven
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11
12#include "tm_ostream.hpp"
13#include "fast_alloc.hpp"
14#include "string.hpp"
15
16#if defined(OS_WIN) || defined(OS_MINGW)
17#include <windows.h>
18#endif
19
20/******************************************************************************
21 * Routines for abstract base class
22 ******************************************************************************/
23
24tm_ostream_rep::tm_ostream_rep () : ref_count (0) {}
26void
28void
30bool
32 return false;
33}
34void
35tm_ostream_rep::write (const char*) {}
36
37/******************************************************************************
38 * Standard streams
39 ******************************************************************************/
40
43 bool is_w;
44 bool is_mine;
45
46public:
48 std_ostream_rep (char*);
51
52 bool is_writable () const;
53 void write (const char*);
54 void flush ();
55};
56
57std_ostream_rep::std_ostream_rep () : file (0), is_w (false), is_mine (false) {
58 is_w = true;
59 is_mine= true;
60}
61
63 : file (0), is_w (false), is_mine (false) {
64 file= fopen (fn, "w");
65 if (file) {
66 is_w = true;
67 is_mine= true;
68 }
69 else {
70 is_w = false;
71 is_mine= false;
72 }
73}
74
76 : file (0), is_w (false), is_mine (false) {
77#if defined(OS_WIN) || defined(OS_MINGW)
78 SetConsoleOutputCP (65001);
79#endif
80 file= f;
81 if (file) is_w= true;
82 else is_w= false;
83 is_mine= false;
84}
85
89
90bool
92 return is_w;
93}
94
95void
96std_ostream_rep::write (const char* s) {
97 if (file && is_w) {
98 if (0 <= fprintf (file, "%s", s)) {
99 const char* c= s;
100 while (*c != 0 && *c != '\n')
101 ++c;
102 if (*c == '\n') flush ();
103 }
104 else is_w= false;
105 }
106}
107
108void
110 if (file && is_w) fflush (file);
111}
112
113/******************************************************************************
114 * Buffered streams
115 ******************************************************************************/
116
118public:
120 string buf;
121
122public:
125
126 bool is_writable () const;
127 void write (const char*);
128};
129
132
134
135bool
137 return true;
138}
139
140void
142 buf << s;
143}
144
145/******************************************************************************
146 * Abstract user interface
147 ******************************************************************************/
148
153 INC_COUNT (this->rep);
154}
159 INC_COUNT (this->rep);
160}
167 return rep;
168}
171 INC_COUNT (x.rep);
172 DEC_COUNT (this->rep);
173 this->rep= x.rep;
174 return *this;
175}
176bool
178 return (&out == this);
179}
180
181void
183 rep->clear ();
184}
185
186void
188 rep->flush ();
189}
190
191void
196
197string
200 rep = ptr->master;
201 string r = ptr->buf;
202 INC_COUNT (rep);
203 DEC_COUNT (ptr);
204 return r;
205}
206
207void
209 INC_COUNT (x.rep);
210 DEC_COUNT (this->rep);
211 this->rep= x.rep;
212}
213
214/******************************************************************************
215 * Print methods for standard types
216 ******************************************************************************/
217
220 if (b) rep->write ("true");
221 else rep->write ("false");
222 return *this;
223}
224
227 static char _buf[8];
228 sprintf (_buf, "%c", c);
229 rep->write (_buf);
230 return *this;
231}
232
235 static char _buf[32];
236 sprintf (_buf, "%hd", sh);
237 rep->write (_buf);
238 return *this;
239}
240
242tm_ostream::operator<< (unsigned short ush) {
243 static char _buf[32];
244 sprintf (_buf, "%hu", ush);
245 rep->write (_buf);
246 return *this;
247}
248
251 static char _buf[64];
252 sprintf (_buf, "%d", i);
253 rep->write (_buf);
254 return *this;
255}
256
258tm_ostream::operator<< (unsigned int ui) {
259 static char _buf[64];
260 sprintf (_buf, "%u", ui);
261 rep->write (_buf);
262 return *this;
263}
264
267 static char _buf[64];
268 sprintf (_buf, "%ld", l);
269 rep->write (_buf);
270 return *this;
271}
272
274tm_ostream::operator<< (unsigned long ul) {
275 static char _buf[64];
276 sprintf (_buf, "%lu", ul);
277 rep->write (_buf);
278 return *this;
279}
280
282tm_ostream::operator<< (long long ll) {
283 static char _buf[64];
284 sprintf (_buf, "%lld", ll);
285 rep->write (_buf);
286 return *this;
287}
288
290tm_ostream::operator<< (unsigned long long ull) {
291 static char _buf[64];
292 sprintf (_buf, "%llu", ull);
293 rep->write (_buf);
294 return *this;
295}
296
299 static char _buf[32];
300 sprintf (_buf, "%g", f);
301 rep->write (_buf);
302 return *this;
303}
304
307 static char _buf[64];
308 sprintf (_buf, "%g", d);
309 rep->write (_buf);
310 return *this;
311}
312
314tm_ostream::operator<< (long double ld) {
315 static char _buf[128];
316 sprintf (_buf, "%Lg", ld);
317 rep->write (_buf);
318 return *this;
319}
320
322tm_ostream::operator<< (const char* s) {
323 rep->write (s);
324 return *this;
325}
326
328operator<< (tm_ostream& out, string a) {
329 int i, n= N (a);
330 if (n == 0) return out;
331 for (i= 0; i < n; i++)
332 out << a[i];
333 return out;
334}
335
336/******************************************************************************
337 * Standard output streams
338 ******************************************************************************/
339
342
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
blackbox b[13]
buffered_ostream_rep(tm_ostream_rep *master)
tm_ostream_rep * master
void write(const char *)
bool is_writable() const
#define DEC_COUNT(R)
Macro used to decrement the reference count for a structure object and delete it if the count reaches...
Definition classdef.hpp:84
#define INC_COUNT(R)
Macro used to increment the reference count for a structure object.
Definition classdef.hpp:74
The list class represents a linked list.
Definition list.hpp:48
void write(const char *)
bool is_writable() const
virtual ~tm_ostream_rep()
virtual void flush()
virtual void write(const char *)
virtual void clear()
virtual bool is_writable() const
string unbuffer()
tm_ostream & operator<<(bool)
tm_ostream_rep * operator->()
void buffer()
void redirect(tm_ostream x)
static tm_ostream private_cerr
bool operator==(tm_ostream &)
tm_ostream & operator=(tm_ostream x)
static tm_ostream private_cout
tm_ostream_rep * rep
C * tm_new()
base class of resources
Definition resource.hpp:23
tm_ostream & cerr
tm_ostream & cout
tm_ostream & operator<<(tm_ostream &out, string a)