aboutsummaryrefslogtreecommitdiff
path: root/src/formatter.h
blob: 168f48fc094cc77aaf72207f09d5f30ffd31ff2f (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
#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 ),
				uRadix( 10 ),
				uAlign( Right ),
				bPlus( false ),
				bCaps( false )
			{
			}

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

			unsigned int uMinWidth : 8;
			unsigned int uRadix : 6;
			unsigned int uAlign : 2;
			unsigned int bPlus : 1;
			unsigned int bCaps : 1;
		} Fmt;

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

		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 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):('A'+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];
			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):('A'+c-10));
				if( i == 0 )
				{
					if( fLast.bPlus ) buf[--j] = '+';
					writeAligned( buf+j, sizeof(type)*8-j+1 );

					return;
				}
			}
			usedFormat();
		}

		enum Special
		{
			nl
		};

	private:
		Stream &rOut;
		Fmt fLast;
		bool bTempFmt;
	};

	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, 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 );
};

#endif