ODIN
tjstream.h
1 /***************************************************************************
2  tjstream.h - description
3  -------------------
4  begin : Sun Jun 13 2004
5  copyright : (C) 2000-2021 by Thies H. Jochimsen
6  email : thies@jochimsen.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef TJSTREAM_H
19 #define TJSTREAM_H
20 
21 
22 #include <tjutils/tjstd.h>
23 
24 // Macros, includes and typedefs are handled in tjstd.h to avoid circular dependencies
25 
26 
27 typedef STD_ostream& (*streammanipulator)(STD_ostream&);
28 
29 
35 #ifdef STREAM_REPLACEMENT
36 
37 class tjstd_ostream {
38 
39  public:
40  tjstd_ostream() {}
41  virtual ~tjstd_ostream() {}
42 
43  inline tjstd_ostream& operator<<(char v) {append2buff(STD_string(1,v)); return *this;}
44  inline tjstd_ostream& operator<<(unsigned char v) {append2buff(STD_string(1,v)); return *this;}
45  inline tjstd_ostream& operator<<(signed char v) {append2buff(STD_string(1,v)); return *this;}
46  inline tjstd_ostream& operator<<(const char* v) {append2buff(v); return *this;}
47  inline tjstd_ostream& operator<<(int v) {buff_int(v); return *this;}
48  inline tjstd_ostream& operator<<(unsigned int v) {buff_int(v); return *this;}
49  inline tjstd_ostream& operator<<(long v) {buff_int(v); return *this;}
50  inline tjstd_ostream& operator<<(unsigned long v) {buff_int(v); return *this;}
51 #ifdef HAVE_LONG_LONG
52  inline tjstd_ostream& operator<<(long long v) {buff_int(v); return *this;}
53 #endif
54  inline tjstd_ostream& operator<<(short v) {buff_int(v); return *this;}
55  inline tjstd_ostream& operator<<(unsigned short v){buff_int(v); return *this;}
56  inline tjstd_ostream& operator<<(bool v) {if(v) append2buff("true"); else append2buff("false"); return *this;}
57  inline tjstd_ostream& operator<<(double v) {buff_float(v); return *this;}
58  inline tjstd_ostream& operator<<(float v) {buff_float(v); return *this;}
59  inline tjstd_ostream& operator<<(streammanipulator v) {v(*this); return *this;}
60 
61 
62  private:
63 
64  // disable copying
65  tjstd_ostream(const tjstd_ostream&) {}
66  tjstd_ostream& operator = (const tjstd_ostream&) {return *this;}
67 
68  virtual void append2buff(const STD_string& str) {} // do nothing by default, i.e. for cout/cerr
69 
70  void buff_float(float f);
71  void buff_int(int i);
72 
73 };
74 
76 
77 class tjstd_ostringstream : public tjstd_ostream {
78 
79  public:
80  tjstd_ostringstream() {}
81  ~tjstd_ostringstream() {}
82 
83  const STD_string& str() const {return buff;}
84 
85  private:
86 
87  // disable copying
88  tjstd_ostringstream(const tjstd_ostringstream&) {}
89  tjstd_ostringstream& operator = (const tjstd_ostringstream&) {return *this;}
90 
91  // overloaded from tjstd_ostream
92  void append2buff(const STD_string& str) {buff+=str;}
93 
94  STD_string buff;
95 };
96 
98 
99 class tjstd_ofstream : public tjstd_ostream {
100 
101  public:
102 
103  tjstd_ofstream(const char* filename="");
104  ~tjstd_ofstream() {close();}
105 
106  void close();
107 
108  bool bad() {return !file_ptr;}
109 
110  private:
111 
112  // disable default construction and copying
113  tjstd_ofstream(const tjstd_ofstream&) {}
114  tjstd_ofstream& operator = (const tjstd_ofstream&) {return *this;}
115 
116  // overloaded from tjstd_ostream
117  void append2buff(const STD_string& str);
118 
119  void* file_ptr;
120 
121 };
122 
127 
128 
129 class tjstd_istream {
130 
131  public:
132  tjstd_istream() {}
133 
134  inline tjstd_istream& operator>>(char) {return *this;}
135  inline tjstd_istream& operator>>(unsigned char) {return *this;}
136  inline tjstd_istream& operator>>(signed char) {return *this;}
137  inline tjstd_istream& operator>>(const char *) {return *this;}
138  inline tjstd_istream& operator>>(int) {return *this;}
139  inline tjstd_istream& operator>>(unsigned int) {return *this;}
140  inline tjstd_istream& operator>>(long) {return *this;}
141  inline tjstd_istream& operator>>(unsigned long) {return *this;}
142 #ifdef HAVE_LONG_LONG
143  inline tjstd_istream& operator>>(long long) {return *this;}
144 #endif
145  inline tjstd_istream& operator>>(short) {return *this;}
146  inline tjstd_istream& operator>>(unsigned short){return *this;}
147  inline tjstd_istream& operator>>(bool) {return *this;}
148  inline tjstd_istream& operator>>(double) {return *this;}
149  inline tjstd_istream& operator>>(float) {return *this;}
150 
151  inline tjstd_istream& get(char&) {return *this;}
152  inline tjstd_istream& putback(char) {return *this;}
153 
154  operator void* () const {return 0;} // stream is always empty
155 
156 };
157 
158 inline tjstd_istream& tjstd_getline(tjstd_istream& is, STD_string&) {return is;}
159 
160 
161 
163 
164 class tjstd_ifstream : public tjstd_istream {
165 
166  public:
167  tjstd_ifstream(const char* ="") {}
168  void close() {}
169  bool bad() {return false;}
170 };
171 
173 
174 
175 static tjstd_ostream tjstd_cout;
176 static tjstd_ostream tjstd_cerr;
177 static tjstd_istream tjstd_cin;
178 
179 inline STD_ostream& tjstd_endl(STD_ostream& os) {return os << "\n";}
180 inline STD_ostream& tjstd_flush(STD_ostream& os) {return os;}
181 inline const char* tjstd_setprecision(unsigned int) {return "";}
182 
183 #endif
184 
187 #endif
188