2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined(INCLUDED_STREAM_TEXTSTREAM_H)
23 #define INCLUDED_STREAM_TEXTSTREAM_H
26 /// \brief Text-output-formatting.
28 #include "itextstream.h"
36 #include "generic/arrayrange.h"
38 namespace TextOutputDetail
40 inline char* write_unsigned_nonzero_decimal_backward(char* ptr, unsigned int decimal)
42 for (; decimal != 0; decimal /= 10)
44 *--ptr = char('0' + int(decimal % 10));
49 inline char* write_signed_nonzero_decimal_backward(char* ptr, int decimal, bool show_positive)
51 const bool negative = decimal < 0 ;
52 ptr = write_unsigned_nonzero_decimal_backward(ptr, negative ? -decimal : decimal);
57 else if(show_positive)
64 inline char* write_unsigned_nonzero_decimal_backward(char* ptr, unsigned int decimal, bool show_positive)
66 ptr = write_unsigned_nonzero_decimal_backward(ptr, decimal);
74 inline char* write_signed_decimal_backward(char* ptr, int decimal, bool show_positive)
82 ptr = write_signed_nonzero_decimal_backward(ptr, decimal, show_positive);
87 inline char* write_unsigned_decimal_backward(char* ptr, unsigned int decimal, bool show_positive)
95 ptr = write_unsigned_nonzero_decimal_backward(ptr, decimal, show_positive);
103 #define snprintf _snprintf
106 /// \brief Writes a single character \p c to \p ostream.
107 template<typename TextOutputStreamType>
108 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, char c)
110 ostream.write(&c, 1);
114 /// \brief Writes a double-precision floating point value \p d to \p ostream.
115 /// The value will be formatted either as decimal with trailing zeros removed, or with scientific 'e' notation, whichever is shorter.
116 template<typename TextOutputStreamType>
117 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const double d)
119 const std::size_t bufferSize = 16;
120 char buf[bufferSize];
121 ostream.write(buf, snprintf(buf, bufferSize, "%g", d));
125 /// \brief Writes a single-precision floating point value \p f to \p ostream.
126 /// The value will be formatted either as decimal with trailing zeros removed, or with scientific 'e' notation, whichever is shorter.
127 template<typename TextOutputStreamType>
128 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const float f)
130 return ostream_write(ostream, static_cast<double>(f));
133 /// \brief Writes a signed integer \p i to \p ostream in decimal form.
134 /// A '-' sign will be added if the value is negative.
135 template<typename TextOutputStreamType>
136 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const int i)
138 const std::size_t bufferSize = 16;
140 char buf[bufferSize];
141 char* begin = TextOutputDetail::write_signed_decimal_backward(buf + bufferSize, i, false);
142 ostream.write(begin, (buf + bufferSize) - begin);
144 char buf[bufferSize];
145 ostream.write(buf, snprintf(buf, bufferSize, "%i", i));
150 typedef unsigned int Unsigned;
152 /// \brief Writes an unsigned integer \p i to \p ostream in decimal form.
153 template<typename TextOutputStreamType>
154 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Unsigned i)
156 const std::size_t bufferSize = 16;
158 char buf[bufferSize];
159 char* begin = TextOutputDetail::write_unsigned_decimal_backward(buf + bufferSize, i, false);
160 ostream.write(begin, (buf + bufferSize) - begin);
162 char buf[bufferSize];
163 ostream.write(buf, snprintf(buf, bufferSize, "%u", i));
168 /// \brief Writes a null-terminated \p string to \p ostream.
169 template<typename TextOutputStreamType>
170 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const char* string)
172 ostream.write(string, strlen(string));
180 HexChar(char value) : m_value(value)
185 /// \brief Writes a single character \p c to \p ostream in hexadecimal form.
186 template<typename TextOutputStreamType>
187 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const HexChar& c)
189 const std::size_t bufferSize = 16;
190 char buf[bufferSize];
191 ostream.write(buf, snprintf(buf, bufferSize, "%X", c.m_value & 0xFF));
201 LeftJustified(const T& t, std::size_t size) : m_t(t), m_size(size)
207 LeftJustified<T> makeLeftJustified(const T& t, std::size_t size)
209 return LeftJustified<T>(t, size);
212 template<typename TextOutputStreamType>
213 class CountingOutputStream
215 TextOutputStreamType& m_ostream;
218 CountingOutputStream(TextOutputStreamType& ostream) : m_ostream(ostream)
221 std::size_t write(const char* buffer, std::size_t length)
224 return m_ostream.write(buffer, length);
228 template<typename TextOutputStreamType, typename T>
229 inline CountingOutputStream<TextOutputStreamType>& operator<<(CountingOutputStream<TextOutputStreamType>& ostream, const T& t)
231 return ostream_write(ostream, t);
235 /// \brief Writes any type to \p ostream padded with spaces afterwards.
236 template<typename TextOutputStreamType, typename T>
237 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const LeftJustified<T>& justified)
239 CountingOutputStream<TextOutputStreamType> count(ostream);
240 count << justified.m_t;
241 while(justified.m_size > count.m_count)
254 FloatFormat(double f, int width, int precision)
255 : m_f(f), m_width(width), m_precision(precision)
260 /// \brief Writes a floating point value to \p ostream with a specific width and precision.
261 template<typename TextOutputStreamType>
262 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const FloatFormat& formatted)
264 const std::size_t bufferSize = 32;
265 char buf[bufferSize];
266 ostream.write(buf, snprintf(buf, bufferSize, "%*.*lf", formatted.m_width, formatted.m_precision, formatted.m_f));
270 // never displays exponent, prints up to 10 decimal places
275 Decimal(double f) : m_f(f)
280 /// \brief Writes a floating point value to \p ostream in decimal form with trailing zeros removed.
281 template<typename TextOutputStreamType>
282 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Decimal& decimal)
284 const int bufferSize = 22;
285 char buf[bufferSize];
286 std::size_t length = snprintf(buf, bufferSize, "%10.10lf", decimal.m_f);
287 const char* first = buf;
288 for(; *first == ' '; ++first)
291 const char* last = buf + length - 1;
292 for(; *last == '0'; --last)
299 ostream.write(first, last - first + 1);
304 typedef ArrayRange<const char> StringRange;
306 /// \brief Writes a \p range of characters to \p ostream.
307 template<typename TextOutputStreamType>
308 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const StringRange& range)
310 ostream.write(range.begin, range.end - range.begin);
314 template<typename Type>
319 Quoted(const Type& type)
325 template<typename Type>
326 inline Quoted<Type> makeQuoted(const Type& type)
328 return Quoted<Type>(type);
331 /// \brief Writes any type to \p ostream with a quotation mark character before and after it.
332 template<typename TextOutputStreamType, typename Type>
333 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Quoted<Type>& quoted)
335 return ostream << '"' << quoted.m_type << '"';
342 const char* m_string;
343 LowerCase(const char* string) : m_string(string)
348 /// \brief Writes a string to \p ostream converted to lower-case.
349 template<typename TextOutputStreamType>
350 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const LowerCase& lower)
352 for(const char* p = lower.m_string; *p != '\0'; ++p)
354 ostream << static_cast<char>(std::tolower(*p));
360 /// \brief A wrapper for a TextInputStream optimised for reading a single character at a time.
361 template<typename TextInputStreamType, int SIZE = 1024>
362 class SingleCharacterInputStream
364 TextInputStreamType& m_inputStream;
371 m_end = m_buffer + m_inputStream.read(m_buffer, SIZE);
373 return m_cur != m_end;
377 SingleCharacterInputStream(TextInputStreamType& inputStream) : m_inputStream(inputStream), m_cur(m_buffer), m_end(m_buffer)
380 bool readChar(char& c)
382 if(m_cur == m_end && !fillBuffer())
392 /// \brief A wrapper for a TextOutputStream, optimised for writing a single character at a time.
393 class SingleCharacterOutputStream : public TextOutputStream
395 enum unnamed0 { m_bufsize = 1024 };
396 TextOutputStream& m_ostream;
397 char m_buffer[m_bufsize];
401 const char* end() const
411 m_ostream.write(m_buffer, m_pos - m_buffer);
415 SingleCharacterOutputStream(TextOutputStream& ostream) : m_ostream(ostream), m_pos(m_buffer), m_end(m_buffer+m_bufsize)
418 ~SingleCharacterOutputStream()
422 void write(const char c)
430 std::size_t write(const char* buffer, std::size_t length)
432 const char*const end = buffer + length;
433 for(const char* p = buffer; p != end; ++p)
441 /// \brief A wrapper for a TextOutputStream, optimised for writing a few characters at a time.
442 template<typename TextOutputStreamType, int SIZE = 1024>
443 class BufferedTextOutputStream : public TextOutputStream
445 TextOutputStreamType outputStream;
450 BufferedTextOutputStream(TextOutputStreamType& outputStream) : outputStream(outputStream), m_cur(m_buffer)
453 ~BufferedTextOutputStream()
455 outputStream.write(m_buffer, m_cur - m_buffer);
457 std::size_t write(const char* buffer, std::size_t length)
459 std::size_t remaining = length;
462 std::size_t n = std::min(remaining, std::size_t((m_buffer + SIZE) - m_cur));
463 m_cur = std::copy(buffer, buffer + n, m_cur);
469 outputStream.write(m_buffer, SIZE);