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 #if defined (_WIN64) || defined (__LP64__)
51 inline char* write_size_t_nonzero_decimal_backward(char* ptr, size_t decimal)
53 for (; decimal != 0; decimal /= 10)
55 *--ptr = char('0' + (size_t)(decimal % 10));
61 inline char* write_signed_nonzero_decimal_backward(char* ptr, int decimal, bool show_positive)
63 const bool negative = decimal < 0 ;
64 ptr = write_unsigned_nonzero_decimal_backward(ptr, negative ? -decimal : decimal);
69 else if(show_positive)
76 inline char* write_unsigned_nonzero_decimal_backward(char* ptr, unsigned int decimal, bool show_positive)
78 ptr = write_unsigned_nonzero_decimal_backward(ptr, decimal);
86 #if defined (_WIN64) || defined (__LP64__)
87 inline char* write_size_t_nonzero_decimal_backward(char* ptr, size_t decimal, bool show_positive)
89 ptr = write_size_t_nonzero_decimal_backward(ptr, decimal);
98 inline char* write_signed_decimal_backward(char* ptr, int decimal, bool show_positive)
106 ptr = write_signed_nonzero_decimal_backward(ptr, decimal, show_positive);
111 inline char* write_unsigned_decimal_backward(char* ptr, unsigned int decimal, bool show_positive)
119 ptr = write_unsigned_nonzero_decimal_backward(ptr, decimal, show_positive);
124 #if defined (_WIN64) || defined (__LP64__)
125 inline char* write_size_t_decimal_backward(char* ptr, size_t decimal, bool show_positive)
133 ptr = write_size_t_nonzero_decimal_backward(ptr, decimal, show_positive);
142 #define snprintf _snprintf
145 /// \brief Writes a single character \p c to \p ostream.
146 template<typename TextOutputStreamType>
147 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, char c)
149 ostream.write(&c, 1);
153 /// \brief Writes a double-precision floating point value \p d to \p ostream.
154 /// The value will be formatted either as decimal with trailing zeros removed, or with scientific 'e' notation, whichever is shorter.
155 template<typename TextOutputStreamType>
156 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const double d)
158 const std::size_t bufferSize = 16;
159 char buf[bufferSize];
160 ostream.write(buf, snprintf(buf, bufferSize, "%g", d));
164 /// \brief Writes a single-precision floating point value \p f to \p ostream.
165 /// The value will be formatted either as decimal with trailing zeros removed, or with scientific 'e' notation, whichever is shorter.
166 template<typename TextOutputStreamType>
167 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const float f)
169 return ostream_write(ostream, static_cast<double>(f));
172 /// \brief Writes a signed integer \p i to \p ostream in decimal form.
173 /// A '-' sign will be added if the value is negative.
174 template<typename TextOutputStreamType>
175 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const int i)
177 const std::size_t bufferSize = 16;
179 char buf[bufferSize];
180 char* begin = TextOutputDetail::write_signed_decimal_backward(buf + bufferSize, i, false);
181 ostream.write(begin, (buf + bufferSize) - begin);
183 char buf[bufferSize];
184 ostream.write(buf, snprintf(buf, bufferSize, "%i", i));
189 typedef unsigned int Unsigned;
191 /// \brief Writes an unsigned integer \p i to \p ostream in decimal form.
192 template<typename TextOutputStreamType>
193 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Unsigned i)
195 const std::size_t bufferSize = 16;
197 char buf[bufferSize];
198 char* begin = TextOutputDetail::write_unsigned_decimal_backward(buf + bufferSize, i, false);
199 ostream.write(begin, (buf + bufferSize) - begin);
201 char buf[bufferSize];
202 ostream.write(buf, snprintf(buf, bufferSize, "%u", i));
207 #if defined (_WIN64) || defined (__LP64__)
209 /// \brief Writes a size_t \p i to \p ostream in decimal form.
210 template<typename TextOutputStreamType>
211 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const size_t i)
213 // max is 18446744073709551615, buffer of 32 chars should always be enough
214 const std::size_t bufferSize = 32;
216 char buf[bufferSize];
217 char* begin = TextOutputDetail::write_size_t_decimal_backward(buf + bufferSize, i, false);
218 ostream.write(begin, (buf + bufferSize) - begin);
220 char buf[bufferSize];
221 ostream.write(buf, snprintf(buf, bufferSize, "%u", i));
228 /// \brief Writes a null-terminated \p string to \p ostream.
229 template<typename TextOutputStreamType>
230 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const char* string)
232 ostream.write(string, strlen(string));
240 HexChar(char value) : m_value(value)
245 /// \brief Writes a single character \p c to \p ostream in hexadecimal form.
246 template<typename TextOutputStreamType>
247 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const HexChar& c)
249 const std::size_t bufferSize = 16;
250 char buf[bufferSize];
251 ostream.write(buf, snprintf(buf, bufferSize, "%X", c.m_value & 0xFF));
261 LeftJustified(const T& t, std::size_t size) : m_t(t), m_size(size)
267 LeftJustified<T> makeLeftJustified(const T& t, std::size_t size)
269 return LeftJustified<T>(t, size);
272 template<typename TextOutputStreamType>
273 class CountingOutputStream
275 TextOutputStreamType& m_ostream;
278 CountingOutputStream(TextOutputStreamType& ostream) : m_ostream(ostream)
281 std::size_t write(const char* buffer, std::size_t length)
284 return m_ostream.write(buffer, length);
288 template<typename TextOutputStreamType, typename T>
289 inline CountingOutputStream<TextOutputStreamType>& operator<<(CountingOutputStream<TextOutputStreamType>& ostream, const T& t)
291 return ostream_write(ostream, t);
295 /// \brief Writes any type to \p ostream padded with spaces afterwards.
296 template<typename TextOutputStreamType, typename T>
297 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const LeftJustified<T>& justified)
299 CountingOutputStream<TextOutputStreamType> count(ostream);
300 count << justified.m_t;
301 while(justified.m_size > count.m_count)
314 FloatFormat(double f, int width, int precision)
315 : m_f(f), m_width(width), m_precision(precision)
320 /// \brief Writes a floating point value to \p ostream with a specific width and precision.
321 template<typename TextOutputStreamType>
322 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const FloatFormat& formatted)
324 const std::size_t bufferSize = 32;
325 char buf[bufferSize];
326 ostream.write(buf, snprintf(buf, bufferSize, "%*.*lf", formatted.m_width, formatted.m_precision, formatted.m_f));
330 // never displays exponent, prints up to 10 decimal places
335 Decimal(double f) : m_f(f)
340 /// \brief Writes a floating point value to \p ostream in decimal form with trailing zeros removed.
341 template<typename TextOutputStreamType>
342 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Decimal& decimal)
344 const int bufferSize = 22;
345 char buf[bufferSize];
346 std::size_t length = snprintf(buf, bufferSize, "%10.10lf", decimal.m_f);
347 const char* first = buf;
348 for(; *first == ' '; ++first)
351 const char* last = buf + length - 1;
352 for(; *last == '0'; --last)
359 ostream.write(first, last - first + 1);
364 /// \brief Writes a \p range of characters to \p ostream.
365 template<typename TextOutputStreamType>
366 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const StringRange& range)
368 ostream.write(range.first, range.last - range.first);
372 template<typename Type>
377 Quoted(const Type& type)
383 template<typename Type>
384 inline Quoted<Type> makeQuoted(const Type& type)
386 return Quoted<Type>(type);
389 /// \brief Writes any type to \p ostream with a quotation mark character before and after it.
390 template<typename TextOutputStreamType, typename Type>
391 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Quoted<Type>& quoted)
393 return ostream << '"' << quoted.m_type << '"';
400 const char* m_string;
401 LowerCase(const char* string) : m_string(string)
406 /// \brief Writes a string to \p ostream converted to lower-case.
407 template<typename TextOutputStreamType>
408 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const LowerCase& lower)
410 for(const char* p = lower.m_string; *p != '\0'; ++p)
412 ostream << static_cast<char>(std::tolower(*p));
418 /// \brief A wrapper for a TextInputStream optimised for reading a single character at a time.
419 template<typename TextInputStreamType, int SIZE = 1024>
420 class SingleCharacterInputStream
422 TextInputStreamType& m_inputStream;
429 m_end = m_buffer + m_inputStream.read(m_buffer, SIZE);
431 return m_cur != m_end;
435 SingleCharacterInputStream(TextInputStreamType& inputStream) : m_inputStream(inputStream), m_cur(m_buffer), m_end(m_buffer)
438 bool readChar(char& c)
440 if(m_cur == m_end && !fillBuffer())
450 /// \brief A wrapper for a TextOutputStream, optimised for writing a single character at a time.
451 class SingleCharacterOutputStream : public TextOutputStream
453 enum unnamed0 { m_bufsize = 1024 };
454 TextOutputStream& m_ostream;
455 char m_buffer[m_bufsize];
459 const char* end() const
469 m_ostream.write(m_buffer, m_pos - m_buffer);
473 SingleCharacterOutputStream(TextOutputStream& ostream) : m_ostream(ostream), m_pos(m_buffer), m_end(m_buffer+m_bufsize)
476 ~SingleCharacterOutputStream()
480 void write(const char c)
488 std::size_t write(const char* buffer, std::size_t length)
490 const char*const end = buffer + length;
491 for(const char* p = buffer; p != end; ++p)
499 /// \brief A wrapper for a TextOutputStream, optimised for writing a few characters at a time.
500 template<typename TextOutputStreamType, int SIZE = 1024>
501 class BufferedTextOutputStream : public TextOutputStream
503 TextOutputStreamType outputStream;
508 BufferedTextOutputStream(TextOutputStreamType& outputStream) : outputStream(outputStream), m_cur(m_buffer)
511 ~BufferedTextOutputStream()
513 outputStream.write(m_buffer, m_cur - m_buffer);
515 std::size_t write(const char* buffer, std::size_t length)
517 std::size_t remaining = length;
520 std::size_t n = std::min(remaining, std::size_t((m_buffer + SIZE) - m_cur));
521 m_cur = std::copy(buffer, buffer + n, m_cur);
527 outputStream.write(m_buffer, SIZE);