summaryrefslogtreecommitdiff
path: root/src/formatter.h
blob: db144ebea8c012a75e3c4f97282302d67e2fd071 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 * Copyright (C) 2007-2008 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#ifndef BU_FORMATTER_H
#define BU_FORMATTER_H

#include "stream.h"

namespace Bu
{
	class Formatter
	{
	public:
		Formatter( Stream &rOut );
		virtual ~Formatter();

		typedef struct Fmt
		{
			enum Alignment
			{
				Left	= 0,
				Center	= 1,
				Right	= 2
			};
			Fmt() :
				uMinWidth( 0 ),
				cFillChar(' '),
				uRadix( 10 ),
				uAlign( Right ),
				bPlus( false ),
				bCaps( true )
			{
			}

			Fmt( unsigned int uMinWidth, unsigned int uRadix=10,
					Alignment a=Right, bool bPlus=false, bool bCaps=true,
					char cFill=' ') :
				uMinWidth( uMinWidth ),
				cFillChar(cFill),
				uRadix( uRadix ),
				uAlign( a ),
				bPlus( bPlus ),
				bCaps( bCaps )
			{
			}
			Fmt( unsigned int uMinWidth, Alignment a,
					unsigned int uRadix=10, bool bPlus=false, bool bCaps=true,
					char cFill=' ') :
				uMinWidth( uMinWidth ),
				cFillChar(cFill),
				uRadix( uRadix ),
				uAlign( a ),
				bPlus( bPlus ),
				bCaps( bCaps )
			{
			}

			static Fmt hex( unsigned int uWidth=0, bool bCaps=true )
			{
				return Fmt( uWidth, 16, Right, false, bCaps, '0' );
			}
			
			static Fmt oct( unsigned int uWidth=0 )
			{
				return Fmt( uWidth, 8, Right, false, false, '0' );
			}

			static Fmt bin( unsigned int uWidth=0 )
			{
				return Fmt( uWidth, 1, Right, false, false, '0' );
			}

			static Fmt ptr( bool bCaps=true )
			{
				return Fmt( sizeof(ptrdiff_t)*2, 16, Right, false, bCaps, '0' );
			}

			Fmt &width( unsigned int uWidth );
			Fmt &fill( char cFill='0' );
			Fmt &radix( unsigned int uRadix );
			Fmt &align( Alignment eAlign );
			Fmt &plus( bool bPlus=true );
			Fmt &caps( bool bCaps=true );

			Fmt &left();
			Fmt &right();
			Fmt &center();

			unsigned char uMinWidth;
			char cFillChar;
			unsigned short uRadix : 6;
			unsigned short uAlign : 2;
			unsigned short bPlus : 1;
			unsigned short bCaps : 1;
		} Fmt;

		void write( const Bu::FString &sStr );
		void write( const void *sStr, int iLen );
		void writeAligned( const Bu::FString &sStr );
		void writeAligned( const char *sStr, int iLen );

		void incIndent();
		void decIndent();
		void setIndent( uint8_t uLevel );
		void clearIndent();
		uint8_t getIndent() const { return uIndent; }
		void setIndentChar( char cIndent );
		char getIndentChar() const { return cIndent; }

		void setFormat( const Fmt &f )
		{
			fLast = f;
			bTempFmt = false;
		}

		void setTempFormat( const Fmt &f )
		{
			fLast = f;
			bTempFmt = true;
		}

		void usedFormat()
		{
			if( bTempFmt )
				fLast = Fmt();
		}

		template<typename type>
		void ifmt( type i )
		{
			// This code is taken from Nango, hopefully we can make it better.
			bool bNeg = i<0;
			char cBase = fLast.bCaps?'A':'a';
			char buf[sizeof(type)*8+1];
			if( bNeg ) i = -i;
			if( fLast.uRadix < 2 || fLast.uRadix > 36 )
			{
				usedFormat();
				return;
			}

			for( int j = sizeof(type)*8; j >= 0; j-- )
			{
				int c = i%fLast.uRadix;
				i /= fLast.uRadix;
				buf[j] = (char)((c<10)?('0'+c):(cBase+c-10));
				if( i == 0 )
				{
					if( bNeg ) buf[--j] = '-';
					else if( fLast.bPlus ) buf[--j] = '+';
					writeAligned( buf+j, sizeof(type)*8-j+1 );

					return;
				}
			}
			usedFormat();
		}

		template<typename type>
		void ufmt( type i )
		{
			// This code is taken from Nango, hopefully we can make it better.
			char buf[sizeof(type)*8+1];
			char cBase = fLast.bCaps?'A':'a';
			if( fLast.uRadix < 2 || fLast.uRadix > 36 )
			{
				usedFormat();
				return;
			}

			for( int j = sizeof(type)*8; j >= 0; j-- )
			{
				int c = i%fLast.uRadix;
				i /= fLast.uRadix;
				buf[j] = (char)((c<10)?('0'+c):(cBase+c-10));
				if( i == 0 )
				{
					if( fLast.bPlus ) buf[--j] = '+';
					writeAligned( buf+j, sizeof(type)*8-j+1 );

					return;
				}
			}
			usedFormat();
		}

		template<typename type>
		void ffmt( type f )
		{
			Bu::FString fTmp;
			fTmp.format("%f", f );
//			writeAligned("**make floats work**");
			writeAligned( fTmp );
			usedFormat();
		}

		enum Special
		{
			nl,
			flush
		};

		void doFlush()
		{
			rOut.flush();
		}

	private:
		Stream &rOut;
		Fmt fLast;
		bool bTempFmt;
		uint8_t uIndent;
		char cIndent;
	};

	typedef Formatter::Fmt Fmt;

	Formatter &operator<<( Formatter &rOut, const Formatter::Fmt &f );
	Formatter &operator<<( Formatter &rOut, Formatter::Special s );
	Formatter &operator<<( Formatter &rOut, const char *sStr );
	Formatter &operator<<( Formatter &rOut, char *sStr );
	Formatter &operator<<( Formatter &rOut, const Bu::FString &sStr );
	Formatter &operator<<( Formatter &rOut, signed char c );
	Formatter &operator<<( Formatter &rOut, char c );
	Formatter &operator<<( Formatter &rOut, unsigned char c );
	Formatter &operator<<( Formatter &rOut, signed short i );
	Formatter &operator<<( Formatter &rOut, unsigned short i );
	Formatter &operator<<( Formatter &rOut, signed int i );
	Formatter &operator<<( Formatter &rOut, unsigned int i );
	Formatter &operator<<( Formatter &rOut, signed long i );
	Formatter &operator<<( Formatter &rOut, unsigned long i );
	Formatter &operator<<( Formatter &rOut, signed long long i );
	Formatter &operator<<( Formatter &rOut, unsigned long long i );
	Formatter &operator<<( Formatter &rOut, float f );
	Formatter &operator<<( Formatter &rOut, double f );
	Formatter &operator<<( Formatter &rOut, long double f );
	Formatter &operator<<( Formatter &rOut, bool b );

	template<typename type>
	Formatter &operator<<( Formatter &rOut, const type *p )
	{
		return rOut << "0x" << Fmt::hex(sizeof(ptrdiff_t)*2) << (ptrdiff_t)(p);
	}
};

#endif