aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-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