Doxygen
Loading...
Searching...
No Matches
TextStream Class Referencefinal

Text streaming class that buffers data. More...

#include <src/textstream.h>

Public Member Functions

 TextStream (size_t capacity=INITIAL_CAPACITY)
 Creates an empty stream object.
 
 TextStream (std::ostream *s)
 Create a text stream object for writing to a std::ostream.
 
 TextStream (const std::string &s)
 Create a text stream, initializing the buffer with string s.
 
 ~TextStream ()
 Writes any data that is buffered to the attached std::ostream.
 
void setStream (std::ostream *s)
 Sets or changes the std::ostream to write to.
 
void setFile (FILE *f)
 
std::ostream * stream () const
 Returns the attached std::ostream object.
 
FILE * file () const
 
TextStreamoperator<< (char c)
 Adds a character to the stream.
 
TextStreamoperator<< (unsigned char c)
 Adds an unsigned character to the stream.
 
TextStreamoperator<< (unsigned char *s)
 Adds an unsigned character string to the stream.
 
TextStreamoperator<< (const char *s)
 Adds a C-style string to the stream.
 
TextStreamoperator<< (const QCString &s)
 Adds a QCString to the stream.
 
TextStreamoperator<< (const std::string &s)
 Adds a std::string to the stream.
 
TextStreamoperator<< (signed short i)
 Adds a signed short integer to the stream.
 
TextStreamoperator<< (unsigned short i)
 Adds a unsigned short integer to the stream.
 
TextStreamoperator<< (signed int i)
 Adds a signed integer to the stream.
 
TextStreamoperator<< (unsigned int i)
 Adds a unsigned integer to the stream.
 
template<typename T , typename std::enable_if< std::is_same< T, size_t >::value, T >::type * = nullptr>
TextStreamoperator<< (T i)
 Adds a size_t integer to the stream.
 
TextStreamoperator<< (float f)
 Adds a float to the stream.
 
TextStreamoperator<< (double d)
 Adds a double to the stream.
 
void write (const char *buf, size_t len)
 Adds a array of character to the stream.
 
void flush ()
 Flushes the buffer.
 
void clear ()
 Clears any buffered data.
 
std::string str () const
 Return the contents of the buffer as a std::string object.
 
void str (const std::string &s)
 Sets the buffer's contents to string s.
 
void str (const char *s)
 Sets the buffer's contents to string s Any data already in the buffer will be flushed.
 
bool empty () const
 Returns true iff the buffer is empty.
 

Private Member Functions

void output_int32 (uint32_t n, bool neg)
 Writes a string representation of an integer to the buffer.
 
void output_double (double d)
 

Private Attributes

std::string m_buffer
 
std::ostream * m_s = nullptr
 
FILE * m_f = nullptr
 

Static Private Attributes

static const int INITIAL_CAPACITY = 4096
 

Detailed Description

Text streaming class that buffers data.

Simpler version of std::ostringstream that has much better performance.

Definition at line 35 of file textstream.h.

Constructor & Destructor Documentation

◆ TextStream() [1/3]

TextStream::TextStream ( size_t capacity = INITIAL_CAPACITY)
inlineexplicit

Creates an empty stream object.

Definition at line 41 of file textstream.h.

42 {
43 m_buffer.reserve(capacity);
44 }
std::string m_buffer
Definition textstream.h:282

References INITIAL_CAPACITY, and m_buffer.

Referenced by operator<<(), operator<<(), operator<<(), operator<<(), operator<<(), operator<<(), operator<<(), operator<<(), operator<<(), operator<<(), operator<<(), operator<<(), and operator<<().

◆ TextStream() [2/3]

TextStream::TextStream ( std::ostream * s)
inlineexplicit

Create a text stream object for writing to a std::ostream.

Note
data is buffered until flush() is called or the object is destroyed.

Definition at line 48 of file textstream.h.

48 : m_s(s)
49 {
51 }
std::ostream * m_s
Definition textstream.h:283
static const int INITIAL_CAPACITY
Definition textstream.h:37

References INITIAL_CAPACITY, m_buffer, and m_s.

◆ TextStream() [3/3]

TextStream::TextStream ( const std::string & s)
inlineexplicit

Create a text stream, initializing the buffer with string s.

Definition at line 54 of file textstream.h.

54 : m_buffer(s)
55 {
56 m_buffer.reserve(s.length()+INITIAL_CAPACITY);
57 }

References INITIAL_CAPACITY, and m_buffer.

◆ ~TextStream()

TextStream::~TextStream ( )
inline

Writes any data that is buffered to the attached std::ostream.

Definition at line 60 of file textstream.h.

60{ flush(); }
void flush()
Flushes the buffer.
Definition textstream.h:209

References flush().

Member Function Documentation

◆ clear()

void TextStream::clear ( )
inline

Clears any buffered data.

Definition at line 223 of file textstream.h.

224 {
225 m_buffer.clear();
226 }

References m_buffer.

◆ empty()

bool TextStream::empty ( ) const
inline

Returns true iff the buffer is empty.

Definition at line 253 of file textstream.h.

254 {
255 return m_buffer.empty();
256 }

References m_buffer.

Referenced by HtmlGenerator::endClassDiagram(), insertMapFile(), DotFilePatcher::run(), and writeDotImageMapFromFile().

◆ file()

FILE * TextStream::file ( ) const
inline

Definition at line 89 of file textstream.h.

90 {
91 return m_f;
92 }
FILE * m_f
Definition textstream.h:284

References m_f.

◆ flush()

void TextStream::flush ( )
inline

Flushes the buffer.

If a std::ostream is attached, the buffer's contents will be written to the stream.

Definition at line 209 of file textstream.h.

210 {
211 if (m_s)
212 {
213 m_s->write(m_buffer.c_str(),m_buffer.length());
214 }
215 else if (m_f)
216 {
217 fwrite(m_buffer.c_str(),1,m_buffer.length(),m_f);
218 }
219 m_buffer.clear();
220 }

References m_buffer, m_f, and m_s.

Referenced by Qhp::addContentsItem(), FormulaManager::createLatexFile(), RTFGenerator::preProcessFileInplace(), DotFilePatcher::run(), setFile(), setStream(), str(), str(), FlowChart::writeFlowChart(), and ~TextStream().

◆ operator<<() [1/13]

TextStream & TextStream::operator<< ( char c)
inline

Adds a character to the stream.

Definition at line 95 of file textstream.h.

96 {
97 m_buffer+=c;
98 return static_cast<TextStream&>(*this);
99 }
TextStream(size_t capacity=INITIAL_CAPACITY)
Creates an empty stream object.
Definition textstream.h:41

References m_buffer, and TextStream().

◆ operator<<() [2/13]

TextStream & TextStream::operator<< ( const char * s)
inline

Adds a C-style string to the stream.

Definition at line 123 of file textstream.h.

124 {
125 if (s) m_buffer+=s;
126 return static_cast<TextStream&>(*this);
127 }

References m_buffer, and TextStream().

◆ operator<<() [3/13]

TextStream & TextStream::operator<< ( const QCString & s)
inline

Adds a QCString to the stream.

Definition at line 130 of file textstream.h.

131 {
132 m_buffer+=s.str();
133 return static_cast<TextStream&>(*this);
134 }
const std::string & str() const
Definition qcstring.h:526

References m_buffer, QCString::str(), and TextStream().

◆ operator<<() [4/13]

TextStream & TextStream::operator<< ( const std::string & s)
inline

Adds a std::string to the stream.

Definition at line 137 of file textstream.h.

138 {
139 m_buffer+=s;
140 return static_cast<TextStream&>(*this);
141 }

References m_buffer, and TextStream().

◆ operator<<() [5/13]

TextStream & TextStream::operator<< ( double d)
inline

Adds a double to the stream.

Definition at line 191 of file textstream.h.

192 {
193 output_double(d);
194 return static_cast<TextStream&>(*this);
195 }
void output_double(double d)
Definition textstream.h:276

References output_double(), and TextStream().

◆ operator<<() [6/13]

TextStream & TextStream::operator<< ( float f)
inline

Adds a float to the stream.

Definition at line 184 of file textstream.h.

185 {
186 output_double(static_cast<double>(f));
187 return static_cast<TextStream&>(*this);
188 }

References output_double(), and TextStream().

◆ operator<<() [7/13]

TextStream & TextStream::operator<< ( signed int i)
inline

Adds a signed integer to the stream.

Definition at line 158 of file textstream.h.

159 {
160 output_int32(i,i<0);
161 return static_cast<TextStream&>(*this);
162 }
void output_int32(uint32_t n, bool neg)
Writes a string representation of an integer to the buffer.
Definition textstream.h:263

References output_int32(), and TextStream().

◆ operator<<() [8/13]

TextStream & TextStream::operator<< ( signed short i)
inline

Adds a signed short integer to the stream.

Definition at line 144 of file textstream.h.

145 {
146 output_int32(i,i<0);
147 return static_cast<TextStream&>(*this);
148 }

References output_int32(), and TextStream().

◆ operator<<() [9/13]

template<typename T , typename std::enable_if< std::is_same< T, size_t >::value, T >::type * = nullptr>
TextStream & TextStream::operator<< ( T i)
inline

Adds a size_t integer to the stream.

We use SFINAE to avoid a compiler error in case size_t already matches the 'unsigned int' overload.

Definition at line 177 of file textstream.h.

178 {
179 output_int32(static_cast<uint32_t>(i),false);
180 return static_cast<TextStream&>(*this);
181 }

References output_int32(), and TextStream().

◆ operator<<() [10/13]

TextStream & TextStream::operator<< ( unsigned char * s)
inline

Adds an unsigned character string to the stream.

Definition at line 108 of file textstream.h.

109 {
110 if (s)
111 {
112 unsigned char *p = s;
113 while(*p)
114 {
115 m_buffer+=*p;
116 p++;
117 }
118 }
119 return static_cast<TextStream&>(*this);
120 }

References m_buffer, and TextStream().

◆ operator<<() [11/13]

TextStream & TextStream::operator<< ( unsigned char c)
inline

Adds an unsigned character to the stream.

Definition at line 101 of file textstream.h.

102 {
103 m_buffer+=c;
104 return static_cast<TextStream&>(*this);
105 }

References m_buffer, and TextStream().

◆ operator<<() [12/13]

TextStream & TextStream::operator<< ( unsigned int i)
inline

Adds a unsigned integer to the stream.

Definition at line 165 of file textstream.h.

166 {
167 output_int32(i,false);
168 return static_cast<TextStream&>(*this);
169 }

References output_int32(), and TextStream().

◆ operator<<() [13/13]

TextStream & TextStream::operator<< ( unsigned short i)
inline

Adds a unsigned short integer to the stream.

Definition at line 151 of file textstream.h.

152 {
153 output_int32(i,false);
154 return static_cast<TextStream&>(*this);
155 }

References output_int32(), and TextStream().

◆ output_double()

void TextStream::output_double ( double d)
inlineprivate

Definition at line 276 of file textstream.h.

277 {
278 char buf[64];
279 snprintf(buf,64,"%f",d);
280 m_buffer+=buf;
281 }

References m_buffer.

Referenced by operator<<(), and operator<<().

◆ output_int32()

void TextStream::output_int32 ( uint32_t n,
bool neg )
inlineprivate

Writes a string representation of an integer to the buffer.

Parameters
nthe absolute value of the integer
negindicates if the integer is negative

Definition at line 263 of file textstream.h.

264 {
265 char buf[20];
266 char *p = &buf[19];
267 *p = '\0';
268 if ( neg )
269 {
270 n = static_cast<uint32_t>(-static_cast<int32_t>(n));
271 }
272 do { *--p = (static_cast<char>(n%10)) + '0'; n /= 10; } while ( n );
273 if ( neg ) *--p = '-';
274 m_buffer+=p;
275 }

References m_buffer.

Referenced by operator<<(), operator<<(), operator<<(), operator<<(), and operator<<().

◆ setFile()

void TextStream::setFile ( FILE * f)
inline

Definition at line 74 of file textstream.h.

75 {
76 flush();
77 m_s = nullptr;
78 m_f = f;
79 }

References flush(), m_f, and m_s.

◆ setStream()

void TextStream::setStream ( std::ostream * s)
inline

Sets or changes the std::ostream to write to.

Note
Any data already buffered will be flushed.

Definition at line 67 of file textstream.h.

68 {
69 flush();
70 m_s = s;
71 m_f = nullptr;
72 }

References flush(), m_f, m_s, and setStream().

Referenced by Qhp::addContentsItem(), DotFilePatcher::run(), and setStream().

◆ str() [1/3]

◆ str() [2/3]

void TextStream::str ( const char * s)
inline

Sets the buffer's contents to string s Any data already in the buffer will be flushed.

Definition at line 246 of file textstream.h.

247 {
248 flush();
249 if (s) m_buffer=s;
250 }

References flush(), and m_buffer.

◆ str() [3/3]

void TextStream::str ( const std::string & s)
inline

Sets the buffer's contents to string s.

Any data already in the buffer will be flushed.

Definition at line 237 of file textstream.h.

238 {
239 flush();
240 m_buffer=s;
241 }

References flush(), and m_buffer.

◆ stream()

std::ostream * TextStream::stream ( ) const
inline

Returns the attached std::ostream object.

See also
setStream()

Definition at line 84 of file textstream.h.

85 {
86 return m_s;
87 }

References m_s.

◆ write()

void TextStream::write ( const char * buf,
size_t len )
inline

Adds a array of character to the stream.

Parameters
bufthe character buffer
lenthe number of characters in the buffer to write

Definition at line 201 of file textstream.h.

202 {
203 m_buffer.append(buf,len);
204 }

References m_buffer.

Referenced by generateXML(), and writeUTF8Char().

Member Data Documentation

◆ INITIAL_CAPACITY

const int TextStream::INITIAL_CAPACITY = 4096
staticprivate

Definition at line 37 of file textstream.h.

Referenced by TextStream(), TextStream(), and TextStream().

◆ m_buffer

◆ m_f

FILE* TextStream::m_f = nullptr
private

Definition at line 284 of file textstream.h.

Referenced by file(), flush(), setFile(), and setStream().

◆ m_s

std::ostream* TextStream::m_s = nullptr
private

Definition at line 283 of file textstream.h.

Referenced by flush(), setFile(), setStream(), stream(), and TextStream().


The documentation for this class was generated from the following file: