]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/stream/textstream.h
added string-pooling for shader variable names and entity keys
[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 /// \brief Writes a \p range of characters to \p ostream.
306 template<typename TextOutputStreamType>
307 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const StringRange& range)
308 {
309   ostream.write(range.first, range.last - range.first);
310   return ostream;
311 }
312
313 template<typename Type>
314 class Quoted
315 {
316 public:
317   const Type& m_type;
318   Quoted(const Type& type)
319     : m_type(type)
320   {
321   }
322 };
323
324 template<typename Type>
325 inline Quoted<Type> makeQuoted(const Type& type)
326 {
327   return Quoted<Type>(type);
328 }
329
330 /// \brief Writes any type to \p ostream with a quotation mark character before and after it.
331 template<typename TextOutputStreamType, typename Type>
332 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Quoted<Type>& quoted)
333 {
334   return ostream << '"' << quoted.m_type << '"';
335 }
336
337
338 class LowerCase
339 {
340 public:
341   const char* m_string;
342   LowerCase(const char* string) : m_string(string)
343   {
344   }
345 };
346
347 /// \brief Writes a string to \p ostream converted to lower-case.
348 template<typename TextOutputStreamType>
349 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const LowerCase& lower)
350 {
351   for(const char* p = lower.m_string; *p != '\0'; ++p)
352   {
353     ostream << static_cast<char>(std::tolower(*p));
354   }
355   return ostream;
356 }
357
358
359 /// \brief A wrapper for a TextInputStream optimised for reading a single character at a time.
360 template<typename TextInputStreamType, int SIZE = 1024>
361 class SingleCharacterInputStream
362 {
363   TextInputStreamType& m_inputStream;
364   char m_buffer[SIZE];
365   char* m_cur;
366   char* m_end;
367
368   bool fillBuffer()
369   {
370     m_end = m_buffer + m_inputStream.read(m_buffer, SIZE);
371     m_cur = m_buffer;
372     return m_cur != m_end;
373   }
374 public:
375
376   SingleCharacterInputStream(TextInputStreamType& inputStream) : m_inputStream(inputStream), m_cur(m_buffer), m_end(m_buffer)
377   {
378   }
379   bool readChar(char& c)
380   {
381     if(m_cur == m_end && !fillBuffer())
382     {
383       return false;
384     }
385
386     c = *m_cur++;
387     return true;
388   }
389 };
390
391 /// \brief A wrapper for a TextOutputStream, optimised for writing a single character at a time.
392 class SingleCharacterOutputStream : public TextOutputStream
393 {
394   enum unnamed0 { m_bufsize = 1024 };
395   TextOutputStream& m_ostream;
396   char m_buffer[m_bufsize];
397   char* m_pos;
398   const char* m_end;
399
400   const char* end() const
401   {
402     return m_end;
403   }
404   void reset()
405   {
406     m_pos = m_buffer;
407   }
408   void flush()
409   {
410     m_ostream.write(m_buffer, m_pos - m_buffer);
411     reset();
412   }
413 public:
414   SingleCharacterOutputStream(TextOutputStream& ostream) : m_ostream(ostream), m_pos(m_buffer), m_end(m_buffer+m_bufsize)
415   {
416   }
417   ~SingleCharacterOutputStream()
418   {
419     flush();
420   }
421   void write(const char c)
422   {
423     if(m_pos == end())
424     {
425       flush();
426     }
427     *m_pos++ = c;
428   }
429   std::size_t write(const char* buffer, std::size_t length)
430   {
431     const char*const end = buffer + length;
432     for(const char* p = buffer; p != end; ++p)
433     {
434       write(*p);
435     }
436     return length;
437   }
438 };
439
440 /// \brief A wrapper for a TextOutputStream, optimised for writing a few characters at a time.
441 template<typename TextOutputStreamType, int SIZE = 1024>
442 class BufferedTextOutputStream : public TextOutputStream
443 {
444   TextOutputStreamType outputStream;
445   char m_buffer[SIZE];
446   char* m_cur;
447
448 public:
449   BufferedTextOutputStream(TextOutputStreamType& outputStream) : outputStream(outputStream), m_cur(m_buffer)
450   {
451   }
452   ~BufferedTextOutputStream()
453   {
454     outputStream.write(m_buffer, m_cur - m_buffer);
455   }
456   std::size_t write(const char* buffer, std::size_t length)
457   {
458     std::size_t remaining = length;
459     for(;;)
460     {
461       std::size_t n = std::min(remaining, std::size_t((m_buffer + SIZE) - m_cur));
462       m_cur = std::copy(buffer, buffer + n, m_cur);
463       remaining -= n;
464       if(remaining == 0)
465       {
466         return 0;
467       }
468       outputStream.write(m_buffer, SIZE);
469       m_cur = m_buffer;
470     }
471   }
472 };
473
474 #endif