]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - misc/mediasource/extra/netradiant-src/libs/stream/textstream.h
Include netRadiant source in this GIT
[voretournament/voretournament.git] / misc / mediasource / extra / netradiant-src / 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 class FloatFormat
256 {
257 public:
258   double m_f;
259   int m_width;
260   int m_precision;
261   FloatFormat(double f, int width, int precision)
262     : m_f(f), m_width(width), m_precision(precision)
263   {
264   }
265 };
266
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)
270 {
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));
274   return ostream;
275 }
276
277 // never displays exponent, prints up to 10 decimal places
278 class Decimal
279 {
280 public:
281   double m_f;
282   Decimal(double f) : m_f(f)
283   {
284   }
285 };
286
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)
290 {
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)
296   {
297   }
298   const char* last = buf + length - 1;
299   for(; *last == '0'; --last)
300   {
301   }
302   if(*last == '.')
303   {
304     --last;
305   }
306   ostream.write(first, last - first + 1);
307   return ostream;
308 }
309
310
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)
314 {
315   ostream.write(range.first, range.last - range.first);
316   return ostream;
317 }
318
319 template<typename Type>
320 class Quoted
321 {
322 public:
323   const Type& m_type;
324   Quoted(const Type& type)
325     : m_type(type)
326   {
327   }
328 };
329
330 template<typename Type>
331 inline Quoted<Type> makeQuoted(const Type& type)
332 {
333   return Quoted<Type>(type);
334 }
335
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)
339 {
340   return ostream << '"' << quoted.m_type << '"';
341 }
342
343
344 class LowerCase
345 {
346 public:
347   const char* m_string;
348   LowerCase(const char* string) : m_string(string)
349   {
350   }
351 };
352
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)
356 {
357   for(const char* p = lower.m_string; *p != '\0'; ++p)
358   {
359     ostream << static_cast<char>(std::tolower(*p));
360   }
361   return ostream;
362 }
363
364
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
368 {
369   TextInputStreamType& m_inputStream;
370   char m_buffer[SIZE];
371   char* m_cur;
372   char* m_end;
373
374   bool fillBuffer()
375   {
376     m_end = m_buffer + m_inputStream.read(m_buffer, SIZE);
377     m_cur = m_buffer;
378     return m_cur != m_end;
379   }
380 public:
381
382   SingleCharacterInputStream(TextInputStreamType& inputStream) : m_inputStream(inputStream), m_cur(m_buffer), m_end(m_buffer)
383   {
384   }
385   bool readChar(char& c)
386   {
387     if(m_cur == m_end && !fillBuffer())
388     {
389       return false;
390     }
391
392     c = *m_cur++;
393     return true;
394   }
395 };
396
397 /// \brief A wrapper for a TextOutputStream, optimised for writing a single character at a time.
398 class SingleCharacterOutputStream : public TextOutputStream
399 {
400   enum unnamed0 { m_bufsize = 1024 };
401   TextOutputStream& m_ostream;
402   char m_buffer[m_bufsize];
403   char* m_pos;
404   const char* m_end;
405
406   const char* end() const
407   {
408     return m_end;
409   }
410   void reset()
411   {
412     m_pos = m_buffer;
413   }
414   void flush()
415   {
416     m_ostream.write(m_buffer, m_pos - m_buffer);
417     reset();
418   }
419 public:
420   SingleCharacterOutputStream(TextOutputStream& ostream) : m_ostream(ostream), m_pos(m_buffer), m_end(m_buffer+m_bufsize)
421   {
422   }
423   ~SingleCharacterOutputStream()
424   {
425     flush();
426   }
427   void write(const char c)
428   {
429     if(m_pos == end())
430     {
431       flush();
432     }
433     *m_pos++ = c;
434   }
435   std::size_t write(const char* buffer, std::size_t length)
436   {
437     const char*const end = buffer + length;
438     for(const char* p = buffer; p != end; ++p)
439     {
440       write(*p);
441     }
442     return length;
443   }
444 };
445
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
449 {
450   TextOutputStreamType outputStream;
451   char m_buffer[SIZE];
452   char* m_cur;
453
454 public:
455   BufferedTextOutputStream(TextOutputStreamType& outputStream) : outputStream(outputStream), m_cur(m_buffer)
456   {
457   }
458   ~BufferedTextOutputStream()
459   {
460     outputStream.write(m_buffer, m_cur - m_buffer);
461   }
462   std::size_t write(const char* buffer, std::size_t length)
463   {
464     std::size_t remaining = length;
465     for(;;)
466     {
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);
469       remaining -= n;
470       if(remaining == 0)
471       {
472         return 0;
473       }
474       outputStream.write(m_buffer, SIZE);
475       m_cur = m_buffer;
476     }
477   }
478 };
479
480 #endif