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 
26 #include "qcstring.h"
27 
33 class TextStream final
34 {
35  static const int INITIAL_CAPACITY = 4096;
36  public:
40  {
41  m_buffer.reserve(INITIAL_CAPACITY);
42  }
46  TextStream(std::ostream *s) : m_s(s)
47  {
48  m_buffer.reserve(INITIAL_CAPACITY);
49  }
52  TextStream(const std::string &s) : m_buffer(s)
53  {
54  m_buffer.reserve(s.length()+INITIAL_CAPACITY);
55  }
56 
58  ~TextStream() { flush(); }
59 
60  TextStream(const TextStream &) = delete;
61  TextStream &operator=(const TextStream &) = delete;
62 
66  void setStream(std::ostream *s)
67  {
68  flush();
69  m_s = s;
70  m_f = nullptr;
71  }
72 
73  void setFile(FILE *f)
74  {
75  flush();
76  m_s = nullptr;
77  m_f = f;
78  }
79 
83  std::ostream *stream() const
84  {
85  return m_s;
86  }
87 
88  FILE *file() const
89  {
90  return m_f;
91  }
92 
95  {
96  m_buffer+=c;
97  return static_cast<TextStream&>(*this);
98  }
99 
101  TextStream &operator<<( const char *s)
102  {
103  if (s) m_buffer+=s;
104  return static_cast<TextStream&>(*this);
105  }
106 
109  {
110  m_buffer+=s.str();
111  return static_cast<TextStream&>(*this);
112  }
113 
115  TextStream &operator<<( const std::string &s )
116  {
117  m_buffer+=s;
118  return static_cast<TextStream&>(*this);
119  }
120 
122  TextStream &operator<<( signed short i)
123  {
124  output_int32(i,i<0);
125  return static_cast<TextStream&>(*this);
126  }
127 
129  TextStream &operator<<( unsigned short i)
130  {
131  output_int32(i,false);
132  return static_cast<TextStream&>(*this);
133  }
134 
136  TextStream &operator<<( signed int i)
137  {
138  output_int32(i,i<0);
139  return static_cast<TextStream&>(*this);
140  }
141 
143  TextStream &operator<<( unsigned int i)
144  {
145  output_int32(i,false);
146  return static_cast<TextStream&>(*this);
147  }
148 
151  {
152  output_double((double)f);
153  return static_cast<TextStream&>(*this);
154  }
155 
157  TextStream &operator<<( double d)
158  {
159  output_double(d);
160  return static_cast<TextStream&>(*this);
161  }
162 
167  void write(const char *buf,size_t len)
168  {
169  m_buffer.append(buf,len);
170  }
171 
175  void flush()
176  {
177  if (m_s)
178  {
179  m_s->write(m_buffer.c_str(),m_buffer.length());
180  }
181  else if (m_f)
182  {
183  fwrite(m_buffer.c_str(),1,m_buffer.length(),m_f);
184  }
185  m_buffer.clear();
186  }
187 
189  void clear()
190  {
191  m_buffer.clear();
192  }
193 
195  std::string str() const
196  {
197  return m_buffer;
198  }
199 
203  void str(const std::string &s)
204  {
205  flush();
206  m_buffer=s;
207  }
208 
212  void str(const char *s)
213  {
214  flush();
215  if (s) m_buffer=s;
216  }
217 
219  bool empty() const
220  {
221  return m_buffer.empty();
222  }
223 
224  private:
229  void output_int32( uint32_t n, bool neg )
230  {
231  char buf[20];
232  char *p = &buf[19];
233  *p = '\0';
234  if ( neg )
235  {
236  n = (uint32_t)(-(int32_t)n);
237  }
238  do { *--p = ((char)(n%10)) + '0'; n /= 10; } while ( n );
239  if ( neg ) *--p = '-';
240  m_buffer+=p;
241  }
242  void output_double( double d)
243  {
244  char buf[64];
245  snprintf(buf,64,"%f",d);
246  m_buffer+=buf;
247  }
248  std::string m_buffer;
249  std::ostream *m_s = nullptr;
250  FILE *m_f = nullptr;
251 };
252 
253 #endif
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:212
~TextStream()
Writes any data that is buffered to the attached std::ostream.
Definition: textstream.h:58
Text streaming class that buffers data.
Definition: textstream.h:33
TextStream & operator<<(char c)
Adds a character to the stream.
Definition: textstream.h:94
TextStream & operator<<(signed int i)
Adds a signed integer to the stream.
Definition: textstream.h:136
TextStream & operator<<(unsigned short i)
Adds a unsigned short integer to the stream.
Definition: textstream.h:129
TextStream()
Creates an empty stream object.
Definition: textstream.h:39
TextStream & operator<<(unsigned int i)
Adds a unsigned integer to the stream.
Definition: textstream.h:143
void flush()
Flushes the buffer.
Definition: textstream.h:175
void str(const std::string &s)
Sets the buffer&#39;s contents to string s.
Definition: textstream.h:203
TextStream & operator<<(double d)
Adds a double to the stream.
Definition: textstream.h:157
TextStream & operator<<(const std::string &s)
Adds a std::string to the stream.
Definition: textstream.h:115
std::ostream * stream() const
Returns the attached std::ostream object.
Definition: textstream.h:83
std::string str() const
Return the contents of the buffer as a std::string object.
Definition: textstream.h:195
void write(const char *buf, size_t len)
Adds a array of character to the stream.
Definition: textstream.h:167
TextStream & operator<<(signed short i)
Adds a signed short integer to the stream.
Definition: textstream.h:122
TextStream & operator<<(float f)
Adds a float to the stream.
Definition: textstream.h:150
TextStream & operator<<(const QCString &s)
Adds a QCString to the stream.
Definition: textstream.h:108
bool empty() const
Returns true iff the buffer is empty.
Definition: textstream.h:219
TextStream(const std::string &s)
Create a text stream, initializing the buffer with string s.
Definition: textstream.h:52
void setStream(std::ostream *s)
Sets or changes the std::ostream to write to.
Definition: textstream.h:66
TextStream(std::ostream *s)
Create a text stream object for writing to a std::ostream.
Definition: textstream.h:46
This is an alternative implementation of QCString.
Definition: qcstring.h:108
void clear()
Clears any buffered data.
Definition: textstream.h:189
TextStream & operator<<(const char *s)
Adds a C-style string to the stream.
Definition: textstream.h:101