doxygen
textstream.h
1 /******************************************************************************
2  *
3  * Copyright (C) 1997-2021 by Dimitri van Heesch.
4  *
5  * Permission to use, copy, modify, and distribute this software and its
6  * documentation under the terms of the GNU General Public License is hereby
7  * granted. No representations are made about the suitability of this software
8  * for any purpose. It is provided "as is" without express or implied warranty.
9  * See the GNU General Public License for more details.
10  *
11  * Documents produced by Doxygen are derivative works derived from the
12  * input used in their production; they are not affected by this license.
13  *
14  */
15 
16 #ifndef TEXTSTREAM_H
17 #define TEXTSTREAM_H
18 
19 #include <string>
20 #include <iostream>
21 #include <sstream>
22 #include <cstdint>
23 #include <cstdio>
24 #include <fstream>
25 #include <type_traits>
26 
27 #include "qcstring.h"
28 
34 class TextStream final
35 {
36  static const int INITIAL_CAPACITY = 4096;
37  public:
40  explicit TextStream(size_t capacity = INITIAL_CAPACITY)
41  {
42  m_buffer.reserve(capacity);
43  }
47  explicit TextStream(std::ostream *s) : m_s(s)
48  {
49  m_buffer.reserve(INITIAL_CAPACITY);
50  }
53  explicit TextStream(const std::string &s) : m_buffer(s)
54  {
55  m_buffer.reserve(s.length()+INITIAL_CAPACITY);
56  }
57 
59  ~TextStream() { flush(); }
60 
61  TextStream(const TextStream &) = delete;
62  TextStream &operator=(const TextStream &) = delete;
63  TextStream(TextStream &&) = default;
64  TextStream &operator=(TextStream &&) = delete;
65 
69  void setStream(std::ostream *s)
70  {
71  flush();
72  m_s = s;
73  m_f = nullptr;
74  }
75 
76  void setFile(FILE *f)
77  {
78  flush();
79  m_s = nullptr;
80  m_f = f;
81  }
82 
86  std::ostream *stream() const
87  {
88  return m_s;
89  }
90 
91  FILE *file() const
92  {
93  return m_f;
94  }
95 
98  {
99  m_buffer+=c;
100  return static_cast<TextStream&>(*this);
101  }
103  TextStream &operator<<( unsigned char c)
104  {
105  m_buffer+=c;
106  return static_cast<TextStream&>(*this);
107  }
108 
110  TextStream &operator<<( unsigned char *s)
111  {
112  if (s)
113  {
114  unsigned char *p = s;
115  while(*p)
116  {
117  m_buffer+=*p;
118  p++;
119  }
120  }
121  return static_cast<TextStream&>(*this);
122  }
123 
125  TextStream &operator<<( const char *s)
126  {
127  if (s) m_buffer+=s;
128  return static_cast<TextStream&>(*this);
129  }
130 
133  {
134  m_buffer+=s.str();
135  return static_cast<TextStream&>(*this);
136  }
137 
139  TextStream &operator<<( const std::string &s )
140  {
141  m_buffer+=s;
142  return static_cast<TextStream&>(*this);
143  }
144 
146  TextStream &operator<<( signed short i)
147  {
148  output_int32(i,i<0);
149  return static_cast<TextStream&>(*this);
150  }
151 
153  TextStream &operator<<( unsigned short i)
154  {
155  output_int32(i,false);
156  return static_cast<TextStream&>(*this);
157  }
158 
160  TextStream &operator<<( signed int i)
161  {
162  output_int32(i,i<0);
163  return static_cast<TextStream&>(*this);
164  }
165 
167  TextStream &operator<<( unsigned int i)
168  {
169  output_int32(i,false);
170  return static_cast<TextStream&>(*this);
171  }
172 
176  template<typename T,
177  typename std::enable_if<std::is_same<T,size_t>::value,T>::type* = nullptr
178  >
180  {
181  output_int32(static_cast<uint32_t>(i),false);
182  return static_cast<TextStream&>(*this);
183  }
184 
187  {
188  output_double(static_cast<double>(f));
189  return static_cast<TextStream&>(*this);
190  }
191 
193  TextStream &operator<<( double d)
194  {
195  output_double(d);
196  return static_cast<TextStream&>(*this);
197  }
198 
203  void write(const char *buf,size_t len)
204  {
205  m_buffer.append(buf,len);
206  }
207 
211  void flush()
212  {
213  if (m_s)
214  {
215  m_s->write(m_buffer.c_str(),m_buffer.length());
216  }
217  else if (m_f)
218  {
219  fwrite(m_buffer.c_str(),1,m_buffer.length(),m_f);
220  }
221  m_buffer.clear();
222  }
223 
225  void clear()
226  {
227  m_buffer.clear();
228  }
229 
231  std::string str() const
232  {
233  return m_buffer;
234  }
235 
239  void str(const std::string &s)
240  {
241  flush();
242  m_buffer=s;
243  }
244 
248  void str(const char *s)
249  {
250  flush();
251  if (s) m_buffer=s;
252  }
253 
255  bool empty() const
256  {
257  return m_buffer.empty();
258  }
259 
260  private:
265  void output_int32( uint32_t n, bool neg )
266  {
267  char buf[20];
268  char *p = &buf[19];
269  *p = '\0';
270  if ( neg )
271  {
272  n = static_cast<uint32_t>(-static_cast<int32_t>(n));
273  }
274  do { *--p = (static_cast<char>(n%10)) + '0'; n /= 10; } while ( n );
275  if ( neg ) *--p = '-';
276  m_buffer+=p;
277  }
278  void output_double( double d)
279  {
280  char buf[64];
281  snprintf(buf,64,"%f",d);
282  m_buffer+=buf;
283  }
284  std::string m_buffer;
285  std::ostream *m_s = nullptr;
286  FILE *m_f = nullptr;
287 };
288 
289 #endif
TextStream(size_t capacity=INITIAL_CAPACITY)
Creates an empty stream object.
Definition: textstream.h:40
void str(const char *s)
Sets the buffer&#39;s contents to string s Any data already in the buffer will be flushed.
Definition: textstream.h:248
~TextStream()
Writes any data that is buffered to the attached std::ostream.
Definition: textstream.h:59
Text streaming class that buffers data.
Definition: textstream.h:34
TextStream & operator<<(char c)
Adds a character to the stream.
Definition: textstream.h:97
TextStream & operator<<(signed int i)
Adds a signed integer to the stream.
Definition: textstream.h:160
TextStream & operator<<(unsigned short i)
Adds a unsigned short integer to the stream.
Definition: textstream.h:153
TextStream & operator<<(unsigned char *s)
Adds an unsigned character string to the stream.
Definition: textstream.h:110
TextStream & operator<<(unsigned int i)
Adds a unsigned integer to the stream.
Definition: textstream.h:167
void flush()
Flushes the buffer.
Definition: textstream.h:211
void str(const std::string &s)
Sets the buffer&#39;s contents to string s.
Definition: textstream.h:239
TextStream & operator<<(double d)
Adds a double to the stream.
Definition: textstream.h:193
TextStream & operator<<(const std::string &s)
Adds a std::string to the stream.
Definition: textstream.h:139
std::ostream * stream() const
Returns the attached std::ostream object.
Definition: textstream.h:86
std::string str() const
Return the contents of the buffer as a std::string object.
Definition: textstream.h:231
void write(const char *buf, size_t len)
Adds a array of character to the stream.
Definition: textstream.h:203
TextStream & operator<<(signed short i)
Adds a signed short integer to the stream.
Definition: textstream.h:146
TextStream & operator<<(float f)
Adds a float to the stream.
Definition: textstream.h:186
TextStream & operator<<(const QCString &s)
Adds a QCString to the stream.
Definition: textstream.h:132
bool empty() const
Returns true iff the buffer is empty.
Definition: textstream.h:255
TextStream(const std::string &s)
Create a text stream, initializing the buffer with string s.
Definition: textstream.h:53
void setStream(std::ostream *s)
Sets or changes the std::ostream to write to.
Definition: textstream.h:69
TextStream(std::ostream *s)
Create a text stream object for writing to a std::ostream.
Definition: textstream.h:47
This is an alternative implementation of QCString.
Definition: qcstring.h:93
void clear()
Clears any buffered data.
Definition: textstream.h:225
TextStream & operator<<(T i)
Adds a size_t integer to the stream.
Definition: textstream.h:179
TextStream & operator<<(unsigned char c)
Adds an unsigned character to the stream.
Definition: textstream.h:103
TextStream & operator<<(const char *s)
Adds a C-style string to the stream.
Definition: textstream.h:125