]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/stream/textstream.h
fixed gcc compile errors
[xonotic/netradiant.git] / libs / stream / textstream.h
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
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.
11
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.
16
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
20 */
21
22 #if !defined(INCLUDED_STREAM_TEXTSTREAM_H)
23 #define INCLUDED_STREAM_TEXTSTREAM_H
24
25 /// \file
26 /// \brief Text-output-formatting.
27
28 #include "itextstream.h"
29
30 #include <cctype>
31 #include <cstddef>
32 #include <cmath>
33 #include <stdio.h>
34 #include <string.h>
35 #include <algorithm>
36
37 #include "generic/arrayrange.h"
38
39 namespace TextOutputDetail
40 {
41   inline char* write_unsigned_nonzero_decimal_backward(char* ptr, unsigned int decimal)
42   {
43     for (; decimal != 0; decimal /= 10)
44     {
45       *--ptr = char('0' + int(decimal % 10));
46     }
47     return ptr;
48   }
49
50   inline char* write_signed_nonzero_decimal_backward(char* ptr, int decimal, bool show_positive)
51   {
52     const bool negative = decimal < 0 ;
53     ptr = write_unsigned_nonzero_decimal_backward(ptr, negative ? -decimal : decimal);
54     if(negative)
55     {
56       *--ptr = '-';
57     }
58     else if(show_positive)
59     {
60       *--ptr = '+';
61     }
62     return ptr;
63   }
64
65   inline char* write_unsigned_nonzero_decimal_backward(char* ptr, unsigned int decimal, bool show_positive)
66   {
67     ptr = write_unsigned_nonzero_decimal_backward(ptr, decimal);
68     if(show_positive)
69     {
70       *--ptr = '+';
71     }
72     return ptr;
73   }
74
75   inline char* write_signed_decimal_backward(char* ptr, int decimal, bool show_positive)
76   {
77     if(decimal == 0)
78     {
79       *--ptr = '0';
80     }
81     else
82     {
83       ptr = write_signed_nonzero_decimal_backward(ptr, decimal, show_positive);
84     }
85     return ptr;
86   }
87
88   inline char* write_unsigned_decimal_backward(char* ptr, unsigned int decimal, bool show_positive)
89   {
90     if(decimal == 0)
91     {
92       *--ptr = '0';
93     }
94     else
95     {
96       ptr = write_unsigned_nonzero_decimal_backward(ptr, decimal, show_positive);
97     }
98     return ptr;
99   }
100 }
101
102
103 #ifdef WIN32
104 #define snprintf _snprintf
105 #endif
106
107 /// \brief Writes a single character \p c to \p ostream.
108 template<typename TextOutputStreamType>
109 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, char c)
110 {
111   ostream.write(&c, 1);
112   return ostream;
113 }
114
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)
119 {
120   const std::size_t bufferSize = 16;
121   char buf[bufferSize];
122   ostream.write(buf, snprintf(buf, bufferSize, "%g", d));
123   return ostream;
124 }
125
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)
130 {
131   return ostream_write(ostream, static_cast<double>(f));
132 }
133
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)
138 {
139   const std::size_t bufferSize = 16;
140 #if 1
141   char buf[bufferSize];
142   char* begin = TextOutputDetail::write_signed_decimal_backward(buf + bufferSize, i, false);
143   ostream.write(begin, (buf + bufferSize) - begin);
144 #else
145   char buf[bufferSize];
146   ostream.write(buf, snprintf(buf, bufferSize, "%i", i));
147 #endif
148   return ostream;
149 }
150
151 typedef unsigned int Unsigned;
152
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)
156 {
157   const std::size_t bufferSize = 16;
158 #if 1
159   char buf[bufferSize];
160   char* begin = TextOutputDetail::write_unsigned_decimal_backward(buf + bufferSize, i, false);
161   ostream.write(begin, (buf + bufferSize) - begin);
162 #else
163   char buf[bufferSize];
164   ostream.write(buf, snprintf(buf, bufferSize, "%u", i));
165 #endif
166   return ostream;
167 }
168
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)
172 {
173   ostream.write(string, strlen(string));
174   return ostream;
175 }
176
177 class HexChar
178 {
179 public:
180   char m_value;
181   HexChar(char value) : m_value(value)
182   {
183   }
184 };
185
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)
189 {
190   const std::size_t bufferSize = 16;
191   char buf[bufferSize];
192   ostream.write(buf, snprintf(buf, bufferSize, "%X", c.m_value & 0xFF));
193   return ostream;
194 }
195
196 template<typename T>
197 class LeftJustified
198 {
199 public:
200   const T& m_t;
201   std::size_t m_size;
202   LeftJustified(const T& t, std::size_t size) : m_t(t), m_size(size)
203   {
204   }
205 };
206
207 template<typename T>
208 LeftJustified<T> makeLeftJustified(const T& t, std::size_t size)
209 {
210   return LeftJustified<T>(t, size);
211 }
212
213 template<typename TextOutputStreamType>
214 class CountingOutputStream
215 {
216   TextOutputStreamType& m_ostream;
217 public:
218   std::size_t m_count;
219   CountingOutputStream(TextOutputStreamType& ostream) : m_ostream(ostream)
220   {
221   }
222   std::size_t write(const char* buffer, std::size_t length)
223   {
224     m_count += length;
225     return m_ostream.write(buffer, length);
226   }
227 };
228
229 template<typename TextOutputStreamType, typename T>
230 inline CountingOutputStream<TextOutputStreamType>& operator<<(CountingOutputStream<TextOutputStreamType>& ostream, const T& t)
231 {
232   return ostream_write(ostream, t);
233 }
234
235
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)
239 {
240   CountingOutputStream<TextOutputStreamType> count(ostream);
241   count << justified.m_t;
242   while(justified.m_size > count.m_count)
243   {
244     count << ' ';
245   }
246   return ostream;
247 }
248
249 class FloatFormat
250 {
251 public:
252   double m_f;
253   int m_width;
254   int m_precision;
255   FloatFormat(double f, int width, int precision)
256     : m_f(f), m_width(width), m_precision(precision)
257   {
258   }
259 };
260
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)
264 {
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));
268   return ostream;
269 }
270
271 // never displays exponent, prints up to 10 decimal places
272 class Decimal
273 {
274 public:
275   double m_f;
276   Decimal(double f) : m_f(f)
277   {
278   }
279 };
280
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)
284 {
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)
290   {
291   }
292   const char* last = buf + length - 1;
293   for(; *last == '0'; --last)
294   {
295   }
296   if(*last == '.')
297   {
298     --last;
299   }
300   ostream.write(first, last - first + 1);
301   return ostream;
302 }
303
304
305 typedef ArrayRange<const char> StringRange;
306
307 /// \brief Writes a \p range of characters to \p ostream.
308 template<typename TextOutputStreamType>
309 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const StringRange& range)
310 {
311   ostream.write(range.begin, range.end - range.begin);
312   return ostream;
313 }
314
315 template<typename Type>
316 class Quoted
317 {
318 public:
319   const Type& m_type;
320   Quoted(const Type& type)
321     : m_type(type)
322   {
323   }
324 };
325
326 template<typename Type>
327 inline Quoted<Type> makeQuoted(const Type& type)
328 {
329   return Quoted<Type>(type);
330 }
331
332 /// \brief Writes any type to \p ostream with a quotation mark character before and after it.
333 template<typename TextOutputStreamType, typename Type>
334 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Quoted<Type>& quoted)
335 {
336   return ostream << '"' << quoted.m_type << '"';
337 }
338
339
340 class LowerCase
341 {
342 public:
343   const char* m_string;
344   LowerCase(const char* string) : m_string(string)
345   {
346   }
347 };
348
349 /// \brief Writes a string to \p ostream converted to lower-case.
350 template<typename TextOutputStreamType>
351 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const LowerCase& lower)
352 {
353   for(const char* p = lower.m_string; *p != '\0'; ++p)
354   {
355     ostream << static_cast<char>(std::tolower(*p));
356   }
357   return ostream;
358 }
359
360
361 /// \brief A wrapper for a TextInputStream optimised for reading a single character at a time.
362 template<typename TextInputStreamType, int SIZE = 1024>
363 class SingleCharacterInputStream
364 {
365   TextInputStreamType& m_inputStream;
366   char m_buffer[SIZE];
367   char* m_cur;
368   char* m_end;
369
370   bool fillBuffer()
371   {
372     m_end = m_buffer + m_inputStream.read(m_buffer, SIZE);
373     m_cur = m_buffer;
374     return m_cur != m_end;
375   }
376 public:
377
378   SingleCharacterInputStream(TextInputStreamType& inputStream) : m_inputStream(inputStream), m_cur(m_buffer), m_end(m_buffer)
379   {
380   }
381   bool readChar(char& c)
382   {
383     if(m_cur == m_end && !fillBuffer())
384     {
385       return false;
386     }
387
388     c = *m_cur++;
389     return true;
390   }
391 };
392
393 /// \brief A wrapper for a TextOutputStream, optimised for writing a single character at a time.
394 class SingleCharacterOutputStream : public TextOutputStream
395 {
396   enum unnamed0 { m_bufsize = 1024 };
397   TextOutputStream& m_ostream;
398   char m_buffer[m_bufsize];
399   char* m_pos;
400   const char* m_end;
401
402   const char* end() const
403   {
404     return m_end;
405   }
406   void reset()
407   {
408     m_pos = m_buffer;
409   }
410   void flush()
411   {
412     m_ostream.write(m_buffer, m_pos - m_buffer);
413     reset();
414   }
415 public:
416   SingleCharacterOutputStream(TextOutputStream& ostream) : m_ostream(ostream), m_pos(m_buffer), m_end(m_buffer+m_bufsize)
417   {
418   }
419   ~SingleCharacterOutputStream()
420   {
421     flush();
422   }
423   void write(const char c)
424   {
425     if(m_pos == end())
426     {
427       flush();
428     }
429     *m_pos++ = c;
430   }
431   std::size_t write(const char* buffer, std::size_t length)
432   {
433     const char*const end = buffer + length;
434     for(const char* p = buffer; p != end; ++p)
435     {
436       write(*p);
437     }
438     return length;
439   }
440 };
441
442 /// \brief A wrapper for a TextOutputStream, optimised for writing a few characters at a time.
443 template<typename TextOutputStreamType, int SIZE = 1024>
444 class BufferedTextOutputStream : public TextOutputStream
445 {
446   TextOutputStreamType outputStream;
447   char m_buffer[SIZE];
448   char* m_cur;
449
450 public:
451   BufferedTextOutputStream(TextOutputStreamType& outputStream) : outputStream(outputStream), m_cur(m_buffer)
452   {
453   }
454   ~BufferedTextOutputStream()
455   {
456     outputStream.write(m_buffer, m_cur - m_buffer);
457   }
458   std::size_t write(const char* buffer, std::size_t length)
459   {
460     std::size_t remaining = length;
461     for(;;)
462     {
463       std::size_t n = std::min(remaining, std::size_t((m_buffer + SIZE) - m_cur));
464       m_cur = std::copy(buffer, buffer + n, m_cur);
465       remaining -= n;
466       if(remaining == 0)
467       {
468         return 0;
469       }
470       outputStream.write(m_buffer, SIZE);
471       m_cur = m_buffer;
472     }
473   }
474 };
475
476 #endif