aboutsummaryrefslogtreecommitdiff
path: root/src/tools/format.cpp
blob: 6aa4c56dd7182a8da14b17cbccee41b124238217 (plain)
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
#include <bu/string.h>
#include <bu/formatter.h>
#include <bu/sio.h>
#include <bu/variant.h>
#include <bu/membuf.h>

#include <stdlib.h>

using namespace Bu;

class Fmter
{
public:
	Fmter( const Bu::String &sSrc ) :
		sSrc( sSrc )
	{
	}

	template<typename T>
	Fmter &arg( const T &x )
	{
		lParm.append( Pair( x ) );

		return *this;
	}

	template<typename T>
	Fmter &arg( const T &x, Bu::Formatter::Fmt f )
	{
		lParm.append( Pair( x, f ) );

		return *this;
	}

	operator Bu::String() const
	{
		int iCount = lParm.getSize();
		ParmList::const_iterator *aParm =
			new ParmList::const_iterator[iCount];
		{
			int j = 0;
			for( ParmList::const_iterator i = lParm.begin(); i; i++, j++ )
			{
				aParm[j] = i;
			}
		}
		Bu::MemBuf mbOut;
		Bu::Formatter f( mbOut );
		for( Bu::String::const_iterator s = sSrc.begin(); s; s++ )
		{
			if( *s == '%' )
			{
				s++;
				if( *s == '%' )
					f << *s;
				else
				{
					Bu::String sNum;
					while( s && *s >= '0' && *s <= '9' )
					{
						sNum += *s;
						s++;
					}
					int iIndex = strtol( sNum.getStr(), 0, 10 )-1;
					if( iIndex < 0 || iIndex >= iCount )
					{
						delete[] aParm;
						throw Bu::ExceptionBase(
							"Argument index %d is outside of "
							"valid range (1-%d).", iIndex+1, iCount
							);
					}

					f << (*aParm[iIndex]).format << (*aParm[iIndex]).value;
					if( s )
						f << *s;
				}
			}
			else
			{
				f << *s;
			}
		}

		delete[] aParm;
		return mbOut.getString();
	}

private:
	const Bu::String &sSrc;
	class Pair
	{
	public:
		template<typename T>
		Pair( const T &v ) :
			value( v )
		{
		}

		template<typename T>
		Pair( const T &v, Bu::Formatter::Fmt f ) :
			value( v ),
			format( f )
		{
		}

		Bu::Variant value;
		Bu::Formatter::Fmt format;
	};
	typedef Bu::List<Pair> ParmList;
	ParmList lParm;
};

Bu::Formatter &operator<<( Bu::Formatter &f, const Fmter &r )
{
	return f << (Bu::String)r;
}

int main()
{
	sio << Fmter("A word is %1 and a number is %2 %1").
		arg("Hello").arg(75, Fmt::hex() ).arg(" - ") << sio.nl;
}