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"
37 #include "generic/arrayrange.h"
39 namespace TextOutputDetail
41 inline char* write_unsigned_nonzero_decimal_backward(char* ptr, unsigned int decimal)
43 for (; decimal != 0; decimal /= 10)
45 *--ptr = char('0' + int(decimal % 10));
50 inline char* write_signed_nonzero_decimal_backward(char* ptr, int decimal, bool show_positive)
52 const bool negative = decimal < 0 ;
53 ptr = write_unsigned_nonzero_decimal_backward(ptr, negative ? -decimal : decimal);
58 else if(show_positive)
65 inline char* write_unsigned_nonzero_decimal_backward(char* ptr, unsigned int decimal, bool show_positive)
67 ptr = write_unsigned_nonzero_decimal_backward(ptr, decimal);
75 inline char* write_signed_decimal_backward(char* ptr, int decimal, bool show_positive)
83 ptr = write_signed_nonzero_decimal_backward(ptr, decimal, show_positive);
88 inline char* write_unsigned_decimal_backward(char* ptr, unsigned int decimal, bool show_positive)
96 ptr = write_unsigned_nonzero_decimal_backward(ptr, decimal, show_positive);
104 #define snprintf _snprintf
107 /// \brief Writes a single character \p c to \p ostream.
108 template<typename TextOutputStreamType>
109 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, char c)
111 ostream.write(&c, 1);
115 /// \brief Writes a double-precision floating point value \p d to \p ostream.
116 /// The value will be formatted either as decimal with trailing zeros removed, or with scientific 'e' notation, whichever is shorter.
117 template<typename TextOutputStreamType>
118 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const double d)
120 const std::size_t bufferSize = 16;
121 char buf[bufferSize];
122 ostream.write(buf, snprintf(buf, bufferSize, "%g", d));
126 /// \brief Writes a single-precision floating point value \p f to \p ostream.
127 /// The value will be formatted either as decimal with trailing zeros removed, or with scientific 'e' notation, whichever is shorter.
128 template<typename TextOutputStreamType>
129 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const float f)
131 return ostream_write(ostream, static_cast<double>(f));
134 /// \brief Writes a signed integer \p i to \p ostream in decimal form.
135 /// A '-' sign will be added if the value is negative.
136 template<typename TextOutputStreamType>
137 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const int i)
139 const std::size_t bufferSize = 16;
141 char buf[bufferSize];
142 char* begin = TextOutputDetail::write_signed_decimal_backward(buf + bufferSize, i, false);
143 ostream.write(begin, (buf + bufferSize) - begin);
145 char buf[bufferSize];
146 ostream.write(buf, snprintf(buf, bufferSize, "%i", i));
151 typedef unsigned int Unsigned;
153 /// \brief Writes an unsigned integer \p i to \p ostream in decimal form.
154 template<typename TextOutputStreamType>
155 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Unsigned i)
157 const std::size_t bufferSize = 16;
159 char buf[bufferSize];
160 char* begin = TextOutputDetail::write_unsigned_decimal_backward(buf + bufferSize, i, false);
161 ostream.write(begin, (buf + bufferSize) - begin);
163 char buf[bufferSize];
164 ostream.write(buf, snprintf(buf, bufferSize, "%u", i));
169 /// \brief Writes a null-terminated \p string to \p ostream.
170 template<typename TextOutputStreamType>
171 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const char* string)
173 ostream.write(string, strlen(string));
181 HexChar(char value) : m_value(value)
186 /// \brief Writes a single character \p c to \p ostream in hexadecimal form.
187 template<typename TextOutputStreamType>
188 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const HexChar& c)
190 const std::size_t bufferSize = 16;
191 char buf[bufferSize];
192 ostream.write(buf, snprintf(buf, bufferSize, "%X", c.m_value & 0xFF));
202 LeftJustified(const T& t, std::size_t size) : m_t(t), m_size(size)
208 LeftJustified<T> makeLeftJustified(const T& t, std::size_t size)
210 return LeftJustified<T>(t, size);
213 template<typename TextOutputStreamType>
214 class CountingOutputStream
216 TextOutputStreamType& m_ostream;
219 CountingOutputStream(TextOutputStreamType& ostream) : m_ostream(ostream)
222 std::size_t write(const char* buffer, std::size_t length)
225 return m_ostream.write(buffer, length);
229 template<typename TextOutputStreamType, typename T>
230 inline CountingOutputStream<TextOutputStreamType>& operator<<(CountingOutputStream<TextOutputStreamType>& ostream, const T& t)
232 return ostream_write(ostream, t);
236 /// \brief Writes any type to \p ostream padded with spaces afterwards.
237 template<typename TextOutputStreamType, typename T>
238 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const LeftJustified<T>& justified)
240 CountingOutputStream<TextOutputStreamType> count(ostream);
241 count << justified.m_t;
242 while(justified.m_size > count.m_count)
255 FloatFormat(double f, int width, int precision)
256 : m_f(f), m_width(width), m_precision(precision)
261 /// \brief Writes a floating point value to \p ostream with a specific width and precision.
262 template<typename TextOutputStreamType>
263 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const FloatFormat& formatted)
265 const std::size_t bufferSize = 32;
266 char buf[bufferSize];
267 ostream.write(buf, snprintf(buf, bufferSize, "%*.*lf", formatted.m_width, formatted.m_precision, formatted.m_f));
271 // never displays exponent, prints up to 10 decimal places
276 Decimal(double f) : m_f(f)
281 /// \brief Writes a floating point value to \p ostream in decimal form with trailing zeros removed.
282 template<typename TextOutputStreamType>
283 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Decimal& decimal)
285 const int bufferSize = 22;
286 char buf[bufferSize];
287 std::size_t length = snprintf(buf, bufferSize, "%10.10lf", decimal.m_f);
288 const char* first = buf;
289 for(; *first == ' '; ++first)
292 const char* last = buf + length - 1;
293 for(; *last == '0'; --last)
300 ostream.write(first, last - first + 1);
305 /// \brief Writes a \p range of characters to \p ostream.
306 template<typename TextOutputStreamType>
307 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const StringRange& range)
309 ostream.write(range.first, range.last - range.first);
313 template<typename Type>
318 Quoted(const Type& type)
324 template<typename Type>
325 inline Quoted<Type> makeQuoted(const Type& type)
327 return Quoted<Type>(type);
330 /// \brief Writes any type to \p ostream with a quotation mark character before and after it.
331 template<typename TextOutputStreamType, typename Type>
332 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Quoted<Type>& quoted)
334 return ostream << '"' << quoted.m_type << '"';
341 const char* m_string;
342 LowerCase(const char* string) : m_string(string)
347 /// \brief Writes a string to \p ostream converted to lower-case.
348 template<typename TextOutputStreamType>
349 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const LowerCase& lower)
351 for(const char* p = lower.m_string; *p != '\0'; ++p)
353 ostream << static_cast<char>(std::tolower(*p));
359 /// \brief A wrapper for a TextInputStream optimised for reading a single character at a time.
360 template<typename TextInputStreamType, int SIZE = 1024>
361 class SingleCharacterInputStream
363 TextInputStreamType& m_inputStream;
370 m_end = m_buffer + m_inputStream.read(m_buffer, SIZE);
372 return m_cur != m_end;
376 SingleCharacterInputStream(TextInputStreamType& inputStream) : m_inputStream(inputStream), m_cur(m_buffer), m_end(m_buffer)
379 bool readChar(char& c)
381 if(m_cur == m_end && !fillBuffer())
391 /// \brief A wrapper for a TextOutputStream, optimised for writing a single character at a time.
392 class SingleCharacterOutputStream : public TextOutputStream
394 enum unnamed0 { m_bufsize = 1024 };
395 TextOutputStream& m_ostream;
396 char m_buffer[m_bufsize];
400 const char* end() const
410 m_ostream.write(m_buffer, m_pos - m_buffer);
414 SingleCharacterOutputStream(TextOutputStream& ostream) : m_ostream(ostream), m_pos(m_buffer), m_end(m_buffer+m_bufsize)
417 ~SingleCharacterOutputStream()
421 void write(const char c)
429 std::size_t write(const char* buffer, std::size_t length)
431 const char*const end = buffer + length;
432 for(const char* p = buffer; p != end; ++p)
440 /// \brief A wrapper for a TextOutputStream, optimised for writing a few characters at a time.
441 template<typename TextOutputStreamType, int SIZE = 1024>
442 class BufferedTextOutputStream : public TextOutputStream
444 TextOutputStreamType outputStream;
449 BufferedTextOutputStream(TextOutputStreamType& outputStream) : outputStream(outputStream), m_cur(m_buffer)
452 ~BufferedTextOutputStream()
454 outputStream.write(m_buffer, m_cur - m_buffer);
456 std::size_t write(const char* buffer, std::size_t length)
458 std::size_t remaining = length;
461 std::size_t n = std::min(remaining, std::size_t((m_buffer + SIZE) - m_cur));
462 m_cur = std::copy(buffer, buffer + n, m_cur);
468 outputStream.write(m_buffer, SIZE);