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)
TextStreamoperator<< (char c)
 Adds a character to the stream.
TextStreamoperator<< (unsigned char c)
 Adds an unsigned character to the stream.
TextStreamoperator<< (const 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 DString &s)
 Adds a DString 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 operator+= (char c)
void operator+= (unsigned char c)
void operator+= (const unsigned char *s)
void operator+= (const char *s)
void operator+= (const DString &s)
void operator+= (const std::string &s)
void operator+= (signed short i)
void operator+= (unsigned short i)
void operator+= (signed int i)
void operator+= (unsigned int i)
void operator+= (float f)
void operator+= (double d)
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:285

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:286
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:212

References flush().

Member Function Documentation

◆ clear()

void TextStream::clear ( )
inline

Clears any buffered data.

Definition at line 226 of file textstream.h.

227 {
228 m_buffer.clear();
229 }

References m_buffer.

◆ empty()

bool TextStream::empty ( ) const
inline

Returns true iff the buffer is empty.

Definition at line 256 of file textstream.h.

257 {
258 return m_buffer.empty();
259 }

References m_buffer.

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

◆ 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 212 of file textstream.h.

213 {
214 if (m_s)
215 {
216 m_s->write(m_buffer.c_str(),m_buffer.length());
217 }
218 else if (m_f)
219 {
220 fwrite(m_buffer.c_str(),1,m_buffer.length(),m_f);
221 }
222 m_buffer.clear();
223 }
FILE * m_f
Definition textstream.h:287

References m_buffer, m_f, and m_s.

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

◆ operator+=() [1/12]

void TextStream::operator+= ( char c)
inline

Definition at line 187 of file textstream.h.

187{ *this << c; }

◆ operator+=() [2/12]

void TextStream::operator+= ( const char * s)
inline

Definition at line 190 of file textstream.h.

190{ *this << s; }

◆ operator+=() [3/12]

void TextStream::operator+= ( const DString & s)
inline

Definition at line 191 of file textstream.h.

191{ *this << s; }

◆ operator+=() [4/12]

void TextStream::operator+= ( const std::string & s)
inline

Definition at line 192 of file textstream.h.

192{ *this << s; }

◆ operator+=() [5/12]

void TextStream::operator+= ( const unsigned char * s)
inline

Definition at line 189 of file textstream.h.

189{ *this << s; }

◆ operator+=() [6/12]

void TextStream::operator+= ( double d)
inline

Definition at line 198 of file textstream.h.

198{ *this << d; }

◆ operator+=() [7/12]

void TextStream::operator+= ( float f)
inline

Definition at line 197 of file textstream.h.

197{ *this << f; }

◆ operator+=() [8/12]

void TextStream::operator+= ( signed int i)
inline

Definition at line 195 of file textstream.h.

195{ *this << i; }

◆ operator+=() [9/12]

void TextStream::operator+= ( signed short i)
inline

Definition at line 193 of file textstream.h.

193{ *this << i; }

◆ operator+=() [10/12]

void TextStream::operator+= ( unsigned char c)
inline

Definition at line 188 of file textstream.h.

188{ *this << c; }

◆ operator+=() [11/12]

void TextStream::operator+= ( unsigned int i)
inline

Definition at line 196 of file textstream.h.

196{ *this << i; }

◆ operator+=() [12/12]

void TextStream::operator+= ( unsigned short i)
inline

Definition at line 194 of file textstream.h.

194{ *this << i; }

◆ operator<<() [1/13]

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

Adds a character to the stream.

Definition at line 82 of file textstream.h.

83 {
84 m_buffer+=c;
85 return *this;
86 }

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 112 of file textstream.h.

113 {
114 if (s) m_buffer+=s;
115 return *this;
116 }

References m_buffer, and TextStream().

◆ operator<<() [3/13]

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

Adds a DString to the stream.

Definition at line 119 of file textstream.h.

120 {
121 m_buffer+=s.str();
122 return *this;
123 }
const std::string & str() const
Definition dstring.h:634

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

◆ operator<<() [4/13]

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

Adds a std::string to the stream.

Definition at line 126 of file textstream.h.

127 {
128 m_buffer+=s;
129 return *this;
130 }

References m_buffer, and TextStream().

◆ operator<<() [5/13]

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

Adds an unsigned character string to the stream.

Definition at line 97 of file textstream.h.

98 {
99 if (s)
100 {
101 const unsigned char *p = s;
102 while (*p)
103 {
104 m_buffer+=*p;
105 p++;
106 }
107 }
108 return *this;
109 }

References m_buffer, and TextStream().

◆ operator<<() [6/13]

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

Adds a double to the stream.

Definition at line 180 of file textstream.h.

181 {
182 output_double(d);
183 return *this;
184 }
void output_double(double d)
Definition textstream.h:279

References output_double(), and TextStream().

◆ operator<<() [7/13]

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

Adds a float to the stream.

Definition at line 173 of file textstream.h.

174 {
175 output_double(static_cast<double>(f));
176 return *this;
177 }

References output_double(), and TextStream().

◆ operator<<() [8/13]

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

Adds a signed integer to the stream.

Definition at line 147 of file textstream.h.

148 {
149 output_int32(i,i<0);
150 return *this;
151 }
void output_int32(uint32_t n, bool neg)
Writes a string representation of an integer to the buffer.
Definition textstream.h:266

References output_int32(), and TextStream().

◆ operator<<() [9/13]

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

Adds a signed short integer to the stream.

Definition at line 133 of file textstream.h.

134 {
135 output_int32(i,i<0);
136 return *this;
137 }

References output_int32(), and TextStream().

◆ operator<<() [10/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 166 of file textstream.h.

167 {
168 output_int32(static_cast<uint32_t>(i),false);
169 return *this;
170 }

References output_int32(), and TextStream().

◆ operator<<() [11/13]

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

Adds an unsigned character to the stream.

Definition at line 90 of file textstream.h.

91 {
92 m_buffer+=c;
93 return *this;
94 }

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 154 of file textstream.h.

155 {
156 output_int32(i,false);
157 return *this;
158 }

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 140 of file textstream.h.

141 {
142 output_int32(i,false);
143 return *this;
144 }

References output_int32(), and TextStream().

◆ output_double()

void TextStream::output_double ( double d)
inlineprivate

Definition at line 279 of file textstream.h.

280 {
281 char buf[64];
282 snprintf(buf,64,"%f",d);
283 m_buffer+=buf;
284 }

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 266 of file textstream.h.

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

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.

Referenced by OutputGenerator::startPlainFile().

◆ 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(), OutputGenerator::endPlainFile(), 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 249 of file textstream.h.

250 {
251 flush();
252 if (s) m_buffer=s;
253 }

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 240 of file textstream.h.

241 {
242 flush();
243 m_buffer=s;
244 }

References flush(), and m_buffer.

◆ 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 204 of file textstream.h.

205 {
206 m_buffer.append(buf,len);
207 }

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 287 of file textstream.h.

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

◆ m_s

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

Definition at line 286 of file textstream.h.

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


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