aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2009-02-11 22:51:25 +0000
committerMike Buland <eichlan@xagasoft.com>2009-02-11 22:51:25 +0000
commit3f958097632256329cdbaf2219e2ba15325e9c52 (patch)
treee7616f7db3709d68ae51d6da0ef8ce44600bd33f /src
parentf4b191f0ea396b58465bfba40749977780a3af58 (diff)
downloadlibbu++-3f958097632256329cdbaf2219e2ba15325e9c52.tar.gz
libbu++-3f958097632256329cdbaf2219e2ba15325e9c52.tar.bz2
libbu++-3f958097632256329cdbaf2219e2ba15325e9c52.tar.xz
libbu++-3f958097632256329cdbaf2219e2ba15325e9c52.zip
Hey, formatter, awesome, and look at that...I'm adding uuid support.
Diffstat (limited to '')
-rw-r--r--src/formatter.cpp76
-rw-r--r--src/formatter.h71
-rw-r--r--src/tests/stdstream.cpp31
-rw-r--r--src/uuid.cpp18
-rw-r--r--src/uuid.h25
5 files changed, 195 insertions, 26 deletions
diff --git a/src/formatter.cpp b/src/formatter.cpp
index c6ae3b7..1ec3c39 100644
--- a/src/formatter.cpp
+++ b/src/formatter.cpp
@@ -33,7 +33,7 @@ void Bu::Formatter::writeAligned( const Bu::FString &sStr )
33 { 33 {
34 case Fmt::Right: 34 case Fmt::Right:
35 for( int k = 0; k < iRem; k++ ) 35 for( int k = 0; k < iRem; k++ )
36 write(" ", 1 ); 36 write( &fLast.cFillChar, 1 );
37 write( sStr ); 37 write( sStr );
38 break; 38 break;
39 39
@@ -41,18 +41,18 @@ void Bu::Formatter::writeAligned( const Bu::FString &sStr )
41 { 41 {
42 int iHlf = iRem/2; 42 int iHlf = iRem/2;
43 for( int k = 0; k < iHlf; k++ ) 43 for( int k = 0; k < iHlf; k++ )
44 write(" ", 1 ); 44 write( &fLast.cFillChar, 1 );
45 write( sStr ); 45 write( sStr );
46 iHlf = iRem-iHlf;; 46 iHlf = iRem-iHlf;;
47 for( int k = 0; k < iHlf; k++ ) 47 for( int k = 0; k < iHlf; k++ )
48 write(" ", 1 ); 48 write( &fLast.cFillChar, 1 );
49 } 49 }
50 break; 50 break;
51 51
52 case Fmt::Left: 52 case Fmt::Left:
53 write( sStr ); 53 write( sStr );
54 for( int k = 0; k < iRem; k++ ) 54 for( int k = 0; k < iRem; k++ )
55 write(" ", 1 ); 55 write( &fLast.cFillChar, 1 );
56 break; 56 break;
57 } 57 }
58 } 58 }
@@ -73,7 +73,7 @@ void Bu::Formatter::writeAligned( const char *sStr, int iLen )
73 { 73 {
74 case Fmt::Right: 74 case Fmt::Right:
75 for( int k = 0; k < iRem; k++ ) 75 for( int k = 0; k < iRem; k++ )
76 write(" ", 1 ); 76 write( &fLast.cFillChar, 1 );
77 write( sStr, iLen ); 77 write( sStr, iLen );
78 break; 78 break;
79 79
@@ -81,18 +81,18 @@ void Bu::Formatter::writeAligned( const char *sStr, int iLen )
81 { 81 {
82 int iHlf = iRem/2; 82 int iHlf = iRem/2;
83 for( int k = 0; k < iHlf; k++ ) 83 for( int k = 0; k < iHlf; k++ )
84 write(" ", 1 ); 84 write( &fLast.cFillChar, 1 );
85 write( sStr, iLen ); 85 write( sStr, iLen );
86 iHlf = iRem-iHlf;; 86 iHlf = iRem-iHlf;;
87 for( int k = 0; k < iHlf; k++ ) 87 for( int k = 0; k < iHlf; k++ )
88 write(" ", 1 ); 88 write( &fLast.cFillChar, 1 );
89 } 89 }
90 break; 90 break;
91 91
92 case Fmt::Left: 92 case Fmt::Left:
93 write( sStr, iLen ); 93 write( sStr, iLen );
94 for( int k = 0; k < iRem; k++ ) 94 for( int k = 0; k < iRem; k++ )
95 write(" ", 1 ); 95 write( &fLast.cFillChar, 1 );
96 break; 96 break;
97 } 97 }
98 } 98 }
@@ -100,6 +100,42 @@ void Bu::Formatter::writeAligned( const char *sStr, int iLen )
100 usedFormat(); 100 usedFormat();
101} 101}
102 102
103Bu::Formatter::Fmt &Bu::Formatter::Fmt::width( unsigned int uWidth )
104{
105 this->uMinWidth = uWidth;
106 return *this;
107}
108
109Bu::Formatter::Fmt &Bu::Formatter::Fmt::fill( char cFill )
110{
111 this->cFillChar = (unsigned char)cFill;
112 return *this;
113}
114
115Bu::Formatter::Fmt &Bu::Formatter::Fmt::radix( unsigned int uRadix )
116{
117 this->uRadix = uRadix;
118 return *this;
119}
120
121Bu::Formatter::Fmt &Bu::Formatter::Fmt::align( Alignment eAlign )
122{
123 this->uAlign = eAlign;
124 return *this;
125}
126
127Bu::Formatter::Fmt &Bu::Formatter::Fmt::plus( bool bPlus )
128{
129 this->bPlus = bPlus;
130 return *this;
131}
132
133Bu::Formatter::Fmt &Bu::Formatter::Fmt::caps( bool bCaps )
134{
135 this->bCaps = bCaps;
136 return *this;
137}
138
103Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, const Bu::Formatter::Fmt &f ) 139Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, const Bu::Formatter::Fmt &f )
104{ 140{
105 rOut.setTempFormat( f ); 141 rOut.setTempFormat( f );
@@ -195,3 +231,27 @@ Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned long long i )
195 return rOut; 231 return rOut;
196} 232}
197 233
234Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, float f )
235{
236 rOut.ffmt<float>( f );
237 return rOut;
238}
239
240Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, double f )
241{
242 rOut.ffmt<double>( f );
243 return rOut;
244}
245
246Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, long double f )
247{
248 rOut.ffmt<long double>( f );
249 return rOut;
250}
251
252Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, bool b )
253{
254 rOut.writeAligned( b?("true"):("false") );
255 return rOut;
256}
257
diff --git a/src/formatter.h b/src/formatter.h
index 168f48f..4bab505 100644
--- a/src/formatter.h
+++ b/src/formatter.h
@@ -21,28 +21,65 @@ namespace Bu
21 }; 21 };
22 Fmt() : 22 Fmt() :
23 uMinWidth( 0 ), 23 uMinWidth( 0 ),
24 cFillChar(' '),
24 uRadix( 10 ), 25 uRadix( 10 ),
25 uAlign( Right ), 26 uAlign( Right ),
26 bPlus( false ), 27 bPlus( false ),
27 bCaps( false ) 28 bCaps( true )
28 { 29 {
29 } 30 }
30 31
31 Fmt( unsigned int uMinWidth, unsigned int uRadix=10, 32 Fmt( unsigned int uMinWidth, unsigned int uRadix=10,
32 Alignment a=Right, bool bPlus=false ) : 33 Alignment a=Right, bool bPlus=false, bool bCaps=true,
34 char cFill=' ') :
33 uMinWidth( uMinWidth ), 35 uMinWidth( uMinWidth ),
36 cFillChar(cFill),
34 uRadix( uRadix ), 37 uRadix( uRadix ),
35 uAlign( a ), 38 uAlign( a ),
36 bPlus( bPlus ), 39 bPlus( bPlus ),
37 bCaps( false ) 40 bCaps( bCaps )
38 { 41 {
39 } 42 }
43 Fmt( unsigned int uMinWidth, Alignment a=Right,
44 unsigned int uRadix=10, bool bPlus=false, bool bCaps=true,
45 char cFill=' ') :
46 uMinWidth( uMinWidth ),
47 cFillChar(cFill),
48 uRadix( uRadix ),
49 uAlign( a ),
50 bPlus( bPlus ),
51 bCaps( bCaps )
52 {
53 }
54
55 static Fmt hex( unsigned int uWidth=0, bool bCaps=true )
56 {
57 return Fmt( uWidth, 16, Right, false, bCaps, '0' );
58 }
59
60 static Fmt oct( unsigned int uWidth=0, bool bCaps=true )
61 {
62 return Fmt( uWidth, 8, Right, false, bCaps, '0' );
63 }
40 64
41 unsigned int uMinWidth : 8; 65 static Fmt ptr( bool bCaps=true )
42 unsigned int uRadix : 6; 66 {
43 unsigned int uAlign : 2; 67 return Fmt( sizeof(ptrdiff_t)*2, 16, Right, false, bCaps, '0' );
44 unsigned int bPlus : 1; 68 }
45 unsigned int bCaps : 1; 69
70 Fmt &width( unsigned int uWidth );
71 Fmt &fill( char cFill='0' );
72 Fmt &radix( unsigned int uRadix );
73 Fmt &align( Alignment eAlign );
74 Fmt &plus( bool bPlus=true );
75 Fmt &caps( bool bCaps=true );
76
77 unsigned char uMinWidth;
78 char cFillChar;
79 unsigned short uRadix : 6;
80 unsigned short uAlign : 2;
81 unsigned short bPlus : 1;
82 unsigned short bCaps : 1;
46 } Fmt; 83 } Fmt;
47 84
48 void write( const Bu::FString &sStr ); 85 void write( const Bu::FString &sStr );
@@ -125,6 +162,13 @@ namespace Bu
125 usedFormat(); 162 usedFormat();
126 } 163 }
127 164
165 template<typename type>
166 void ffmt( type /*f*/ )
167 {
168 writeAligned("**make floats work**");
169 usedFormat();
170 }
171
128 enum Special 172 enum Special
129 { 173 {
130 nl 174 nl
@@ -153,6 +197,17 @@ namespace Bu
153 Formatter &operator<<( Formatter &rOut, unsigned long i ); 197 Formatter &operator<<( Formatter &rOut, unsigned long i );
154 Formatter &operator<<( Formatter &rOut, signed long long i ); 198 Formatter &operator<<( Formatter &rOut, signed long long i );
155 Formatter &operator<<( Formatter &rOut, unsigned long long i ); 199 Formatter &operator<<( Formatter &rOut, unsigned long long i );
200 Formatter &operator<<( Formatter &rOut, float f );
201 Formatter &operator<<( Formatter &rOut, double f );
202 Formatter &operator<<( Formatter &rOut, long double f );
203 Formatter &operator<<( Formatter &rOut, bool b );
204
205 template<typename type>
206 Formatter &operator<<( Formatter &rOut, type *p )
207 {
208 rOut << (ptrdiff_t)(p);
209 return rOut;
210 }
156}; 211};
157 212
158#endif 213#endif
diff --git a/src/tests/stdstream.cpp b/src/tests/stdstream.cpp
index d67f9b8..cb22e22 100644
--- a/src/tests/stdstream.cpp
+++ b/src/tests/stdstream.cpp
@@ -1,20 +1,31 @@
1#include "bu/sio.h" 1#include "bu/sio.h"
2 2
3using Bu::sio;
4using Bu::Fmt;
5
3int main() 6int main()
4{ 7{
5 Bu::sio << "Hello there" << Bu::sio.nl; 8 sio << "Hello there" << sio.nl;
9
10 sio << "sizeof(Fmt) = " << sizeof(Fmt) << sio.nl;
11
12 sio << -123 << ", " << 0 << ", " << 123 << sio.nl;
13
14 sio << "+----------+" << sio.nl;
15 sio << "|" << Fmt( 10, 10, Fmt::Center ) << "Test" << "|" << sio.nl;
16 sio << "+----------+" << sio.nl;
17 sio << "|" << Fmt( 10, 10, Fmt::Left ) << 123 << "|" << sio.nl;
18 sio << "|" << Fmt( 10, 10, Fmt::Center ) << 123 << "|" << sio.nl;
19 sio << "|" << Fmt( 10, 10, Fmt::Right ) << 123 << "|" << sio.nl;
20 sio << "+----------+" << sio.nl;
6 21
7 Bu::sio << "sizeof(Fmt) = " << sizeof(Bu::Fmt) << Bu::sio.nl; 22 sio << Fmt(10,Fmt::Left) << "Hexcode:" << Fmt::ptr() << (&sio) << sio.nl;
8 23
9 Bu::sio << -123 << ", " << 0 << ", " << 123 << Bu::sio.nl; 24 sio << 0.123 << sio.nl;
25 sio << true << " and then " << false << sio.nl;
10 26
11 Bu::sio << "+----------+" << Bu::sio.nl; 27 //for( int j = 2; j <= 36; j++ )
12 Bu::sio << "|" << Bu::Fmt( 10, 10, Bu::Fmt::Center ) << "Test" << "|" << Bu::sio.nl; 28 // sio << "radix(" << j << ") = " << Fmt().radix( j ) << 255 << sio.nl;
13 Bu::sio << "+----------+" << Bu::sio.nl;
14 Bu::sio << "|" << Bu::Fmt( 10, 10, Bu::Fmt::Left ) << 123 << "|" << Bu::sio.nl;
15 Bu::sio << "|" << Bu::Fmt( 10, 10, Bu::Fmt::Center ) << 123 << "|" << Bu::sio.nl;
16 Bu::sio << "|" << Bu::Fmt( 10, 10, Bu::Fmt::Right ) << 123 << "|" << Bu::sio.nl;
17 Bu::sio << "+----------+" << Bu::sio.nl;
18 29
19 return 0; 30 return 0;
20} 31}
diff --git a/src/uuid.cpp b/src/uuid.cpp
new file mode 100644
index 0000000..8a30e2a
--- /dev/null
+++ b/src/uuid.cpp
@@ -0,0 +1,18 @@
1#include "bu/uuid.h"
2
3Bu::Uuid::Uuid()
4{
5 clear();
6}
7
8Bu::Uuid::~Uuid()
9{
10}
11
12#define msb( i ) (1<<(7-i))
13
14void Bu::Uuid::clear()
15{
16 data[7] = msb(0);
17}
18
diff --git a/src/uuid.h b/src/uuid.h
new file mode 100644
index 0000000..2bb1404
--- /dev/null
+++ b/src/uuid.h
@@ -0,0 +1,25 @@
1#ifndef BU_UUID_H
2#define BU_UUID_H
3
4namespace Bu
5{
6 class Uuid
7 {
8 public:
9 Uuid();
10 virtual ~Uuid();
11
12 static Uuid genV1();
13 static Uuid genV2();
14 static Uuid genV3();
15 static Uuid genV4();
16 static Uuid genV5();
17
18 void clear();
19
20 private:
21 unsigned char data[8];
22 };
23};
24
25#endif