aboutsummaryrefslogtreecommitdiff
path: root/src/stable/formatter.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/stable/formatter.h572
1 files changed, 286 insertions, 286 deletions
diff --git a/src/stable/formatter.h b/src/stable/formatter.h
index d92a53b..94794b5 100644
--- a/src/stable/formatter.h
+++ b/src/stable/formatter.h
@@ -15,292 +15,292 @@
15 15
16namespace Bu 16namespace Bu
17{ 17{
18 class Stream; 18 class Stream;
19 19
20 template<typename t> t tlog( t x ); 20 template<typename t> t tlog( t x );
21 template<> float tlog( float x ); 21 template<> float tlog( float x );
22 template<> double tlog( double x ); 22 template<> double tlog( double x );
23 template<> long double tlog( long double x ); 23 template<> long double tlog( long double x );
24 24
25 template<typename t> t tfloor( t x ); 25 template<typename t> t tfloor( t x );
26 template<> float tfloor( float x ); 26 template<> float tfloor( float x );
27 template<> double tfloor( double x ); 27 template<> double tfloor( double x );
28 template<> long double tfloor( long double x ); 28 template<> long double tfloor( long double x );
29 29
30 template<typename t> t tpow( t x, t y ); 30 template<typename t> t tpow( t x, t y );
31 template<> float tpow( float x, float y ); 31 template<> float tpow( float x, float y );
32 template<> double tpow( double x, double y ); 32 template<> double tpow( double x, double y );
33 template<> long double tpow( long double x, long double y ); 33 template<> long double tpow( long double x, long double y );
34 34
35 class Formatter 35 class Formatter
36 { 36 {
37 public: 37 public:
38 Formatter( Stream &rStream ); 38 Formatter( Stream &rStream );
39 virtual ~Formatter(); 39 virtual ~Formatter();
40 40
41 void write( const Bu::String &sStr ); 41 void write( const Bu::String &sStr );
42 void write( const void *sStr, int iLen ); 42 void write( const void *sStr, int iLen );
43 void writeAligned( const Bu::String &sStr ); 43 void writeAligned( const Bu::String &sStr );
44 void writeAligned( const char *sStr, int iLen ); 44 void writeAligned( const char *sStr, int iLen );
45 45
46 void read( void *sStr, int iLen ); 46 void read( void *sStr, int iLen );
47 Bu::String readToken(); 47 Bu::String readToken();
48 48
49 void incIndent(); 49 void incIndent();
50 void decIndent(); 50 void decIndent();
51 void setIndent( uint8_t uLevel ); 51 void setIndent( uint8_t uLevel );
52 void clearIndent(); 52 void clearIndent();
53 uint8_t getIndent() const { return uIndent; } 53 uint8_t getIndent() const { return uIndent; }
54 void setIndentChar( char cIndent ); 54 void setIndentChar( char cIndent );
55 char getIndentChar() const { return cIndent; } 55 char getIndentChar() const { return cIndent; }
56 56
57 void setFormat( const Fmt &f ) 57 void setFormat( const Fmt &f )
58 { 58 {
59 fLast = f; 59 fLast = f;
60 bTempFmt = false; 60 bTempFmt = false;
61 } 61 }
62 62
63 void setTempFormat( const Fmt &f ) 63 void setTempFormat( const Fmt &f )
64 { 64 {
65 fLast = f; 65 fLast = f;
66 bTempFmt = true; 66 bTempFmt = true;
67 } 67 }
68 68
69 void usedFormat() 69 void usedFormat()
70 { 70 {
71 if( bTempFmt ) 71 if( bTempFmt )
72 fLast = Fmt(); 72 fLast = Fmt();
73 } 73 }
74 74
75 template<typename type> 75 template<typename type>
76 void ifmt( type i ) 76 void ifmt( type i )
77 { 77 {
78 // This code is taken from Nango, hopefully we can make it better. 78 // This code is taken from Nango, hopefully we can make it better.
79 bool bNeg = i<0; 79 bool bNeg = i<0;
80 char cBase = fLast.bCaps?'A':'a'; 80 char cBase = fLast.bCaps?'A':'a';
81 char buf[sizeof(type)*8+1]; 81 char buf[sizeof(type)*8+1];
82 if( bNeg ) i = -i; 82 if( bNeg ) i = -i;
83 if( fLast.uRadix < 2 || fLast.uRadix > 36 ) 83 if( fLast.uRadix < 2 || fLast.uRadix > 36 )
84 { 84 {
85 usedFormat(); 85 usedFormat();
86 return; 86 return;
87 } 87 }
88 88
89 for( int j = sizeof(type)*8; j >= 0; j-- ) 89 for( int j = sizeof(type)*8; j >= 0; j-- )
90 { 90 {
91 int c = i%fLast.uRadix; 91 int c = i%fLast.uRadix;
92 i /= fLast.uRadix; 92 i /= fLast.uRadix;
93 buf[j] = (char)((c<10)?('0'+c):(cBase+c-10)); 93 buf[j] = (char)((c<10)?('0'+c):(cBase+c-10));
94 if( i == 0 ) 94 if( i == 0 )
95 { 95 {
96 if( bNeg ) buf[--j] = '-'; 96 if( bNeg ) buf[--j] = '-';
97 else if( fLast.bPlus ) buf[--j] = '+'; 97 else if( fLast.bPlus ) buf[--j] = '+';
98 writeAligned( buf+j, sizeof(type)*8-j+1 ); 98 writeAligned( buf+j, sizeof(type)*8-j+1 );
99 99
100 return; 100 return;
101 } 101 }
102 } 102 }
103 usedFormat(); 103 usedFormat();
104 } 104 }
105 105
106 template<typename type> 106 template<typename type>
107 void ufmt( type i ) 107 void ufmt( type i )
108 { 108 {
109 // This code is taken from Nango, hopefully we can make it better. 109 // This code is taken from Nango, hopefully we can make it better.
110 char buf[sizeof(type)*8+1]; 110 char buf[sizeof(type)*8+1];
111 char cBase = fLast.bCaps?'A':'a'; 111 char cBase = fLast.bCaps?'A':'a';
112 if( fLast.uRadix < 2 || fLast.uRadix > 36 ) 112 if( fLast.uRadix < 2 || fLast.uRadix > 36 )
113 { 113 {
114 usedFormat(); 114 usedFormat();
115 return; 115 return;
116 } 116 }
117 117
118 for( int j = sizeof(type)*8; j >= 0; j-- ) 118 for( int j = sizeof(type)*8; j >= 0; j-- )
119 { 119 {
120 int c = i%fLast.uRadix; 120 int c = i%fLast.uRadix;
121 i /= fLast.uRadix; 121 i /= fLast.uRadix;
122 buf[j] = (char)((c<10)?('0'+c):(cBase+c-10)); 122 buf[j] = (char)((c<10)?('0'+c):(cBase+c-10));
123 if( i == 0 ) 123 if( i == 0 )
124 { 124 {
125 if( fLast.bPlus ) buf[--j] = '+'; 125 if( fLast.bPlus ) buf[--j] = '+';
126 writeAligned( buf+j, sizeof(type)*8-j+1 ); 126 writeAligned( buf+j, sizeof(type)*8-j+1 );
127 127
128 return; 128 return;
129 } 129 }
130 } 130 }
131 usedFormat(); 131 usedFormat();
132 } 132 }
133 133
134 template<typename type> 134 template<typename type>
135 void ffmt( type f ) 135 void ffmt( type f )
136 { 136 {
137 Bu::String fTmp; 137 Bu::String fTmp;
138 char cBase = fLast.bCaps?'A':'a'; 138 char cBase = fLast.bCaps?'A':'a';
139 if( fLast.uRadix < 2 || fLast.uRadix > 36 ) 139 if( fLast.uRadix < 2 || fLast.uRadix > 36 )
140 { 140 {
141 usedFormat(); 141 usedFormat();
142 return; 142 return;
143 } 143 }
144 144
145 if( signbit(f) ) 145 if( signbit(f) )
146 { 146 {
147 f = -f; 147 f = -f;
148 fTmp += "-"; 148 fTmp += "-";
149 } 149 }
150 int iScale = tfloor(tlog( f ) / tlog( (type)fLast.uRadix )); 150 int iScale = tfloor(tlog( f ) / tlog( (type)fLast.uRadix ));
151 f /= tpow( (type)fLast.uRadix, (type)iScale ); 151 f /= tpow( (type)fLast.uRadix, (type)iScale );
152 152
153 if( iScale < 0 ) 153 if( iScale < 0 )
154 { 154 {
155 fTmp += "0."; 155 fTmp += "0.";
156 for( int j = 1; j < -iScale; j++ ) 156 for( int j = 1; j < -iScale; j++ )
157 fTmp += '0'; 157 fTmp += '0';
158 } 158 }
159 int c = f; 159 int c = f;
160 fTmp += (char)((c<10)?('0'+c):(cBase+c-10)); 160 fTmp += (char)((c<10)?('0'+c):(cBase+c-10));
161 f -= (int)f; 161 f -= (int)f;
162 int j; 162 int j;
163 for( j = 0; j < 8 && f; j++ ) 163 for( j = 0; j < 8 && f; j++ )
164 { 164 {
165 if( iScale - j == 0 ) 165 if( iScale - j == 0 )
166 fTmp += '.'; 166 fTmp += '.';
167 f = f*fLast.uRadix; 167 f = f*fLast.uRadix;
168 int c = f; 168 int c = f;
169 fTmp += (char)((c<10)?('0'+c):(cBase+c-10)); 169 fTmp += (char)((c<10)?('0'+c):(cBase+c-10));
170 f -= (int)f; 170 f -= (int)f;
171 } 171 }
172 if( iScale >= j ) 172 if( iScale >= j )
173 { 173 {
174 for( int k = j; k < iScale; k++ ) 174 for( int k = j; k < iScale; k++ )
175 fTmp += '0'; 175 fTmp += '0';
176 fTmp += ".0"; 176 fTmp += ".0";
177 } 177 }
178 178
179 writeAligned( fTmp ); 179 writeAligned( fTmp );
180 usedFormat(); 180 usedFormat();
181 } 181 }
182 182
183 template<typename type> 183 template<typename type>
184 void iparse( type &i, const Bu::String &sBuf ) 184 void iparse( type &i, const Bu::String &sBuf )
185 { 185 {
186 if( !sBuf.isSet() ) 186 if( !sBuf.isSet() )
187 return; 187 return;
188 if( sBuf[0] != '+' && sBuf[0] != '-' && 188 if( sBuf[0] != '+' && sBuf[0] != '-' &&
189 (sBuf[0] < '0' && sBuf[0] > '9') ) 189 (sBuf[0] < '0' && sBuf[0] > '9') )
190 return; 190 return;
191 int j = 1; 191 int j = 1;
192 int iMax = sBuf.getSize(); 192 int iMax = sBuf.getSize();
193 for(; j < iMax && (sBuf[j] >= '0' && sBuf[j] <= '9'); j++ ) { } 193 for(; j < iMax && (sBuf[j] >= '0' && sBuf[j] <= '9'); j++ ) { }
194 i = 0; 194 i = 0;
195 type iPos = 1; 195 type iPos = 1;
196 for(j--; j >= 0; j-- ) 196 for(j--; j >= 0; j-- )
197 { 197 {
198 if( sBuf[j] == '+' || sBuf[j] == '-' ) 198 if( sBuf[j] == '+' || sBuf[j] == '-' )
199 continue; 199 continue;
200 i += (sBuf[j]-'0')*iPos; 200 i += (sBuf[j]-'0')*iPos;
201 iPos *= fLast.uRadix; 201 iPos *= fLast.uRadix;
202 } 202 }
203 if( sBuf[0] == '-' ) 203 if( sBuf[0] == '-' )
204 i = -i; 204 i = -i;
205 205
206 usedFormat(); 206 usedFormat();
207 } 207 }
208 208
209 template<typename type> 209 template<typename type>
210 void uparse( type &i, const Bu::String &sBuf ) 210 void uparse( type &i, const Bu::String &sBuf )
211 { 211 {
212 if( !sBuf.isSet() ) 212 if( !sBuf.isSet() )
213 return; 213 return;
214 if( sBuf[0] != '+' && 214 if( sBuf[0] != '+' &&
215 (sBuf[0] < '0' && sBuf[0] > '9') ) 215 (sBuf[0] < '0' && sBuf[0] > '9') )
216 return; 216 return;
217 int j = 1; 217 int j = 1;
218 int iMax = sBuf.getSize(); 218 int iMax = sBuf.getSize();
219 for(; j < iMax && (sBuf[j] >= '0' && sBuf[j] <= '9'); j++ ) { } 219 for(; j < iMax && (sBuf[j] >= '0' && sBuf[j] <= '9'); j++ ) { }
220 i = 0; 220 i = 0;
221 type iPos = 1; 221 type iPos = 1;
222 for(j--; j >= 0; j-- ) 222 for(j--; j >= 0; j-- )
223 { 223 {
224 if( sBuf[j] == '+' ) 224 if( sBuf[j] == '+' )
225 continue; 225 continue;
226 i += (sBuf[j]-'0')*iPos; 226 i += (sBuf[j]-'0')*iPos;
227 iPos *= fLast.uRadix; 227 iPos *= fLast.uRadix;
228 } 228 }
229 229
230 usedFormat(); 230 usedFormat();
231 } 231 }
232 232
233 template<typename type> 233 template<typename type>
234 void fparse( type &f, const Bu::String &sBuf ) 234 void fparse( type &f, const Bu::String &sBuf )
235 { 235 {
236 double fIn; 236 double fIn;
237 sscanf( sBuf.getStr(), "%lf", &fIn ); 237 sscanf( sBuf.getStr(), "%lf", &fIn );
238 f = fIn; 238 f = fIn;
239 usedFormat(); 239 usedFormat();
240 } 240 }
241 241
242 enum Special 242 enum Special
243 { 243 {
244 nl, 244 nl,
245 flush 245 flush
246 }; 246 };
247 247
248 void doFlush(); 248 void doFlush();
249 249
250 Stream &getStream() { return rStream; } 250 Stream &getStream() { return rStream; }
251 operator Stream&() { return rStream; } 251 operator Stream&() { return rStream; }
252 252
253 private: 253 private:
254 Stream &rStream; 254 Stream &rStream;
255 Fmt fLast; 255 Fmt fLast;
256 bool bTempFmt; 256 bool bTempFmt;
257 uint8_t uIndent; 257 uint8_t uIndent;
258 char cIndent; 258 char cIndent;
259 }; 259 };
260 260
261 Formatter &operator<<( Formatter &f, const Fmt &fmt ); 261 Formatter &operator<<( Formatter &f, const Fmt &fmt );
262 Formatter &operator<<( Formatter &f, Formatter::Special s ); 262 Formatter &operator<<( Formatter &f, Formatter::Special s );
263 Formatter &operator<<( Formatter &f, const char *sStr ); 263 Formatter &operator<<( Formatter &f, const char *sStr );
264 Formatter &operator<<( Formatter &f, char *sStr ); 264 Formatter &operator<<( Formatter &f, char *sStr );
265 Formatter &operator<<( Formatter &f, const Bu::String &sStr ); 265 Formatter &operator<<( Formatter &f, const Bu::String &sStr );
266 Formatter &operator<<( Formatter &f, signed char c ); 266 Formatter &operator<<( Formatter &f, signed char c );
267 Formatter &operator<<( Formatter &f, char c ); 267 Formatter &operator<<( Formatter &f, char c );
268 Formatter &operator<<( Formatter &f, unsigned char c ); 268 Formatter &operator<<( Formatter &f, unsigned char c );
269 Formatter &operator<<( Formatter &f, signed short i ); 269 Formatter &operator<<( Formatter &f, signed short i );
270 Formatter &operator<<( Formatter &f, unsigned short i ); 270 Formatter &operator<<( Formatter &f, unsigned short i );
271 Formatter &operator<<( Formatter &f, signed int i ); 271 Formatter &operator<<( Formatter &f, signed int i );
272 Formatter &operator<<( Formatter &f, unsigned int i ); 272 Formatter &operator<<( Formatter &f, unsigned int i );
273 Formatter &operator<<( Formatter &f, signed long i ); 273 Formatter &operator<<( Formatter &f, signed long i );
274 Formatter &operator<<( Formatter &f, unsigned long i ); 274 Formatter &operator<<( Formatter &f, unsigned long i );
275 Formatter &operator<<( Formatter &f, signed long long i ); 275 Formatter &operator<<( Formatter &f, signed long long i );
276 Formatter &operator<<( Formatter &f, unsigned long long i ); 276 Formatter &operator<<( Formatter &f, unsigned long long i );
277 Formatter &operator<<( Formatter &f, float flt ); 277 Formatter &operator<<( Formatter &f, float flt );
278 Formatter &operator<<( Formatter &f, double flt ); 278 Formatter &operator<<( Formatter &f, double flt );
279 Formatter &operator<<( Formatter &f, long double flt ); 279 Formatter &operator<<( Formatter &f, long double flt );
280 Formatter &operator<<( Formatter &f, bool b ); 280 Formatter &operator<<( Formatter &f, bool b );
281 281
282 Formatter &operator>>( Formatter &f, Bu::String &sStr ); 282 Formatter &operator>>( Formatter &f, Bu::String &sStr );
283 Formatter &operator>>( Formatter &f, signed char &c ); 283 Formatter &operator>>( Formatter &f, signed char &c );
284 Formatter &operator>>( Formatter &f, char &c ); 284 Formatter &operator>>( Formatter &f, char &c );
285 Formatter &operator>>( Formatter &f, unsigned char &c ); 285 Formatter &operator>>( Formatter &f, unsigned char &c );
286 Formatter &operator>>( Formatter &f, signed short &i ); 286 Formatter &operator>>( Formatter &f, signed short &i );
287 Formatter &operator>>( Formatter &f, unsigned short &i ); 287 Formatter &operator>>( Formatter &f, unsigned short &i );
288 Formatter &operator>>( Formatter &f, signed int &i ); 288 Formatter &operator>>( Formatter &f, signed int &i );
289 Formatter &operator>>( Formatter &f, unsigned int &i ); 289 Formatter &operator>>( Formatter &f, unsigned int &i );
290 Formatter &operator>>( Formatter &f, signed long &i ); 290 Formatter &operator>>( Formatter &f, signed long &i );
291 Formatter &operator>>( Formatter &f, unsigned long &i ); 291 Formatter &operator>>( Formatter &f, unsigned long &i );
292 Formatter &operator>>( Formatter &f, signed long long &i ); 292 Formatter &operator>>( Formatter &f, signed long long &i );
293 Formatter &operator>>( Formatter &f, unsigned long long &i ); 293 Formatter &operator>>( Formatter &f, unsigned long long &i );
294 Formatter &operator>>( Formatter &f, float &flt ); 294 Formatter &operator>>( Formatter &f, float &flt );
295 Formatter &operator>>( Formatter &f, double &flt ); 295 Formatter &operator>>( Formatter &f, double &flt );
296 Formatter &operator>>( Formatter &f, long double &flt ); 296 Formatter &operator>>( Formatter &f, long double &flt );
297 Formatter &operator>>( Formatter &f, bool &b ); 297 Formatter &operator>>( Formatter &f, bool &b );
298 298
299 template<typename type> 299 template<typename type>
300 Formatter &operator<<( Formatter &f, const type *p ) 300 Formatter &operator<<( Formatter &f, const type *p )
301 { 301 {
302 return f << "0x" << Fmt::hex(sizeof(ptrdiff_t)*2) << (ptrdiff_t)(p); 302 return f << "0x" << Fmt::hex(sizeof(ptrdiff_t)*2) << (ptrdiff_t)(p);
303 } 303 }
304}; 304};
305 305
306#endif 306#endif