summaryrefslogtreecommitdiff
path: root/src/tools
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-03-18 23:02:54 +0000
committerMike Buland <eichlan@xagasoft.com>2011-03-18 23:02:54 +0000
commitcba6293cf22e2c2ae17dd3954ad7d097f379c7ac (patch)
treedcdbda1f54d3d39a0066f2f42c7e1714e1ceb834 /src/tools
parentfcf2dde54316a3ac35936157babccae8c8d8d90b (diff)
downloadlibbu++-cba6293cf22e2c2ae17dd3954ad7d097f379c7ac.tar.gz
libbu++-cba6293cf22e2c2ae17dd3954ad7d097f379c7ac.tar.bz2
libbu++-cba6293cf22e2c2ae17dd3954ad7d097f379c7ac.tar.xz
libbu++-cba6293cf22e2c2ae17dd3954ad7d097f379c7ac.zip
Wow, a lot has changed. String is not a template class, and it can do it's own
formatting ala QString.
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/format.cpp124
1 files changed, 0 insertions, 124 deletions
diff --git a/src/tools/format.cpp b/src/tools/format.cpp
deleted file mode 100644
index 6aa4c56..0000000
--- a/src/tools/format.cpp
+++ /dev/null
@@ -1,124 +0,0 @@
1#include <bu/string.h>
2#include <bu/formatter.h>
3#include <bu/sio.h>
4#include <bu/variant.h>
5#include <bu/membuf.h>
6
7#include <stdlib.h>
8
9using namespace Bu;
10
11class Fmter
12{
13public:
14 Fmter( const Bu::String &sSrc ) :
15 sSrc( sSrc )
16 {
17 }
18
19 template<typename T>
20 Fmter &arg( const T &x )
21 {
22 lParm.append( Pair( x ) );
23
24 return *this;
25 }
26
27 template<typename T>
28 Fmter &arg( const T &x, Bu::Formatter::Fmt f )
29 {
30 lParm.append( Pair( x, f ) );
31
32 return *this;
33 }
34
35 operator Bu::String() const
36 {
37 int iCount = lParm.getSize();
38 ParmList::const_iterator *aParm =
39 new ParmList::const_iterator[iCount];
40 {
41 int j = 0;
42 for( ParmList::const_iterator i = lParm.begin(); i; i++, j++ )
43 {
44 aParm[j] = i;
45 }
46 }
47 Bu::MemBuf mbOut;
48 Bu::Formatter f( mbOut );
49 for( Bu::String::const_iterator s = sSrc.begin(); s; s++ )
50 {
51 if( *s == '%' )
52 {
53 s++;
54 if( *s == '%' )
55 f << *s;
56 else
57 {
58 Bu::String sNum;
59 while( s && *s >= '0' && *s <= '9' )
60 {
61 sNum += *s;
62 s++;
63 }
64 int iIndex = strtol( sNum.getStr(), 0, 10 )-1;
65 if( iIndex < 0 || iIndex >= iCount )
66 {
67 delete[] aParm;
68 throw Bu::ExceptionBase(
69 "Argument index %d is outside of "
70 "valid range (1-%d).", iIndex+1, iCount
71 );
72 }
73
74 f << (*aParm[iIndex]).format << (*aParm[iIndex]).value;
75 if( s )
76 f << *s;
77 }
78 }
79 else
80 {
81 f << *s;
82 }
83 }
84
85 delete[] aParm;
86 return mbOut.getString();
87 }
88
89private:
90 const Bu::String &sSrc;
91 class Pair
92 {
93 public:
94 template<typename T>
95 Pair( const T &v ) :
96 value( v )
97 {
98 }
99
100 template<typename T>
101 Pair( const T &v, Bu::Formatter::Fmt f ) :
102 value( v ),
103 format( f )
104 {
105 }
106
107 Bu::Variant value;
108 Bu::Formatter::Fmt format;
109 };
110 typedef Bu::List<Pair> ParmList;
111 ParmList lParm;
112};
113
114Bu::Formatter &operator<<( Bu::Formatter &f, const Fmter &r )
115{
116 return f << (Bu::String)r;
117}
118
119int main()
120{
121 sio << Fmter("A word is %1 and a number is %2 %1").
122 arg("Hello").arg(75, Fmt::hex() ).arg(" - ") << sio.nl;
123}
124