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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
/*
* Copyright (C) 2007-2011 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 &rStream );
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 ),
bTokenize( 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 ),
bTokenize( true )
{
}
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 ),
bTokenize( true )
{
}
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 &tokenize( bool bTokenize=true );
Fmt &left();
Fmt &right();
Fmt ¢er();
unsigned char uMinWidth;
char cFillChar;
unsigned short uRadix : 6;
unsigned short uAlign : 2;
unsigned short bPlus : 1;
unsigned short bCaps : 1;
unsigned short bTokenize : 1;
} Fmt;
void write( const Bu::String &sStr );
void write( const void *sStr, int iLen );
void writeAligned( const Bu::String &sStr );
void writeAligned( const char *sStr, int iLen );
void read( void *sStr, int iLen );
Bu::String readToken();
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::String fTmp;
fTmp.format("%f", f );
// writeAligned("**make floats work**");
writeAligned( fTmp );
usedFormat();
}
template<typename type>
void iparse( type &i, const Bu::String &sBuf )
{
if( !sBuf.isSet() )
return;
if( sBuf[0] != '+' && sBuf[0] != '-' &&
(sBuf[0] < '0' && sBuf[0] > '9') )
return;
int j = 1;
int iMax = sBuf.getSize();
for(; j < iMax && (sBuf[j] >= '0' && sBuf[j] <= '9'); j++ ) { }
i = 0;
type iPos = 1;
for(j--; j >= 0; j-- )
{
if( sBuf[j] == '+' || sBuf[j] == '-' )
continue;
i += (sBuf[j]-'0')*iPos;
iPos *= fLast.uRadix;
}
if( sBuf[0] == '-' )
i = -i;
usedFormat();
}
template<typename type>
void uparse( type &i, const Bu::String &sBuf )
{
if( !sBuf.isSet() )
return;
if( sBuf[0] != '+' &&
(sBuf[0] < '0' && sBuf[0] > '9') )
return;
int j = 1;
int iMax = sBuf.getSize();
for(; j < iMax && (sBuf[j] >= '0' && sBuf[j] <= '9'); j++ ) { }
i = 0;
type iPos = 1;
for(j--; j >= 0; j-- )
{
if( sBuf[j] == '+' )
continue;
i += (sBuf[j]-'0')*iPos;
iPos *= fLast.uRadix;
}
usedFormat();
}
template<typename type>
void fparse( type &f, const Bu::String &sBuf )
{
sscanf( sBuf.getStr(), "%f", &f );
usedFormat();
}
enum Special
{
nl,
flush
};
void doFlush()
{
rStream.flush();
}
private:
Stream &rStream;
Fmt fLast;
bool bTempFmt;
uint8_t uIndent;
char cIndent;
};
typedef Formatter::Fmt Fmt;
Formatter &operator<<( Formatter &f, const Formatter::Fmt &fmt );
Formatter &operator<<( Formatter &f, Formatter::Special s );
Formatter &operator<<( Formatter &f, const char *sStr );
Formatter &operator<<( Formatter &f, char *sStr );
Formatter &operator<<( Formatter &f, const Bu::String &sStr );
Formatter &operator<<( Formatter &f, signed char c );
Formatter &operator<<( Formatter &f, char c );
Formatter &operator<<( Formatter &f, unsigned char c );
Formatter &operator<<( Formatter &f, signed short i );
Formatter &operator<<( Formatter &f, unsigned short i );
Formatter &operator<<( Formatter &f, signed int i );
Formatter &operator<<( Formatter &f, unsigned int i );
Formatter &operator<<( Formatter &f, signed long i );
Formatter &operator<<( Formatter &f, unsigned long i );
Formatter &operator<<( Formatter &f, signed long long i );
Formatter &operator<<( Formatter &f, unsigned long long i );
Formatter &operator<<( Formatter &f, float flt );
Formatter &operator<<( Formatter &f, double flt );
Formatter &operator<<( Formatter &f, long double flt );
Formatter &operator<<( Formatter &f, bool b );
Formatter &operator>>( Formatter &f, Bu::String &sStr );
Formatter &operator>>( Formatter &f, signed char &c );
Formatter &operator>>( Formatter &f, char &c );
Formatter &operator>>( Formatter &f, unsigned char &c );
Formatter &operator>>( Formatter &f, signed short &i );
Formatter &operator>>( Formatter &f, unsigned short &i );
Formatter &operator>>( Formatter &f, signed int &i );
Formatter &operator>>( Formatter &f, unsigned int &i );
Formatter &operator>>( Formatter &f, signed long &i );
Formatter &operator>>( Formatter &f, unsigned long &i );
Formatter &operator>>( Formatter &f, signed long long &i );
Formatter &operator>>( Formatter &f, unsigned long long &i );
Formatter &operator>>( Formatter &f, float &flt );
Formatter &operator>>( Formatter &f, double &flt );
Formatter &operator>>( Formatter &f, long double &flt );
Formatter &operator>>( Formatter &f, bool &b );
template<typename type>
Formatter &operator<<( Formatter &f, const type *p )
{
return f << "0x" << Fmt::hex(sizeof(ptrdiff_t)*2) << (ptrdiff_t)(p);
}
};
#endif
|