diff options
Diffstat (limited to '')
-rw-r--r-- | src/formatter.cpp | 197 |
1 files changed, 197 insertions, 0 deletions
diff --git a/src/formatter.cpp b/src/formatter.cpp new file mode 100644 index 0000000..c6ae3b7 --- /dev/null +++ b/src/formatter.cpp | |||
@@ -0,0 +1,197 @@ | |||
1 | #include "formatter.h" | ||
2 | |||
3 | Bu::Formatter::Formatter( Stream &rOut ) : | ||
4 | rOut( rOut ) | ||
5 | { | ||
6 | } | ||
7 | |||
8 | Bu::Formatter::~Formatter() | ||
9 | { | ||
10 | } | ||
11 | |||
12 | void Bu::Formatter::write( const Bu::FString &sStr ) | ||
13 | { | ||
14 | rOut.write( sStr ); | ||
15 | } | ||
16 | |||
17 | void Bu::Formatter::write( const char *sStr, int iLen ) | ||
18 | { | ||
19 | rOut.write( sStr, iLen ); | ||
20 | } | ||
21 | |||
22 | void Bu::Formatter::writeAligned( const Bu::FString &sStr ) | ||
23 | { | ||
24 | int iLen = sStr.getSize(); | ||
25 | if( iLen > fLast.uMinWidth ) | ||
26 | { | ||
27 | write( sStr ); | ||
28 | } | ||
29 | else | ||
30 | { | ||
31 | int iRem = fLast.uMinWidth - iLen; | ||
32 | switch( fLast.uAlign ) | ||
33 | { | ||
34 | case Fmt::Right: | ||
35 | for( int k = 0; k < iRem; k++ ) | ||
36 | write(" ", 1 ); | ||
37 | write( sStr ); | ||
38 | break; | ||
39 | |||
40 | case Fmt::Center: | ||
41 | { | ||
42 | int iHlf = iRem/2; | ||
43 | for( int k = 0; k < iHlf; k++ ) | ||
44 | write(" ", 1 ); | ||
45 | write( sStr ); | ||
46 | iHlf = iRem-iHlf;; | ||
47 | for( int k = 0; k < iHlf; k++ ) | ||
48 | write(" ", 1 ); | ||
49 | } | ||
50 | break; | ||
51 | |||
52 | case Fmt::Left: | ||
53 | write( sStr ); | ||
54 | for( int k = 0; k < iRem; k++ ) | ||
55 | write(" ", 1 ); | ||
56 | break; | ||
57 | } | ||
58 | } | ||
59 | |||
60 | usedFormat(); | ||
61 | } | ||
62 | |||
63 | void Bu::Formatter::writeAligned( const char *sStr, int iLen ) | ||
64 | { | ||
65 | if( iLen > fLast.uMinWidth ) | ||
66 | { | ||
67 | write( sStr, iLen ); | ||
68 | } | ||
69 | else | ||
70 | { | ||
71 | int iRem = fLast.uMinWidth - iLen; | ||
72 | switch( fLast.uAlign ) | ||
73 | { | ||
74 | case Fmt::Right: | ||
75 | for( int k = 0; k < iRem; k++ ) | ||
76 | write(" ", 1 ); | ||
77 | write( sStr, iLen ); | ||
78 | break; | ||
79 | |||
80 | case Fmt::Center: | ||
81 | { | ||
82 | int iHlf = iRem/2; | ||
83 | for( int k = 0; k < iHlf; k++ ) | ||
84 | write(" ", 1 ); | ||
85 | write( sStr, iLen ); | ||
86 | iHlf = iRem-iHlf;; | ||
87 | for( int k = 0; k < iHlf; k++ ) | ||
88 | write(" ", 1 ); | ||
89 | } | ||
90 | break; | ||
91 | |||
92 | case Fmt::Left: | ||
93 | write( sStr, iLen ); | ||
94 | for( int k = 0; k < iRem; k++ ) | ||
95 | write(" ", 1 ); | ||
96 | break; | ||
97 | } | ||
98 | } | ||
99 | |||
100 | usedFormat(); | ||
101 | } | ||
102 | |||
103 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, const Bu::Formatter::Fmt &f ) | ||
104 | { | ||
105 | rOut.setTempFormat( f ); | ||
106 | return rOut; | ||
107 | } | ||
108 | |||
109 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, Bu::Formatter::Special s ) | ||
110 | { | ||
111 | switch( s ) | ||
112 | { | ||
113 | case Formatter::nl: | ||
114 | rOut.write("\n", 1 ); | ||
115 | break; | ||
116 | } | ||
117 | return rOut; | ||
118 | } | ||
119 | |||
120 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, const char *sStr ) | ||
121 | { | ||
122 | rOut.writeAligned( sStr, strlen( sStr ) ); | ||
123 | return rOut; | ||
124 | } | ||
125 | |||
126 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, const Bu::FString &sStr ) | ||
127 | { | ||
128 | rOut.writeAligned( sStr ); | ||
129 | return rOut; | ||
130 | } | ||
131 | |||
132 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed char c ) | ||
133 | { | ||
134 | rOut.write( (char *)&c, 1 ); | ||
135 | return rOut; | ||
136 | } | ||
137 | |||
138 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, char c ) | ||
139 | { | ||
140 | rOut.write( (char *)&c, 1 ); | ||
141 | return rOut; | ||
142 | } | ||
143 | |||
144 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned char c ) | ||
145 | { | ||
146 | rOut.write( (char *)&c, 1 ); | ||
147 | return rOut; | ||
148 | } | ||
149 | |||
150 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed short i ) | ||
151 | { | ||
152 | rOut.ifmt<signed short>( i ); | ||
153 | return rOut; | ||
154 | } | ||
155 | |||
156 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned short i ) | ||
157 | { | ||
158 | rOut.ufmt<unsigned short>( i ); | ||
159 | return rOut; | ||
160 | } | ||
161 | |||
162 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed int i ) | ||
163 | { | ||
164 | rOut.ifmt<signed int>( i ); | ||
165 | return rOut; | ||
166 | } | ||
167 | |||
168 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned int i ) | ||
169 | { | ||
170 | rOut.ufmt<unsigned int>( i ); | ||
171 | return rOut; | ||
172 | } | ||
173 | |||
174 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed long i ) | ||
175 | { | ||
176 | rOut.ifmt<signed long>( i ); | ||
177 | return rOut; | ||
178 | } | ||
179 | |||
180 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned long i ) | ||
181 | { | ||
182 | rOut.ufmt<unsigned long>( i ); | ||
183 | return rOut; | ||
184 | } | ||
185 | |||
186 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed long long i ) | ||
187 | { | ||
188 | rOut.ifmt<signed long long>( i ); | ||
189 | return rOut; | ||
190 | } | ||
191 | |||
192 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned long long i ) | ||
193 | { | ||
194 | rOut.ufmt<unsigned long long>( i ); | ||
195 | return rOut; | ||
196 | } | ||
197 | |||