Skip to main content

The TextStream Class Reference

Text streaming class that buffers data. More...

Declaration

class TextStream { ... }

Included Headers

#include <src/textstream.h>

Public Constructors Index

TextStream (size_t capacity=INITIAL_CAPACITY)

Creates an empty stream object. More...

TextStream (std::ostream *s)

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

TextStream (const std::string &s)

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

Public Destructor Index

~TextStream ()

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

Public Operators Index

TextStream &operator<< (char c)

Adds a character to the stream. More...

TextStream &operator<< (unsigned char c)

Adds an unsigned character to the stream. More...

TextStream &operator<< (unsigned char *s)

Adds an unsigned character string to the stream. More...

TextStream &operator<< (const char *s)

Adds a C-style string to the stream. More...

TextStream &operator<< (const QCString &s)

Adds a QCString to the stream. More...

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

Adds a std::string to the stream. More...

TextStream &operator<< (signed short i)

Adds a signed short integer to the stream. More...

TextStream &operator<< (unsigned short i)

Adds a unsigned short integer to the stream. More...

TextStream &operator<< (signed int i)

Adds a signed integer to the stream. More...

TextStream &operator<< (unsigned int i)

Adds a unsigned integer to the stream. More...

template < ... >
TextStream &operator<< (T i)

Adds a size_t integer to the stream. More...

TextStream &operator<< (float f)

Adds a float to the stream. More...

TextStream &operator<< (double d)

Adds a double to the stream. More...

Public Member Functions Index

voidsetStream (std::ostream *s)

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

voidsetFile (FILE *f)
std::ostream *stream () const

Returns the attached std::ostream object. More...

FILE *file () const
voidwrite (const char *buf, size_t len)

Adds a array of character to the stream. More...

voidflush ()

Flushes the buffer. More...

voidclear ()

Clears any buffered data. More...

std::stringstr () const

Return the contents of the buffer as a std::string object. More...

voidstr (const std::string &s)

Sets the buffer's contents to string s. More...

voidstr (const char *s)

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

boolempty () const

Returns true iff the buffer is empty. More...

Private Member Functions Index

voidoutput_int32 (uint32_t n, bool neg)

Writes a string representation of an integer to the buffer. More...

voidoutput_double (double d)

Private Member Attributes Index

std::stringm_buffer
std::ostream *m_s = nullptr
FILE *m_f = nullptr

Private Static Attributes Index

static const intINITIAL_CAPACITY = 4096

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.

Public Constructors

TextStream()

TextStream::TextStream (size_t capacity=INITIAL_CAPACITY)
inline explicit

Creates an empty stream object.

Definition at line 41 of file textstream.h.

41 explicit TextStream(size_t capacity = INITIAL_CAPACITY)
42 {
43 m_buffer.reserve(capacity);
44 }

References INITIAL_CAPACITY and m_buffer.

Referenced by operator<<, operator<<, operator<<, operator<<, operator<<, operator<<, operator<<, operator<<, operator<<, operator<<, operator<<, operator<< and operator<<.

TextStream()

TextStream::TextStream (std::ostream * s)
inline explicit

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

info

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

Definition at line 48 of file textstream.h.

48 explicit TextStream(std::ostream *s) : m_s(s)
49 {
51 }

References INITIAL_CAPACITY, m_buffer and m_s.

TextStream()

TextStream::TextStream (const std::string & s)
inline explicit

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

Definition at line 54 of file textstream.h.

54 explicit TextStream(const std::string &s) : m_buffer(s)
55 {
56 m_buffer.reserve(s.length()+INITIAL_CAPACITY);
57 }

References INITIAL_CAPACITY and m_buffer.

Public Destructor

~TextStream()

TextStream::~TextStream ()
inline

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

Definition at line 60 of file textstream.h.

Reference flush.

Public Operators

operator<<()

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 }

References m_buffer and TextStream.

operator<<()

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

Adds an unsigned character to the stream.

Definition at line 101 of file textstream.h.

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

References m_buffer and TextStream.

operator<<()

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

Adds an unsigned character string to the stream.

Definition at line 108 of file textstream.h.

108 TextStream &operator<<( unsigned char *s)
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<<()

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

Adds a C-style string to the stream.

Definition at line 123 of file textstream.h.

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

References m_buffer and TextStream.

operator<<()

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 }

References m_buffer, QCString::str and TextStream.

operator<<()

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

Adds a std::string to the stream.

Definition at line 137 of file textstream.h.

137 TextStream &operator<<( const std::string &s )
138 {
139 m_buffer+=s;
140 return static_cast<TextStream&>(*this);
141 }

References m_buffer and TextStream.

operator<<()

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

Adds a signed short integer to the stream.

Definition at line 144 of file textstream.h.

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

References output_int32 and TextStream.

operator<<()

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

Adds a unsigned short integer to the stream.

Definition at line 151 of file textstream.h.

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

References output_int32 and TextStream.

operator<<()

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

Adds a signed integer to the stream.

Definition at line 158 of file textstream.h.

158 TextStream &operator<<( signed int i)
159 {
160 output_int32(i,i<0);
161 return static_cast<TextStream&>(*this);
162 }

References output_int32 and TextStream.

operator<<()

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

Adds a unsigned integer to the stream.

Definition at line 165 of file textstream.h.

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

References output_int32 and TextStream.

operator<<()

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<<()

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<<()

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

Adds a double to the stream.

Definition at line 191 of file textstream.h.

192 {
194 return static_cast<TextStream&>(*this);
195 }

References output_double and TextStream.

Public Member Functions

clear()

void TextStream::clear ()
inline

Clears any buffered data.

Definition at line 223 of file textstream.h.

223 void clear()
224 {
225 m_buffer.clear();
226 }

Reference m_buffer.

empty()

bool TextStream::empty ()
inline

Returns true iff the buffer is empty.

Definition at line 253 of file textstream.h.

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

Reference m_buffer.

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

file()

FILE * TextStream::file ()
inline

Definition at line 89 of file textstream.h.

89 FILE *file() const
90 {
91 return m_f;
92 }

Reference 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.

209 void flush()
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.

setFile()

void TextStream::setFile (FILE * f)
inline

Definition at line 74 of file textstream.h.

74 void setFile(FILE *f)
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.

info

Any data already buffered will be flushed.

Definition at line 67 of file textstream.h.

67 void setStream(std::ostream *s)
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()

str()

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.

237 void str(const std::string &s)
238 {
239 flush();
240 m_buffer=s;
241 }

References flush and m_buffer.

str()

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.

246 void str(const char *s)
247 {
248 flush();
249 if (s) m_buffer=s;
250 }

References flush and m_buffer.

stream()

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

Returns the attached std::ostream object.

See Also

setStream()

Definition at line 84 of file textstream.h.

84 std::ostream *stream() const
85 {
86 return m_s;
87 }

Reference m_s.

write()

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

Adds a array of character to the stream.

Parameters
buf

the character buffer

len

the number of characters in the buffer to write

Definition at line 201 of file textstream.h.

201 void write(const char *buf,size_t len)
202 {
203 m_buffer.append(buf,len);
204 }

Reference m_buffer.

Referenced by generateXML and writeUTF8Char.

Private Member Functions

output_double()

void TextStream::output_double (double d)
inline

Definition at line 276 of file textstream.h.

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

Reference m_buffer.

Referenced by operator<< and operator<<.

output_int32()

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

Writes a string representation of an integer to the buffer.

Parameters
n

the absolute value of the integer

neg

indicates if the integer is negative

Definition at line 263 of file textstream.h.

263 void output_int32( uint32_t n, bool neg )
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 }

Reference m_buffer.

Referenced by operator<<, operator<<, operator<<, operator<< and operator<<.

Private Member Attributes

m_buffer

std::string TextStream::m_buffer

m_f

FILE* TextStream::m_f = nullptr

Definition at line 284 of file textstream.h.

284 FILE *m_f = nullptr;

Referenced by file, flush, setFile and setStream.

m_s

std::ostream* TextStream::m_s = nullptr

Definition at line 283 of file textstream.h.

283 std::ostream *m_s = nullptr;

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

Private Static Attributes

INITIAL_CAPACITY

const int TextStream::INITIAL_CAPACITY = 4096
static

Definition at line 37 of file textstream.h.

37 static const int INITIAL_CAPACITY = 4096;

Referenced by TextStream, TextStream and TextStream.


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


Generated via doxygen2docusaurus by Doxygen 1.14.0.