]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/stream/textstream.h
ddc6cf45ef1678c7f8f7222547217f0844f7511c
[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
36 #include "generic/arrayrange.h"
37
38 namespace TextOutputDetail
39 {
40   inline char* write_unsigned_nonzero_decimal_backward(char* ptr, unsigned int decimal)
41   {
42     for (; decimal != 0; decimal /= 10)
43     {
44       *--ptr = char('0' + int(decimal % 10));
45     }
46     return ptr;
47   }
48
49   inline char* write_signed_nonzero_decimal_backward(char* ptr, int decimal, bool show_positive)
50   {
51     const bool negative = decimal < 0 ;
52     ptr = write_unsigned_nonzero_decimal_backward(ptr, negative ? -decimal : decimal);
53     if(negative)
54     {
55       *--ptr = '-';
56     }
57     else if(show_positive)
58     {
59       *--ptr = '+';
60     }
61     return ptr;
62   }
63
64   inline char* write_unsigned_nonzero_decimal_backward(char* ptr, unsigned int decimal, bool show_positive)
65   {
66     ptr = write_unsigned_nonzero_decimal_backward(ptr, decimal);
67     if(show_positive)
68     {
69       *--ptr = '+';
70     }
71     return ptr;
72   }
73
74   inline char* write_signed_decimal_backward(char* ptr, int decimal, bool show_positive)
75   {
76     if(decimal == 0)
77     {
78       *--ptr = '0';
79     }
80     else
81     {
82       ptr = write_signed_nonzero_decimal_backward(ptr, decimal, show_positive);
83     }
84     return ptr;
85   }
86
87   inline char* write_unsigned_decimal_backward(char* ptr, unsigned int decimal, bool show_positive)
88   {
89     if(decimal == 0)
90     {
91       *--ptr = '0';
92     }
93     else
94     {
95       ptr = write_unsigned_nonzero_decimal_backward(ptr, decimal, show_positive);
96     }
97     return ptr;
98   }
99 }
100
101
102 #ifdef WIN32
103 #define snprintf _snprintf
104 #endif
105
106 /// \brief Writes a single character \p c to \p ostream.
107 template<typename TextOutputStreamType>
108 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, char c)
109 {
110   ostream.write(&c, 1);
111   return ostream;
112 }
113
114 /// \brief Writes a double-precision floating point value \p d to \p ostream.
115 /// The value will be formatted either as decimal with trailing zeros removed, or with scientific 'e' notation, whichever is shorter.
116 template<typename TextOutputStreamType>
117 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const double d)
118 {
119   const std::size_t bufferSize = 16;
120   char buf[bufferSize];
121   ostream.write(buf, snprintf(buf, bufferSize, "%g", d));
122   return ostream;
123 }
124
125 /// \brief Writes a single-precision floating point value \p f to \p ostream.
126 /// The value will be formatted either as decimal with trailing zeros removed, or with scientific 'e' notation, whichever is shorter.
127 template<typename TextOutputStreamType>
128 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const float f)
129 {
130   return ostream_write(ostream, static_cast<double>(f));
131 }
132
133 /// \brief Writes a signed integer \p i to \p ostream in decimal form.
134 /// A '-' sign will be added if the value is negative.
135 template<typename TextOutputStreamType>
136 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const int i)
137 {
138   const std::size_t bufferSize = 16;
139 #if 1
140   char buf[bufferSize];
141   char* begin = TextOutputDetail::write_signed_decimal_backward(buf + bufferSize, i, false);
142   ostream.write(begin, (buf + bufferSize) - begin);
143 #else
144   char buf[bufferSize];
145   ostream.write(buf, snprintf(buf, bufferSize, "%i", i));
146 #endif
147   return ostream;
148 }
149
150 typedef unsigned int Unsigned;
151
152 /// \brief Writes an unsigned integer \p i to \p ostream in decimal form.
153 template<typename TextOutputStreamType>
154 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Unsigned i)
155 {
156   const std::size_t bufferSize = 16;
157 #if 1
158   char buf[bufferSize];
159   char* begin = TextOutputDetail::write_unsigned_decimal_backward(buf + bufferSize, i, false);
160   ostream.write(begin, (buf + bufferSize) - begin);
161 #else
162   char buf[bufferSize];
163   ostream.write(buf, snprintf(buf, bufferSize, "%u", i));
164 #endif
165   return ostream;
166 }
167
168 /// \brief Writes a null-terminated \p string to \p ostream.
169 template<typename TextOutputStreamType>
170 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const char* string)
171 {
172   ostream.write(string, strlen(string));
173   return ostream;
174 }
175
176 class HexChar
177 {
178 public:
179   char m_value;
180   HexChar(char value) : m_value(value)
181   {
182   }
183 };
184
185 /// \brief Writes a single character \p c to \p ostream in hexadecimal form.
186 template<typename TextOutputStreamType>
187 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const HexChar& c)
188 {
189   const std::size_t bufferSize = 16;
190   char buf[bufferSize];
191   ostream.write(buf, snprintf(buf, bufferSize, "%X", c.m_value & 0xFF));
192   return ostream;
193 }
194
195 template<typename T>
196 class LeftJustified
197 {
198 public:
199   const T& m_t;
200   std::size_t m_size;
201   LeftJustified(const T& t, std::size_t size) : m_t(t), m_size(size)
202   {
203   }
204 };
205
206 template<typename T>
207 LeftJustified<T> makeLeftJustified(const T& t, std::size_t size)
208 {
209   return LeftJustified<T>(t, size);
210 }
211
212 template<typename TextOutputStreamType>
213 class CountingOutputStream
214 {
215   TextOutputStreamType& m_ostream;
216 public:
217   std::size_t m_count;
218   CountingOutputStream(TextOutputStreamType& ostream) : m_ostream(ostream)
219   {
220   }
221   std::size_t write(const char* buffer, std::size_t length)
222   {
223     m_count += length;
224     return m_ostream.write(buffer, length);
225   }
226 };
227
228 template<typename TextOutputStreamType, typename T>
229 inline CountingOutputStream<TextOutputStreamType>& operator<<(CountingOutputStream<TextOutputStreamType>& ostream, const T& t)
230 {
231   return ostream_write(ostream, t);
232 }
233
234
235 /// \brief Writes any type to \p ostream padded with spaces afterwards.
236 template<typename TextOutputStreamType, typename T>
237 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const LeftJustified<T>& justified)
238 {
239   CountingOutputStream<TextOutputStreamType> count(ostream);
240   count << justified.m_t;
241   while(justified.m_size > count.m_count)
242   {
243     count << ' ';
244   }
245   return ostream;
246 }
247
248 class FloatFormat
249 {
250 public:
251   double m_f;
252   int m_width;
253   int m_precision;
254   FloatFormat(double f, int width, int precision)
255     : m_f(f), m_width(width), m_precision(precision)
256   {
257   }
258 };
259
260 /// \brief Writes a floating point value to \p ostream with a specific width and precision.
261 template<typename TextOutputStreamType>
262 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const FloatFormat& formatted)
263 {
264   const std::size_t bufferSize = 32;
265   char buf[bufferSize];
266   ostream.write(buf, snprintf(buf, bufferSize, "%*.*lf", formatted.m_width, formatted.m_precision, formatted.m_f));
267   return ostream;
268 }
269
270 // never displays exponent, prints up to 10 decimal places
271 class Decimal
272 {
273 public:
274   double m_f;
275   Decimal(double f) : m_f(f)
276   {
277   }
278 };
279
280 /// \brief Writes a floating point value to \p ostream in decimal form with trailing zeros removed.
281 template<typename TextOutputStreamType>
282 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Decimal& decimal)
283 {
284   const int bufferSize = 22;
285   char buf[bufferSize];
286   std::size_t length = snprintf(buf, bufferSize, "%10.10lf", decimal.m_f);
287   const char* first = buf;
288   for(; *first == ' '; ++first)
289   {
290   }
291   const char* last = buf + length - 1;
292   for(; *last == '0'; --last)
293   {
294   }
295   if(*last == '.')
296   {
297     --last;
298   }
299   ostream.write(first, last - first + 1);
300   return ostream;
301 }
302
303
304 typedef ArrayRange<const char> StringRange;
305
306 /// \brief Writes a \p range of characters to \p ostream.
307 template<typename TextOutputStreamType>
308 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const StringRange& range)
309 {
310   ostream.write(range.begin, range.end - range.begin);
311   return ostream;
312 }
313
314 template<typename Type>
315 class Quoted
316 {
317 public:
318   const Type& m_type;
319   Quoted(const Type& type)
320     : m_type(type)
321   {
322   }
323 };
324
325 template<typename Type>
326 inline Quoted<Type> makeQuoted(const Type& type)
327 {
328   return Quoted<Type>(type);
329 }
330
331 /// \brief Writes any type to \p ostream with a quotation mark character before and after it.
332 template<typename TextOutputStreamType, typename Type>
333 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Quoted<Type>& quoted)
334 {
335   return ostream << '"' << quoted.m_type << '"';
336 }
337
338
339 class LowerCase
340 {
341 public:
342   const char* m_string;
343   LowerCase(const char* string) : m_string(string)
344   {
345   }
346 };
347
348 /// \brief Writes a string to \p ostream converted to lower-case.
349 template<typename TextOutputStreamType>
350 inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const LowerCase& lower)
351 {
352   for(const char* p = lower.m_string; *p != '\0'; ++p)
353   {
354     ostream << static_cast<char>(std::tolower(*p));
355   }
356   return ostream;
357 }
358
359
360 /// \brief A wrapper for a TextInputStream optimised for reading a single character at a time.
361 template<typename TextInputStreamType, int SIZE = 1024>
362 class SingleCharacterInputStream
363 {
364   TextInputStreamType& m_inputStream;
365   char m_buffer[SIZE];
366   char* m_cur;
367   char* m_end;
368
369   bool fillBuffer()
370   {
371     m_end = m_buffer + m_inputStream.read(m_buffer, SIZE);
372     m_cur = m_buffer;
373     return m_cur != m_end;
374   }
375 public:
376
377   SingleCharacterInputStream(TextInputStreamType& inputStream) : m_inputStream(inputStream), m_cur(m_buffer), m_end(m_buffer)
378   {
379   }
380   bool readChar(char& c)
381   {
382     if(m_cur == m_end && !fillBuffer())
383     {
384       return false;
385     }
386
387     c = *m_cur++;
388     return true;
389   }
390 };
391
392 #endif