12 #ifndef ECL_FORMATTERS_FLOATS_HPP_
13 #define ECL_FORMATTERS_FLOATS_HPP_
25 #define snprintf _snprintf
48 namespace interfaces {
60 template <
typename Number>
106 FormatFloat<Number>&
width(
const int w);
208 template <
typename OutputStream,
typename N>
228 template <
typename OutputStream>
void pad(
int n, OutputStream &ostream)
const;
229 template <
typename OutputStream>
void prePad(
int n, OutputStream &ostream)
const;
230 template <
typename OutputStream>
void postPad(
int n, OutputStream &ostream)
const;
235 template <
typename OutputStream>
void formatFixed(OutputStream &ostream)
const;
236 template <
typename OutputStream>
void formatSci(OutputStream &ostream)
const;
247 template <
typename Number>
257 template <
typename Number>
261 if ( ( *width_ > 0 ) && ( *alignment_ ==
NoAlign ) )
273 template <
typename Number>
288 template <
typename Number>
296 template <
typename Number>
309 template <
typename Number>
313 ready_to_format =
true;
319 template <
typename Number>
325 precision_ = &tmp_precision;
328 alignment_ = &tmp_alignment;
329 align(prm_alignment);
335 ready_to_format =
true;
339 template <
typename Number>
345 precision_ = &tmp_precision;
348 alignment_ = &tmp_alignment;
355 ready_to_format =
true;
363 template <
typename Number>
364 template <
typename OutputStream>
367 static char format_specifier[6] =
"%.xff";
368 if ( *precision_ < 10 ) {
369 format_specifier[2] =
'0'+*precision_;
370 format_specifier[3] =
'f';
371 format_specifier[4] =
'\0';
372 }
else if ( *precision_ < 20 ) {
373 format_specifier[2] =
'1';
374 format_specifier[3] =
'0'+(*precision_ - 10);
375 format_specifier[4] =
'f';
377 format_specifier[2] =
'2';
378 format_specifier[3] =
'0';
379 format_specifier[4] =
'f';
381 static const int buffer_size = 30;
382 static char buffer[buffer_size];
384 snprintf(buffer, buffer_size, format_specifier, value_);
389 int length = strlen(buffer);
390 prePad(*width_ - length,ostream);
392 postPad(*width_ - length,ostream);
395 template <
typename Number>
396 template <
typename OutputStream>
399 ostream <<
"Scientific format is not yet supported." <<
"\n";
403 template <
typename Number>
404 template <
typename OutputStream>
407 if ( n <= 0 ) {
return; }
408 switch ( *alignment_ )
412 case (
RightAlign ) : { pad(n,ostream);
break; }
413 case (
CentreAlign ) : { pad(n/2+ n%2,ostream);
break; }
418 template <
typename Number>
419 template <
typename OutputStream>
422 if ( n <= 0 ) {
return; }
423 switch ( *alignment_ )
428 case (
CentreAlign ) : { pad(n/2,ostream);
break; }
433 template <
typename Number>
434 template <
typename OutputStream>
437 for (
int i = n; i > 0; --i )
446 template <
typename OutputStream,
typename N>
449 bool ready = formatter.ready_to_format;
452 "either there is no data available, or you have tried to use the "
453 "formatter more than once in a single streaming operation. "
454 "C++ produces unspecified results when functors are used multiply "
455 "in the same stream sequence, so this is not permitted here.") );
459 switch(*(formatter.base_) )
462 formatter.formatFixed(ostream);
466 formatter.formatSci(ostream);
472 if ( formatter.width_ != &(formatter.prm_width) ) {
473 formatter.width_ = &(formatter.prm_width);
474 formatter.precision_ = &(formatter.prm_precision);
475 formatter.alignment_ = &(formatter.prm_alignment);
476 formatter.base_ = &(formatter.prm_base);
478 formatter.ready_to_format =
false;
496 class Format<float> :
public interfaces::FormatFloat<float>
507 interfaces::FormatFloat<float>(w, p, a, b) {}
519 class Format<double> :
public interfaces::FormatFloat<double>
530 interfaces::FormatFloat<double>(w, p, a, b) {