]> icculus.org git repositories - divverent/netradiant.git/blob - libs/stream/textstream.h
initial
[divverent/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   #if defined (_WIN64) || defined (__LP64__)
51   inline char* write_size_t_nonzero_decimal_backward(char* ptr, size_t decimal)
52   {
53     for (; decimal != 0; decimal /= 10)
54     {
55       *--ptr = char('0' + (size_t)(decimal % 10));
56     }
57     return ptr;
58   }
59   #endif
60
61   inline char* write_signed_nonzero_decimal_backward(char* ptr, int decimal, bool show_positive)
62   {
63     const bool negative = decimal < 0 ;
64     ptr = write_unsigned_nonzero_decimal_backward(ptr, negative ? -decimal : decimal);
65     if(negative)
66     {
67       *--ptr = '-';
68     }
69     else if(show_positive)
70     {
71       *--ptr = '+';
72     }
73     return ptr;
74   }
75
76   inline char* write_unsigned_nonzero_decimal_backward(char* ptr, unsigned int decimal, bool show_positive)
77   {
78     ptr = write_unsigned_nonzero_decimal_backward(ptr, decimal);
79     if(show_positive)
80     {
81       *--ptr = '+';
82     }
83     return ptr;
84   }
85   
86   #if defined (_WIN64) || defined (__LP64__)
87   inline char* write_size_t_nonzero_decimal_backward(char* ptr, size_t decimal, bool show_positive)
88   {
89     ptr = write_size_t_nonzero_decimal_backward(ptr, decimal);
90     if(show_positive)
91     {
92       *--ptr = '+';
93     }
94     return ptr;
95   }
96   #endif
97
98   inline char* write_signed_decimal_backward(char* ptr, int decimal, bool show_positive)
99   {
100     if(decimal == 0)
101     {
102       *--ptr = '0';
103     }
104     else
105     {
106       ptr = write_signed_nonzero_decimal_backward(ptr, decimal, show_positive);
107     }
108     return ptr;
109   }
110
111   inline char* write_unsigned_decimal_backward(char* ptr, unsigned int decimal, bool show_positive)
112   {
113     if(decimal == 0)
114     {
115       *--ptr = '0';
116     }
117     else
118     {
119       ptr = write_unsigned_nonzero_decimal_backward(ptr, decimal, show_positive);
120     }
121     return ptr;
122   }
123   
124   #if defined (_WIN64) || defined (__LP64__)
125   inline char* write_size_t_decimal_backward(char* ptr, size_t decimal, bool show_positive)
126   {
127     if(decimal == 0)
128     {
129       *--ptr = '0';
130     }
131     else
132     {
133       ptr = write_size_t_nonzero_decimal_backward(ptr, decimal, show_positive);
134     }
135     return ptr;
136   }
137   #endif
138 }
139
140
141 #ifdef WIN32
142 #define snprintf _snprintf
143 #endif
144
145 /// \brief Writes a single character \p c to \p ostream.
146 template<typename TextOutputStreamType>
147 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, char c)
148 {
149   ostream.write(&c, 1);
150   return ostream;
151 }
152
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)
157 {
158   const std::size_t bufferSize = 16;
159   char buf[bufferSize];
160   ostream.write(buf, snprintf(buf, bufferSize, "%g", d));
161   return ostream;
162 }
163
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)
168 {
169   return ostream_write(ostream, static_cast<double>(f));
170 }
171
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)
176 {
177   const std::size_t bufferSize = 16;
178 #if 1
179   char buf[bufferSize];
180   char* begin = TextOutputDetail::write_signed_decimal_backward(buf + bufferSize, i, false);
181   ostream.write(begin, (buf + bufferSize) - begin);
182 #else
183   char buf[bufferSize];
184   ostream.write(buf, snprintf(buf, bufferSize, "%i", i));
185 #endif
186   return ostream;
187 }
188
189 typedef unsigned int Unsigned;
190
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)
194 {
195   const std::size_t bufferSize = 16;
196 #if 1
197   char buf[bufferSize];
198   char* begin = TextOutputDetail::write_unsigned_decimal_backward(buf + bufferSize, i, false);
199   ostream.write(begin, (buf + bufferSize) - begin);
200 #else
201   char buf[bufferSize];
202   ostream.write(buf, snprintf(buf, bufferSize, "%u", i));
203 #endif
204   return ostream;
205 }
206
207 #if defined (_WIN64) || defined (__LP64__)
208
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)
212 {
213   // max is 18446744073709551615, buffer of 32 chars should always be enough
214   const std::size_t bufferSize = 32;
215 #if 1
216   char buf[bufferSize];
217   char* begin = TextOutputDetail::write_size_t_decimal_backward(buf + bufferSize, i, false);
218   ostream.write(begin, (buf + bufferSize) - begin);
219 #else
220   char buf[bufferSize];
221   ostream.write(buf, snprintf(buf, bufferSize, "%u", i));
222 #endif
223   return ostream;
224 }
225
226 #endif
227
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)
231 {
232   ostream.write(string, strlen(string));
233   return ostream;
234 }
235
236 class HexChar
237 {
238 public:
239   char m_value;
240   HexChar(char value) : m_value(value)
241   {
242   }
243 };
244
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)
248 {
249   const std::size_t bufferSize = 16;
250   char buf[bufferSize];
251   ostream.write(buf, snprintf(buf, bufferSize, "%X", c.m_value & 0xFF));
252   return ostream;
253 }
254
255 template<typename T>
256 class LeftJustified
257 {
258 public:
259   const T& m_t;
260   std::size_t m_size;
261   LeftJustified(const T& t, std::size_t size) : m_t(t), m_size(size)
262   {
263   }
264 };
265
266 template<typename T>
267 LeftJustified<T> makeLeftJustified(const T& t, std::size_t size)
268 {
269   return LeftJustified<T>(t, size);
270 }
271
272 template<typename TextOutputStreamType>
273 class CountingOutputStream
274 {
275   TextOutputStreamType& m_ostream;
276 public:
277   std::size_t m_count;
278   CountingOutputStream(TextOutputStreamType& ostream) : m_ostream(ostream)
279   {
280   }
281   std::size_t write(const char* buffer, std::size_t length)
282   {
283     m_count += length;
284     return m_ostream.write(buffer, length);
285   }
286 };
287
288 template<typename TextOutputStreamType, typename T>
289 inline CountingOutputStream<TextOutputStreamType>& operator<<(CountingOutputStream<TextOutputStreamType>& ostream, const T& t)
290 {
291   return ostream_write(ostream, t);
292 }
293
294
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)
298 {
299   CountingOutputStream<TextOutputStreamType> count(ostream);
300   count << justified.m_t;
301   while(justified.m_size > count.m_count)
302   {
303     count << ' ';
304   }
305   return ostream;
306 }
307
308 class FloatFormat
309 {
310 public:
311   double m_f;
312   int m_width;
313   int m_precision;
314   FloatFormat(double f, int width, int precision)
315     : m_f(f), m_width(width), m_precision(precision)
316   {
317   }
318 };
319
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)
323 {
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));
327   return ostream;
328 }
329
330 // never displays exponent, prints up to 10 decimal places
331 class Decimal
332 {
333 public:
334   double m_f;
335   Decimal(double f) : m_f(f)
336   {
337   }
338 };
339
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)
343 {
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)
349   {
350   }
351   const char* last = buf + length - 1;
352   for(; *last == '0'; --last)
353   {
354   }
355   if(*last == '.')
356   {
357     --last;
358   }
359   ostream.write(first, last - first + 1);
360   return ostream;
361 }
362
363
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)
367 {
368   ostream.write(range.first, range.last - range.first);
369   return ostream;
370 }
371
372 template<typename Type>
373 class Quoted
374 {
375 public:
376   const Type& m_type;
377   Quoted(const Type& type)
378     : m_type(type)
379   {
380   }
381 };
382
383 template<typename Type>
384 inline Quoted<Type> makeQuoted(const Type& type)
385 {
386   return Quoted<Type>(type);
387 }
388
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)
392 {
393   return ostream << '"' << quoted.m_type << '"';
394 }
395
396
397 class LowerCase
398 {
399 public:
400   const char* m_string;
401   LowerCase(const char* string) : m_string(string)
402   {
403   }
404 };
405
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)
409 {
410   for(const char* p = lower.m_string; *p != '\0'; ++p)
411   {
412     ostream << static_cast<char>(std::tolower(*p));
413   }
414   return ostream;
415 }
416
417
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
421 {
422   TextInputStreamType& m_inputStream;
423   char m_buffer[SIZE];
424   char* m_cur;
425   char* m_end;
426
427   bool fillBuffer()
428   {
429     m_end = m_buffer + m_inputStream.read(m_buffer, SIZE);
430     m_cur = m_buffer;
431     return m_cur != m_end;
432   }
433 public:
434
435   SingleCharacterInputStream(TextInputStreamType& inputStream) : m_inputStream(inputStream), m_cur(m_buffer), m_end(m_buffer)
436   {
437   }
438   bool readChar(char& c)
439   {
440     if(m_cur == m_end && !fillBuffer())
441     {
442       return false;
443     }
444
445     c = *m_cur++;
446     return true;
447   }
448 };
449
450 /// \brief A wrapper for a TextOutputStream, optimised for writing a single character at a time.
451 class SingleCharacterOutputStream : public TextOutputStream
452 {
453   enum unnamed0 { m_bufsize = 1024 };
454   TextOutputStream& m_ostream;
455   char m_buffer[m_bufsize];
456   char* m_pos;
457   const char* m_end;
458
459   const char* end() const
460   {
461     return m_end;
462   }
463   void reset()
464   {
465     m_pos = m_buffer;
466   }
467   void flush()
468   {
469     m_ostream.write(m_buffer, m_pos - m_buffer);
470     reset();
471   }
472 public:
473   SingleCharacterOutputStream(TextOutputStream& ostream) : m_ostream(ostream), m_pos(m_buffer), m_end(m_buffer+m_bufsize)
474   {
475   }
476   ~SingleCharacterOutputStream()
477   {
478     flush();
479   }
480   void write(const char c)
481   {
482     if(m_pos == end())
483     {
484       flush();
485     }
486     *m_pos++ = c;
487   }
488   std::size_t write(const char* buffer, std::size_t length)
489   {
490     const char*const end = buffer + length;
491     for(const char* p = buffer; p != end; ++p)
492     {
493       write(*p);
494     }
495     return length;
496   }
497 };
498
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
502 {
503   TextOutputStreamType outputStream;
504   char m_buffer[SIZE];
505   char* m_cur;
506
507 public:
508   BufferedTextOutputStream(TextOutputStreamType& outputStream) : outputStream(outputStream), m_cur(m_buffer)
509   {
510   }
511   ~BufferedTextOutputStream()
512   {
513     outputStream.write(m_buffer, m_cur - m_buffer);
514   }
515   std::size_t write(const char* buffer, std::size_t length)
516   {
517     std::size_t remaining = length;
518     for(;;)
519     {
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);
522       remaining -= n;
523       if(remaining == 0)
524       {
525         return 0;
526       }
527       outputStream.write(m_buffer, SIZE);
528       m_cur = m_buffer;
529     }
530   }
531 };
532
533 #endif