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 
28 /** @brief Text streaming class that buffers data.
29  *
30  * Simpler version of std::ostringstream that has much better
31  * performance.
32  */
33 class TextStream final
34 {
35  static const int INITIAL_CAPACITY = 4096;
36  public:
37  /** Creates an empty stream object.
38  */
39  TextStream()
40  {
41  m_buffer.reserve(INITIAL_CAPACITY);
42  }
43  /** Create a text stream object for writing to a std::ostream.
44  * @note data is buffered until flush() is called or the object is destroyed.
45  */
46  TextStream(std::ostream *s) : m_s(s)
47  {
49  }
50  /** Create a text stream, initializing the buffer with string \a s
51  */
52  TextStream(const std::string &s) : m_buffer(s)
53  {
54  m_buffer.reserve(s.length()+INITIAL_CAPACITY);
55  }
56 
57  /** Writes any data that is buffered to the attached std::ostream */
58  ~TextStream() { flush(); }
59 
60  TextStream(const TextStream &) = delete;
61  TextStream &operator=(const TextStream &) = delete;
62 
63  /** Sets or changes the std::ostream to write to.
64  * @note Any data already buffered will be flushed.
65  */
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 
80  /** Returns the attached std::ostream object.
81  * @see setStream()
82  */
83  std::ostream *stream() const
84  {
85  return m_s;
86  }
87 
88  FILE *file() const
89  {
90  return m_f;
91  }
92 
93  /** Adds a character to the stream */
94  TextStream &operator<<( char c)
95  {
97  return static_cast<TextStream&>(*this);
98  }
99 
100  /** Adds a C-style string to the stream */
101  TextStream &operator<<( const char *s)
102  {
103  if (s) m_buffer+=s;
104  return static_cast<TextStream&>(*this);
105  }
106 
107  /** Adds a QCString to the stream */
108  TextStream &operator<<( const QCString &s )
109  {
110  m_buffer+=s.str();
111  return static_cast<TextStream&>(*this);
112  }
113 
114  /** Adds a std::string to the stream */
115  TextStream &operator<<( const std::string &s )
116  {
117  m_buffer+=s;
118  return static_cast<TextStream&>(*this);
119  }
120 
121  /** Adds a signed short integer to the stream */
122  TextStream &operator<<( signed short i)
123  {
124  output_int32(i,i<0);
125  return static_cast<TextStream&>(*this);
126  }
127 
128  /** Adds a unsigned short integer to the stream */
129  TextStream &operator<<( unsigned short i)
130  {
131  output_int32(i,false);
132  return static_cast<TextStream&>(*this);
133  }
134 
135  /** Adds a signed integer to the stream */
136  TextStream &operator<<( signed int i)
137  {
138  output_int32(i,i<0);
139  return static_cast<TextStream&>(*this);
140  }
141 
142  /** Adds a unsigned integer to the stream */
143  TextStream &operator<<( unsigned int i)
144  {
145  output_int32(i,false);
146  return static_cast<TextStream&>(*this);
147  }
148 
149  /** Adds a float to the stream */
150  TextStream &operator<<( float f)
151  {
152  output_double((double)f);
153  return static_cast<TextStream&>(*this);
154  }
155 
156  /** Adds a double to the stream */
157  TextStream &operator<<( double d)
158  {
159  output_double(d);
160  return static_cast<TextStream&>(*this);
161  }
162 
163  /** Adds a array of character to the stream
164  * @param buf the character buffer
165  * @param len the number of characters in the buffer to write
166  */
167  void write(const char *buf,size_t len)
168  {
169  m_buffer.append(buf,len);
170  }
171 
172  /** Flushes the buffer. If a std::ostream is attached, the buffer's
173  * contents will be written to the stream.
174  */
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 
188  /** Clears any buffered data */
189  void clear()
190  {
191  m_buffer.clear();
192  }
193 
194  /** Return the contents of the buffer as a std::string object */
195  std::string str() const
196  {
197  return m_buffer;
198  }
199 
200  /** Sets the buffer's contents to string \a s.
201  * Any data already in the buffer will be flushed.
202  */
203  void str(const std::string &s)
204  {
205  flush();
206  m_buffer=s;
207  }
208 
209  /** Sets the buffer's contents to string \a s
210  * Any data already in the buffer will be flushed.
211  */
212  void str(const char *s)
213  {
214  flush();
215  if (s) m_buffer=s;
216  }
217 
218  /** Returns true iff the buffer is empty */
219  bool empty() const
220  {
221  return m_buffer.empty();
222  }
223 
224  private:
225  /** Writes a string representation of an integer to the buffer
226  * @param n the absolute value of the integer
227  * @param neg indicates if the integer is negative
228  */
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
TextStream::output_int32
void output_int32(uint32_t n, bool neg)
Writes a string representation of an integer to the buffer
Definition: textstream.h:242
TextStream::m_f
FILE * m_f
Definition: textstream.h:263
TextStream::setStream
void setStream(std::ostream *s)
Sets or changes the std::ostream to write to.
Definition: textstream.h:79
TextStream::clear
void clear()
Clears any buffered data
Definition: textstream.h:202
TextStream::m_buffer
std::string m_buffer
Definition: textstream.h:261
TextStream::setFile
void setFile(FILE *f)
Definition: textstream.h:86
TextStream::stream
std::ostream * stream() const
Returns the attached std::ostream object.
Definition: textstream.h:96
QCString::str
std::string str() const
Definition: qcstring.h:442
TextStream
Text streaming class that buffers data.
Definition: textstream.h:33
qcstring.h
TextStream::empty
bool empty() const
Returns true iff the buffer is empty
Definition: textstream.h:232
TextStream::flush
void flush()
Flushes the buffer.
Definition: textstream.h:188
TextStream::TextStream
TextStream()
Creates an empty stream object.
Definition: textstream.h:52
TextStream::file
FILE * file() const
Definition: textstream.h:101
TextStream::INITIAL_CAPACITY
static const int INITIAL_CAPACITY
Definition: textstream.h:48
TextStream::m_s
std::ostream * m_s
Definition: textstream.h:262
TextStream::operator=
TextStream & operator=(const TextStream &)=delete
TextStream::str
std::string str() const
Return the contents of the buffer as a std::string object
Definition: textstream.h:208
TextStream::~TextStream
~TextStream()
Writes any data that is buffered to the attached std::ostream
Definition: textstream.h:71
TextStream::output_double
void output_double(double d)
Definition: textstream.h:255
TextStream::operator<<
TextStream & operator<<(char c)
Adds a character to the stream
Definition: textstream.h:107
TextStream::write
void write(const char *buf, size_t len)
Adds a array of character to the stream
Definition: textstream.h:180
QCString
This is an alternative implementation of QCString.
Definition: qcstring.h:108