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 FloatFormat(double f, int width, int precision)
262 : m_f(f), m_width(width), m_precision(precision)
267 /// \brief Writes a floating point value to \p ostream with a specific width and precision.
268 template<typename TextOutputStreamType>
269 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const FloatFormat& formatted)
271 const std::size_t bufferSize = 32;
272 char buf[bufferSize];
273 ostream.write(buf, snprintf(buf, bufferSize, "%*.*lf", formatted.m_width, formatted.m_precision, formatted.m_f));
277 // never displays exponent, prints up to 10 decimal places
282 Decimal(double f) : m_f(f)
287 /// \brief Writes a floating point value to \p ostream in decimal form with trailing zeros removed.
288 template<typename TextOutputStreamType>
289 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Decimal& decimal)
291 const int bufferSize = 22;
292 char buf[bufferSize];
293 std::size_t length = snprintf(buf, bufferSize, "%10.10lf", decimal.m_f);
294 const char* first = buf;
295 for(; *first == ' '; ++first)
298 const char* last = buf + length - 1;
299 for(; *last == '0'; --last)
306 ostream.write(first, last - first + 1);
311 /// \brief Writes a \p range of characters to \p ostream.
312 template<typename TextOutputStreamType>
313 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const StringRange& range)
315 ostream.write(range.first, range.last - range.first);
319 template<typename Type>
324 Quoted(const Type& type)
330 template<typename Type>
331 inline Quoted<Type> makeQuoted(const Type& type)
333 return Quoted<Type>(type);
336 /// \brief Writes any type to \p ostream with a quotation mark character before and after it.
337 template<typename TextOutputStreamType, typename Type>
338 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Quoted<Type>& quoted)
340 return ostream << '"' << quoted.m_type << '"';
347 const char* m_string;
348 LowerCase(const char* string) : m_string(string)
353 /// \brief Writes a string to \p ostream converted to lower-case.
354 template<typename TextOutputStreamType>
355 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const LowerCase& lower)
357 for(const char* p = lower.m_string; *p != '\0'; ++p)
359 ostream << static_cast<char>(std::tolower(*p));
365 /// \brief A wrapper for a TextInputStream optimised for reading a single character at a time.
366 template<typename TextInputStreamType, int SIZE = 1024>
367 class SingleCharacterInputStream
369 TextInputStreamType& m_inputStream;
376 m_end = m_buffer + m_inputStream.read(m_buffer, SIZE);
378 return m_cur != m_end;
382 SingleCharacterInputStream(TextInputStreamType& inputStream) : m_inputStream(inputStream), m_cur(m_buffer), m_end(m_buffer)
385 bool readChar(char& c)
387 if(m_cur == m_end && !fillBuffer())
397 /// \brief A wrapper for a TextOutputStream, optimised for writing a single character at a time.
398 class SingleCharacterOutputStream : public TextOutputStream
400 enum unnamed0 { m_bufsize = 1024 };
401 TextOutputStream& m_ostream;
402 char m_buffer[m_bufsize];
406 const char* end() const
416 m_ostream.write(m_buffer, m_pos - m_buffer);
420 SingleCharacterOutputStream(TextOutputStream& ostream) : m_ostream(ostream), m_pos(m_buffer), m_end(m_buffer+m_bufsize)
423 ~SingleCharacterOutputStream()
427 void write(const char c)
435 std::size_t write(const char* buffer, std::size_t length)
437 const char*const end = buffer + length;
438 for(const char* p = buffer; p != end; ++p)
446 /// \brief A wrapper for a TextOutputStream, optimised for writing a few characters at a time.
447 template<typename TextOutputStreamType, int SIZE = 1024>
448 class BufferedTextOutputStream : public TextOutputStream
450 TextOutputStreamType outputStream;
455 BufferedTextOutputStream(TextOutputStreamType& outputStream) : outputStream(outputStream), m_cur(m_buffer)
458 ~BufferedTextOutputStream()
460 outputStream.write(m_buffer, m_cur - m_buffer);
462 std::size_t write(const char* buffer, std::size_t length)
464 std::size_t remaining = length;
467 std::size_t n = std::min(remaining, std::size_t((m_buffer + SIZE) - m_cur));
468 m_cur = std::copy(buffer, buffer + n, m_cur);
474 outputStream.write(m_buffer, SIZE);