summaryrefslogtreecommitdiff
path: root/src/tools/format.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-03-18 03:48:35 +0000
committerMike Buland <eichlan@xagasoft.com>2011-03-18 03:48:35 +0000
commitfcf2dde54316a3ac35936157babccae8c8d8d90b (patch)
tree050ac9a1d601296225e5aaa0ca88f531fa0131a1 /src/tools/format.cpp
parent30e841d5b6f082bce1f98c6df198aeea28a2d95b (diff)
downloadlibbu++-fcf2dde54316a3ac35936157babccae8c8d8d90b.tar.gz
libbu++-fcf2dde54316a3ac35936157babccae8c8d8d90b.tar.bz2
libbu++-fcf2dde54316a3ac35936157babccae8c8d8d90b.tar.xz
libbu++-fcf2dde54316a3ac35936157babccae8c8d8d90b.zip
The inline, printf/qstring style formatting for Bu::String is just about there,
it just needs to be integrated with the Bu::String class itself, pretty exciting.
Diffstat (limited to 'src/tools/format.cpp')
-rw-r--r--src/tools/format.cpp44
1 files changed, 40 insertions, 4 deletions
diff --git a/src/tools/format.cpp b/src/tools/format.cpp
index 6a2f3bd..6aa4c56 100644
--- a/src/tools/format.cpp
+++ b/src/tools/format.cpp
@@ -4,6 +4,8 @@
4#include <bu/variant.h> 4#include <bu/variant.h>
5#include <bu/membuf.h> 5#include <bu/membuf.h>
6 6
7#include <stdlib.h>
8
7using namespace Bu; 9using namespace Bu;
8 10
9class Fmter 11class Fmter
@@ -32,15 +34,47 @@ public:
32 34
33 operator Bu::String() const 35 operator Bu::String() const
34 { 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 }
35 Bu::MemBuf mbOut; 47 Bu::MemBuf mbOut;
36 Bu::Formatter f( mbOut ); 48 Bu::Formatter f( mbOut );
37 ParmList::const_iterator i = lParm.begin();
38 for( Bu::String::const_iterator s = sSrc.begin(); s; s++ ) 49 for( Bu::String::const_iterator s = sSrc.begin(); s; s++ )
39 { 50 {
40 if( *s == '%' ) 51 if( *s == '%' )
41 { 52 {
42 f << (*i).format << (*i).value; 53 s++;
43 i++; 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 }
44 } 78 }
45 else 79 else
46 { 80 {
@@ -48,6 +82,7 @@ public:
48 } 82 }
49 } 83 }
50 84
85 delete[] aParm;
51 return mbOut.getString(); 86 return mbOut.getString();
52 } 87 }
53 88
@@ -83,6 +118,7 @@ Bu::Formatter &operator<<( Bu::Formatter &f, const Fmter &r )
83 118
84int main() 119int main()
85{ 120{
86 sio << Fmter("A word is % and a number is % %.").arg("Hello").arg(75, Fmt::hex() ).arg(" - ") << sio.nl; 121 sio << Fmter("A word is %1 and a number is %2 %1").
122 arg("Hello").arg(75, Fmt::hex() ).arg(" - ") << sio.nl;
87} 123}
88 124