Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
format.cc
Go to the documentation of this file.
1 // Fortran-Compatible Formatted Input/Output Support
2 //
3 // Project: Objexx Fortran Compatibility Library (ObjexxFCL)
4 //
5 // Version: 3.0.0
6 //
7 // Language: C++
8 //
9 // Copyright (c) 2000-2009 Objexx Engineering, Inc. All Rights Reserved.
10 // Use of this source code or any derivative of it is restricted by license.
11 // Licensing is available from Objexx Engineering, Inc.: http://objexx.com Objexx@objexx.com
12 
13 
14 // ObjexxFCL Headers
15 #include <ObjexxFCL/format.hh>
16 #include <ObjexxFCL/byte.hh>
17 #include <ObjexxFCL/ubyte.hh>
18 #include <ObjexxFCL/Fstring.hh>
19 
20 // C++ Headers
21 #include <cmath>
22 #include <istream>
23 
24 
25 namespace ObjexxFCL {
26 namespace format {
27 
28 
29 // Formatted Input
30 
31 
32 // Bite Explicit Specializations
33 
34 
35  /// @brief Assign Stream Bite to Value: byte Specialization
36  template<>
37  void
38  Bite< byte >::assign( std::stringstream & ss ) const
39  {
40  if ( is_blank_string( ss.str() ) ) {
41  t_ = 0;
42  } else {
43  ss >> t_;
44  if ( ss.fail() ) t_ = 0;
45  }
46  }
47 
48 
49  /// @brief Assign Stream Bite to Value: ubyte Specialization
50  template<>
51  void
52  Bite< ubyte >::assign( std::stringstream & ss ) const
53  {
54  if ( is_blank_string( ss.str() ) ) {
55  t_ = 0;
56  } else {
57  ss >> t_;
58  if ( ss.fail() ) t_ = 0;
59  }
60  }
61 
62 
63  /// @brief Assign Stream Bite to Value: Fstring Specialization
64  template<>
65  void
66  Bite< Fstring >::assign( std::stringstream & ss ) const
67  {
68  t_ = ss.str();
69  }
70 
71 
72 // Bite Makers
73 
74 
75 /// @brief Fstring Bite Maker: Take Length of Fstring
77 bite( Fstring & t )
78 {
79  return Bite< Fstring >( t.length(), t );
80 }
81 
82 
83 // Formatted Output
84 
85 
86 // General Formatting
87 
88 
89 /// @brief Single-Spaced Format: bool Specialization
90 template<>
91 std::string
92 SS( bool const & t )
93 {
94  return L( 2, t );
95 }
96 
97 
98 /// @brief Single-Spaced Format: float Specialization
99 template<>
100 std::string
101 SS( float const & t )
102 {
103  std::ostringstream fmt_stream;
104  fmt_stream << std::left << std::noshowpoint << std::uppercase << std::setprecision( 9 ) << ( t < 0.0f ? " " : " " ) << t;
105  return fmt_stream.str();
106 }
107 
108 
109 /// @brief Single-Spaced Format: double Specialization
110 template<>
111 std::string
112 SS( double const & t )
113 {
114  std::ostringstream fmt_stream;
115  fmt_stream << std::left << std::noshowpoint << std::uppercase << std::setprecision( 9 ) << ( t < 0.0 ? " " : " " ) << t;
116  return fmt_stream.str();
117 }
118 
119 
120 /// @brief Single-Spaced Format: long double Specialization
121 template<>
122 std::string
123 SS( long double const & t )
124 {
125  std::ostringstream fmt_stream;
126  fmt_stream << std::left << std::noshowpoint << std::uppercase << std::setprecision( 9 ) << ( t < 0.0l ? " " : " " ) << t;
127  return fmt_stream.str();
128 }
129 
130 
131 /// @brief Single-Spaced Format: complex< float > Specialization
132 template<>
133 std::string
134 SS( std::complex< float > const & t )
135 {
136  std::ostringstream fmt_stream;
137  fmt_stream << std::left << std::noshowpoint << std::uppercase << std::setprecision( 9 ) << " ("
138  << ( t.real() < 0.0f ? "" : " " ) << t.real() << ','
139  << ( t.imag() < 0.0f ? "" : " " ) << t.imag() << ')';
140  return fmt_stream.str();
141 }
142 
143 
144 /// @brief Single-Spaced Format: complex< double > Specialization
145 template<>
146 std::string
147 SS( std::complex< double > const & t )
148 {
149  std::ostringstream fmt_stream;
150  fmt_stream << std::left << std::noshowpoint << std::uppercase << std::setprecision( 9 ) << " ("
151  << ( t.real() < 0.0 ? "" : " " ) << t.real() << ','
152  << ( t.imag() < 0.0 ? "" : " " ) << t.imag() << ')';
153  return fmt_stream.str();
154 }
155 
156 
157 /// @brief Single-Spaced Format: complex< long double > Specialization
158 template<>
159 std::string
160 SS( std::complex< long double > const & t )
161 {
162  std::ostringstream fmt_stream;
163  fmt_stream << std::left << std::noshowpoint << std::uppercase << std::setprecision( 9 ) << " ("
164  << ( t.real() < 0.0l ? "" : " " ) << t.real() << ','
165  << ( t.imag() < 0.0l ? "" : " " ) << t.imag() << ')';
166  return fmt_stream.str();
167 }
168 
169 
170 // String Formatting
171 
172 
173 /// @brief char Format
174 std::string
175 A( int const w, char const c )
176 {
177  if ( w <= 0 ) {
178  return std::string();
179  } else if ( w == 1 ) {
180  return std::string( 1, c );
181  } else {
182  return std::string( w-1, ' ' ) + c;
183  }
184 }
185 
186 
187 /// @brief char Format (Width==1)
188 std::string
189 A( char const c )
190 {
191  return std::string( 1, c );
192 }
193 
194 
195 /// @brief cstring Format
196 std::string
197 A( int const w, c_cstring const s )
198 {
199  return A( w, std::string( s ) );
200 }
201 
202 
203 /// @brief cstring Format (Width of cstring)
204 std::string
205 A( c_cstring const s )
206 {
207  return std::string( s );
208 }
209 
210 
211 /// @brief string Format
212 std::string
213 A( int const w, std::string const & s )
214 {
215  std::string::size_type const s_length( s.length() );
216  if ( w <= 0 ) {
217  return std::string();
218  } else if ( static_cast< int >( s_length ) > w ) {
219  return s.substr( 0, w );
220  } else if ( static_cast< int >( s_length ) == w ) {
221  return s;
222  } else { // s_length < w
223  return std::string( w - s_length, ' ' ) + s;
224  }
225 }
226 
227 
228 /// @brief string Format (Width of string)
229 std::string const &
230 A( std::string const & s )
231 {
232  return s;
233 }
234 
235 
236 /// @brief Fstring Format
237 std::string
238 A( int const w, Fstring const & s )
239 {
240  std::string::size_type const s_length( s.length() );
241  if ( w <= 0 ) {
242  return std::string();
243  } else if ( static_cast< int >( s_length ) > w ) {
244  return s( 1, w );
245  } else if ( static_cast< int >( s_length ) == w ) {
246  return s;
247  } else { // s_length < w
248  return std::string( w - s_length, ' ' ) + s;
249  }
250 }
251 
252 
253 /// @brief Fstring Format (Width of Fstring)
254 std::string
255 A( Fstring const & s )
256 {
257  return s;
258 }
259 
260 
261 /// @brief Blank string
262 std::string
263 X( int const w )
264 {
265  return std::string( std::max( w, 0 ), ' ' );
266 }
267 
268 
269 /// @brief Blank string
270 std::string
271 space( int const w )
272 {
273  return std::string( std::max( w, 0 ), ' ' );
274 }
275 
276 /// @brief Blank string
277 std::string
278 repeat( int const w, char c )
279 {
280  return std::string( std::max( w, 0 ), c );
281 }
282 
283 
284 // Logical Formatting
285 
286 
287 /// @brief Logical Format
288 std::string
289 L( int const w, bool const & t )
290 {
291  if ( w <= 1 ) {
292  return std::string( 1, ( t ? 'T' : 'F' ) );
293  } else {
294  return std::string( w-1, ' ' ) + std::string( 1, ( t ? 'T' : 'F' ) );
295  }
296 }
297 
298 
299 /// @brief Logical Format (Width==1)
300 std::string
301 L( bool const & t )
302 {
303  return std::string( 1, ( t ? 'T' : 'F' ) );
304 }
305 
306 
307 // Floating Point Formatting
308 
309 
310 /// @brief Exponential Format: float
311 std::string
312 E( int const w, int const d, float const & t )
313 {
314  if ( w <= 0 ) return std::string();
315  std::ostringstream fmt_stream;
316  fmt_stream << std::scientific << std::showpoint << std::uppercase
317  << std::setprecision( std::max( std::min( d, w-7 ), 0 ) ) << std::setw( w ) << t;
318  return fmt_stream.str();
319 }
320 
321 
322 /// @brief Exponential Format: double
323 std::string
324 E( int const w, int const d, double const & t )
325 {
326  if ( w <= 0 ) return std::string();
327  std::ostringstream fmt_stream;
328  fmt_stream << std::scientific << std::showpoint << std::uppercase
329  << std::setprecision( std::max( std::min( d, w-7 ), 0 ) ) << std::setw( w ) << t;
330  return fmt_stream.str();
331 }
332 
333 
334 /// @brief Exponential Format: long double
335 std::string
336 E( int const w, int const d, long double const & t )
337 {
338  if ( w <= 0 ) return std::string();
339  std::ostringstream fmt_stream;
340  fmt_stream << std::scientific << std::showpoint << std::uppercase
341  << std::setprecision( std::max( std::min( d, w-7 ), 0 ) ) << std::setw( w ) << t;
342  return fmt_stream.str();
343 }
344 
345 
346 /// @brief Exponential Format: complex< float >
347 std::string
348 E( int const w, int const d, std::complex< float > const & t )
349 {
350  if ( w <= 0 ) return std::string();
351  std::ostringstream fmt_stream;
352  fmt_stream << std::scientific << std::showpoint << std::uppercase
353  << std::setprecision( std::max( std::min( d, w-7 ), 0 ) )
354  << '(' << std::setw( w ) << t.real() << ',' << std::setw( w ) << t.imag() << ')';
355  return fmt_stream.str();
356 }
357 
358 
359 /// @brief Exponential Format: complex< double >
360 std::string
361 E( int const w, int const d, std::complex< double > const & t )
362 {
363  if ( w <= 0 ) return std::string();
364  std::ostringstream fmt_stream;
365  fmt_stream << std::scientific << std::showpoint << std::uppercase
366  << std::setprecision( std::max( std::min( d, w-7 ), 0 ) )
367  << '(' << std::setw( w ) << t.real() << ',' << std::setw( w ) << t.imag() << ')';
368  return fmt_stream.str();
369 }
370 
371 
372 /// @brief Exponential Format: complex< long double >
373 std::string
374 E( int const w, int const d, std::complex< long double > const & t )
375 {
376  if ( w <= 0 ) return std::string();
377  std::ostringstream fmt_stream;
378  fmt_stream << std::scientific << std::showpoint << std::uppercase
379  << std::setprecision( std::max( std::min( d, w-7 ), 0 ) )
380  << '(' << std::setw( w ) << t.real() << ',' << std::setw( w ) << t.imag() << ')';
381  return fmt_stream.str();
382 }
383 
384 
385 /// @brief Fixed Point Format: float
386 std::string
387 F( int const w, int const d, float const & t )
388 {
389  if ( w <= 0 ) return std::string();
390  int const p( w - 3 + ( t >= 0.0f ? 1 : 0 ) + ( std::abs( t ) < 1.0f - 0.5f/std::pow( 10.0f, std::max( d, 0 ) ) ? 1 : 0 ) );
391  std::stringstream fmt_stream;
392  fmt_stream << std::fixed << std::showpoint
393  << std::setprecision( std::max( std::min( d, p ), 0 ) ) << std::setw( w ) << t;
394  if ( ( t < 0.0f ) && ( t >= -0.5f ) ) { // Remove sign from -0.0
395  float x;
396  fmt_stream >> x;
397  if ( x == 0.0f ) return F( w, d, 0.0f );
398  }
399  if ( fmt_stream.str().length() > std::string::size_type( w ) ) { // Exceeded field width
400  if ( fmt_stream.str()[ 0 ] == '0' ) {
401  return fmt_stream.str().substr( 1 ); // Trim lead zero
402  } else if ( fmt_stream.str().substr( 0, 2 ) == "-0" ) {
403  return '-' + fmt_stream.str().substr( 2 ); // Trim lead zero
404  }
405  }
406  return fmt_stream.str();
407 }
408 
409 
410 /// @brief Fixed Point Format: double
411 std::string
412 F( int const w, int const d, double const & t )
413 {
414  if ( w <= 0 ) return std::string();
415  int const p( w - 3 + ( t >= 0.0 ? 1 : 0 ) + ( std::abs( t ) < 1.0 - 0.5/std::pow( 10.0, std::max( d, 0 ) ) ? 1 : 0 ) );
416  std::stringstream fmt_stream;
417  fmt_stream << std::fixed << std::showpoint
418  << std::setprecision( std::max( std::min( d, p ), 0 ) ) << std::setw( w ) << t;
419  if ( ( t < 0.0 ) && ( t >= -0.5 ) ) { // Remove sign from -0.0
420  double x;
421  fmt_stream >> x;
422  if ( x == 0.0 ) return F( w, d, 0.0 );
423  }
424  if ( fmt_stream.str().length() > std::string::size_type( w ) ) { // Exceeded field width
425  if ( fmt_stream.str()[ 0 ] == '0' ) {
426  return fmt_stream.str().substr( 1 ); // Trim lead zero
427  } else if ( fmt_stream.str().substr( 0, 2 ) == "-0" ) {
428  return '-' + fmt_stream.str().substr( 2 ); // Trim lead zero
429  }
430  }
431  return fmt_stream.str();
432 }
433 
434 
435 /// @brief Fixed Point Format: long double
436 std::string
437 F( int const w, int const d, long double const & t )
438 {
439  if ( w <= 0 ) return std::string();
440  int const p( w - 3 + ( t >= 0.0l ? 1 : 0 ) + ( std::abs( t ) < 1.0l - 0.5l/std::pow( 10.0l, std::max( d, 0 ) ) ? 1 : 0 ) );
441  std::stringstream fmt_stream;
442  fmt_stream << std::fixed << std::showpoint
443  << std::setprecision( std::max( std::min( d, p ), 0 ) ) << std::setw( w ) << t;
444  if ( ( t < 0.0L ) && ( t >= -0.5L ) ) { // Remove sign from -0.0
445  long double x;
446  fmt_stream >> x;
447  if ( x == 0.0L ) return F( w, d, 0.0L );
448  }
449  if ( fmt_stream.str().length() > std::string::size_type( w ) ) { // Exceeded field width
450  if ( fmt_stream.str()[ 0 ] == '0' ) {
451  return fmt_stream.str().substr( 1 ); // Trim lead zero
452  } else if ( fmt_stream.str().substr( 0, 2 ) == "-0" ) {
453  return '-' + fmt_stream.str().substr( 2 ); // Trim lead zero
454  }
455  }
456  return fmt_stream.str();
457 }
458 
459 
460 /// @brief Fixed Point Format: complex< float >
461 std::string
462 F( int const w, int const d, std::complex< float > const & t )
463 {
464  return '(' + F( w, d, t.real() ) + ',' + F( w, d, t.imag() ) + ')';
465 }
466 
467 
468 /// @brief Fixed Point Format: complex< double >
469 std::string
470 F( int const w, int const d, std::complex< double > const & t )
471 {
472  return '(' + F( w, d, t.real() ) + ',' + F( w, d, t.imag() ) + ')';
473 }
474 
475 
476 /// @brief Fixed Point Format: complex< long double >
477 std::string
478 F( int const w, int const d, std::complex< long double > const & t )
479 {
480  return '(' + F( w, d, t.real() ) + ',' + F( w, d, t.imag() ) + ')';
481 }
482 
483 
484 /// @brief General Format: float
485 std::string
486 G( int const w, int const d, float const & t )
487 {
488  double const m( std::abs( t ) );
489  if ( m == 0.0 ) {
490  return F( w-4, d-1, t ) + std::string( 4, ' ' );
491  } else if ( m < 0.1 - ( 0.5 * std::pow( 10.0, -d-2 ) ) ) {
492  return E( w, d, t );
493  } else if ( m >= std::pow( 10.0, d ) - 0.5 ) {
494  return E( w, d, t );
495  } else {
496  int const i( static_cast< int >( std::floor( std::log10( m / ( 0.1 - 0.5 / std::pow( 10.0, d+1 ) ) ) ) ) );
497  return F( w-4, d-i, t ) + std::string( 4, ' ' );
498  }
499 }
500 
501 
502 /// @brief General Format: double
503 std::string
504 G( int const w, int const d, double const & t )
505 {
506  double const m( std::abs( t ) );
507  if ( m == 0.0 ) {
508  return F( w-4, d-1, t ) + std::string( 4, ' ' );
509  } else if ( m < 0.1 - ( 0.5 * std::pow( 10.0, -d-2 ) ) ) {
510  return E( w, d, t );
511  } else if ( m >= std::pow( 10.0, d ) - 0.5 ) {
512  return E( w, d, t );
513  } else {
514  int const i( static_cast< int >( std::floor( std::log10( m / ( 0.1 - 0.5 / std::pow( 10.0, d+1 ) ) ) ) ) );
515  return F( w-4, d-i, t ) + std::string( 4, ' ' );
516  }
517 }
518 
519 
520 /// @brief General Format: long double
521 std::string
522 G( int const w, int const d, long double const & t )
523 {
524  long double const m( std::abs( t ) );
525  if ( m == 0.0L ) {
526  return F( w-4, d-1, t ) + std::string( 4, ' ' );
527  } else if ( m < 0.1L - ( 0.5L * std::pow( 10.0L, -d-2 ) ) ) {
528  return E( w, d, t );
529  } else if ( m >= std::pow( 10.0L, d ) - 0.5L ) {
530  return E( w, d, t );
531  } else {
532  int const i( static_cast< int >( std::floor( std::log10( m / ( 0.1l - 0.5l / std::pow( 10.0l, d+1 ) ) ) ) ) );
533  return F( w-4, d-i, t ) + std::string( 4, ' ' );
534  }
535 }
536 
537 
538 /// @brief General Format: complex< float >
539 std::string
540 G( int const w, int const d, std::complex< float > const & t )
541 {
542  return '(' + G( w, d, t.real() ) + ',' + G( w, d, t.imag() ) + ')';
543 }
544 
545 
546 /// @brief General Format: complex< double >
547 std::string
548 G( int const w, int const d, std::complex< double > const & t )
549 {
550  return '(' + G( w, d, t.real() ) + ',' + G( w, d, t.imag() ) + ')';
551 }
552 
553 
554 /// @brief General Format: complex< long double >
555 std::string
556 G( int const w, int const d, std::complex< long double > const & t )
557 {
558  return '(' + G( w, d, t.real() ) + ',' + G( w, d, t.imag() ) + ')';
559 }
560 
561 
562 // Standard Formatting
563 
564 
565 /// @brief Standard Width Format: bool Specialization
566 template<>
567 std::string
568 SW( bool const & t )
569 {
570  return L( 2, t );
571 }
572 
573 
574 /// @brief Standard Width Format: byte Specialization
575 template<>
576 std::string
577 SW( byte const & t )
578 {
579  return I( 7, t );
580 }
581 
582 
583 /// @brief Standard Width Format: short Specialization
584 template<>
585 std::string
586 SW( short int const & t )
587 {
588  return I( 7, t );
589 }
590 
591 
592 /// @brief Standard Width Format: unsigned short Specialization
593 template<>
594 std::string
595 SW( unsigned short int const & t )
596 {
597  return I( 7, t );
598 }
599 
600 
601 /// @brief Standard Width Format: int Specialization
602 template<>
603 std::string
604 SW( int const & t )
605 {
606  return I( 12, t );
607 }
608 
609 
610 /// @brief Standard Width Format: unsigned int Specialization
611 template<>
612 std::string
613 SW( unsigned int const & t )
614 {
615  return I( 12, t );
616 }
617 
618 
619 /// @brief Standard Width Format: long int Specialization
620 template<>
621 std::string
622 SW( long int const & t )
623 {
624  return I( 22, t );
625 }
626 
627 
628 /// @brief Standard Width Format: unsigned long int Specialization
629 template<>
630 std::string
631 SW( unsigned long int const & t )
632 {
633  return I( 22, t );
634 }
635 
636 
637 /// @brief Standard Width Format: float Specialization
638 template<>
639 std::string
640 SW( float const & t )
641 {
642  return G( 16, 7, t );
643 }
644 
645 
646 /// @brief Standard Width Format: double Specialization
647 template<>
648 std::string
649 SW( double const & t )
650 {
651  return G( 26, 17, t );
652 }
653 
654 
655 /// @brief Standard Width Format: long double Specialization
656 template<>
657 std::string
658 SW( long double const & t )
659 {
660  return G( 40, 32, t ); // This should be enough for 128-bit quad precision: long double is 80-bit on some platforms
661 }
662 
663 
664 /// @brief Standard Width Format: complex< float > Specialization
665 template<>
666 std::string
667 SW( std::complex< float > const & t )
668 {
669  return '(' + SW( t.real() ) + ',' + SW( t.imag() ) + ')';
670 }
671 
672 
673 /// @brief Standard Width Format: complex< double > Specialization
674 template<>
675 std::string
676 SW( std::complex< double > const & t )
677 {
678  return '(' + SW( t.real() ) + ',' + SW( t.imag() ) + ')';
679 }
680 
681 
682 /// @brief Standard Width Format: complex< long double > Specialization
683 template<>
684 std::string
685 SW( std::complex< long double > const & t )
686 {
687  return '(' + SW( t.real() ) + ',' + SW( t.imag() ) + ')';
688 }
689 
690 
691 // Manipulators
692 
693 
694 /// @brief general: Manipulator to Turn Off scientific or fixed
695 std::ios_base &
696 general( std::ios_base & base )
697 {
698  base.unsetf( std::ios_base::fixed );
699  base.unsetf( std::ios_base::scientific );
700  return base;
701 }
702 
703 
704 } // namespace format
705 } // namespace ObjexxFCL
std::string G(int const w, int const d, float const &t)
General Format: float.
Definition: format.cc:486
static T min(T x, T y)
Definition: Svm.cc:16
std::string space(int const w)
Blank string.
Definition: format.cc:271
char const * c_cstring
Definition: format.hh:46
std::string X(int const w)
Blank string.
Definition: format.cc:263
cmplx w(cmplx z, double relerr)
Definition: functions.cc:470
std::string SS(bool const &t)
Single-Spaced Format: bool Specialization.
Definition: format.cc:92
def x
std::string E(int const w, int const d, float const &t)
Exponential Format: float.
Definition: format.cc:312
std::ios_base & general(std::ios_base &base)
general: Manipulator to Turn Off scientific or fixed
Definition: format.cc:696
tuple p
Definition: docking.py:9
T abs(T const &x)
std::abs( x ) == | x |
Definition: Fmath.hh:295
std::string L(int const w, bool const &t)
Logical Format.
Definition: format.cc:289
size_type length() const
Length.
Definition: Fstring.hh:514
void assign(std::stringstream &ss) const
Assign Stream Bite to Value: Generic Implementation.
Definition: format.hh:114
bool is_blank_string(std::string const &s)
string is Blank?
Definition: format.hh:142
DimensionExpressionPow pow(Dimension const &dim1, Dimension const &dim2)
pow( Dimension, Dimension )
std::string F(int const w, int const d, float const &t)
Fixed Point Format: float.
Definition: format.cc:387
Fstring: Fixed-Length Fortran-Compatible String.
Definition: Fstring.hh:53
std::string I(int const w, T const &t)
Integer Format.
Definition: format.hh:758
static T max(T x, T y)
Definition: Svm.cc:19
Bite< Fstring > bite(Fstring &t)
Fstring Bite Maker: Take Length of Fstring.
Definition: format.cc:77
char & uppercase(char &c)
Uppercase a Character.
std::string A(int const w, char const c)
char Format
Definition: format.cc:175
std::string repeat(int const w, char c)
Blank string.
Definition: format.cc:278
byte: One-Byte Integer
Definition: byte.hh:31
std::string SW(FArray1< T > const &a)
Standard Width Format: FArray1.
Definition: FArray1.io.hh:105