| 1 |
#ifndef FORMAT_LIB |
| 2 |
#define FORMAT_LIB |
| 3 |
|
| 4 |
#include "str.h" |
| 5 |
|
| 6 |
#ifdef use_namespace |
| 7 |
namespace RBD_STRING { using namespace RBD_COMMON; } |
| 8 |
namespace RBD_LIBRARIES { using namespace RBD_STRING; } |
| 9 |
namespace RBD_STRING { |
| 10 |
#endif |
| 11 |
|
| 12 |
|
| 13 |
class Format |
| 14 |
{ |
| 15 |
public: |
| 16 |
enum OUF {E, HASH, ZERO}; |
| 17 |
enum A {LEFT, RIGHT, CENTRE, CENTER}; |
| 18 |
enum FT {SIG_FIGS, DEC_FIGS, SCIENTIFIC, INTEGER}; |
| 19 |
enum POS {PLUS, SPACE, NX}; |
| 20 |
enum V {VAR0, VAR1}; |
| 21 |
private: |
| 22 |
int min_width_val; |
| 23 |
int max_width_val; |
| 24 |
OUF overflow_policy_val; |
| 25 |
OUF underflow_policy_val; |
| 26 |
A alignment_val; |
| 27 |
FT format_type_val; |
| 28 |
int precision_val; |
| 29 |
POS positive_val; |
| 30 |
String prefix_val; |
| 31 |
String suffix_val; |
| 32 |
String separator_val; |
| 33 |
V variant_val; |
| 34 |
public: |
| 35 |
Format() : min_width_val(0), max_width_val(12), overflow_policy_val(E), |
| 36 |
underflow_policy_val(ZERO), alignment_val(RIGHT), |
| 37 |
format_type_val(SIG_FIGS), precision_val(2), positive_val(NX), |
| 38 |
prefix_val(""), suffix_val(""), separator_val(","), |
| 39 |
variant_val(VAR0) {} |
| 40 |
int MinWidth() const { return min_width_val; } |
| 41 |
void MinWidth(int mw) { min_width_val = mw; } |
| 42 |
int MaxWidth() const { return max_width_val; } |
| 43 |
void MaxWidth(int mw) { max_width_val = mw; } |
| 44 |
void Width(int w) { max_width_val = min_width_val = w; } |
| 45 |
OUF OverFlowPolicy() const { return overflow_policy_val; } |
| 46 |
void OverFlowPolicy(OUF of) { overflow_policy_val = of; } |
| 47 |
OUF UnderFlowPolicy() const { return underflow_policy_val; } |
| 48 |
void UnderFlowPolicy(OUF uf) { underflow_policy_val = uf; } |
| 49 |
A Alignment() const { return alignment_val; } |
| 50 |
void Alignment (A a) { alignment_val = a; } |
| 51 |
FT FormatType() const { return format_type_val; } |
| 52 |
void FormatType (FT ft) { format_type_val = ft; } |
| 53 |
POS Positive() const { return positive_val; } |
| 54 |
void Positive(POS p) { positive_val = p; } |
| 55 |
int Precision() const { return precision_val; } |
| 56 |
void Precision(int p) { precision_val = p; } |
| 57 |
String Prefix() const { return prefix_val; } |
| 58 |
void Prefix(const String& p) { prefix_val = p; } |
| 59 |
String Suffix() const { return suffix_val; } |
| 60 |
void Suffix(const String& s) { suffix_val = s; } |
| 61 |
String Separator() const { return separator_val; } |
| 62 |
void Separator(const String& s) { separator_val = s; } |
| 63 |
V Variant() const { return variant_val; } |
| 64 |
void Variant(V v) { variant_val = v; } |
| 65 |
|
| 66 |
int min_width() const { return min_width_val; } |
| 67 |
void min_width(int mw) { min_width_val = mw; } |
| 68 |
int max_width() const { return max_width_val; } |
| 69 |
void max_width(int mw) { max_width_val = mw; } |
| 70 |
void width(int w) { max_width_val = min_width_val = w; } |
| 71 |
OUF overflow_policy() const { return overflow_policy_val; } |
| 72 |
void overflow_policy(OUF of) { overflow_policy_val = of; } |
| 73 |
OUF underflow_policy() const { return underflow_policy_val; } |
| 74 |
void underflow_policy(OUF uf) { underflow_policy_val = uf; } |
| 75 |
A alignment() const { return alignment_val; } |
| 76 |
void alignment (A a) { alignment_val = a; } |
| 77 |
FT format_type() const { return format_type_val; } |
| 78 |
void format_type (FT ft) { format_type_val = ft; } |
| 79 |
POS positive() const { return positive_val; } |
| 80 |
void positive(POS p) { positive_val = p; } |
| 81 |
int precision() const { return precision_val; } |
| 82 |
void precision(int p) { precision_val = p; } |
| 83 |
String prefix() const { return prefix_val; } |
| 84 |
void prefix(const String& p) { prefix_val = p; } |
| 85 |
String suffix() const { return suffix_val; } |
| 86 |
void suffix(const String& s) { suffix_val = s; } |
| 87 |
String separator() const { return separator_val; } |
| 88 |
void separator(const String& s) { separator_val = s; } |
| 89 |
V variant() const { return variant_val; } |
| 90 |
void variant(V v) { variant_val = v; } |
| 91 |
|
| 92 |
friend class OstreamWithFormat; |
| 93 |
private: |
| 94 |
void SigFig(ostream& os, double value, int nsig) const; |
| 95 |
void DecFig(ostream& os, double value, int ndec, bool force_decimal=false) |
| 96 |
const; |
| 97 |
void IntegerPrint(ostream& os, double value) const; |
| 98 |
void Scientific(ostream& os, double value, int nsig) const; |
| 99 |
|
| 100 |
void FormatOut(ostream& os, double value) const; |
| 101 |
|
| 102 |
void StringPrint(ostream& os, const char* value, int size) const; |
| 103 |
|
| 104 |
void ErrorOut(ostream& os) const; |
| 105 |
}; |
| 106 |
|
| 107 |
class OstreamWithFormat |
| 108 |
{ |
| 109 |
ostream& os; |
| 110 |
const Format& f; |
| 111 |
public: |
| 112 |
OstreamWithFormat(ostream& osx, const Format& fx) |
| 113 |
: os(osx), f(fx) {} |
| 114 |
|
| 115 |
OstreamWithFormat(OstreamWithFormat const& owf) |
| 116 |
: os(owf.os), f(owf.f) {} |
| 117 |
|
| 118 |
OstreamWithFormat& operator<<(const char* value); |
| 119 |
|
| 120 |
OstreamWithFormat& operator<<(const String& value) |
| 121 |
{ f.StringPrint(os, value.data(), value.size()); return *this; } |
| 122 |
|
| 123 |
OstreamWithFormat& operator<<(int value) |
| 124 |
{ f.IntegerPrint(os, (double)value); return *this; } |
| 125 |
|
| 126 |
OstreamWithFormat& operator<<(unsigned int value) |
| 127 |
{ f.IntegerPrint(os, (double)value); return *this; } |
| 128 |
|
| 129 |
OstreamWithFormat& operator<<(double value) |
| 130 |
{ f.FormatOut(os, value); return *this; } |
| 131 |
|
| 132 |
OstreamWithFormat& operator<<(float value) |
| 133 |
{ return operator<<((double)value); } |
| 134 |
|
| 135 |
OstreamWithFormat& operator<<(long value) |
| 136 |
{ f.IntegerPrint(os, (double)value); return *this; } |
| 137 |
|
| 138 |
OstreamWithFormat& operator<<(unsigned long value) |
| 139 |
{ f.IntegerPrint(os, (double)value); return *this; } |
| 140 |
|
| 141 |
OstreamWithFormat& operator<<(char value) { os << value; return *this; } |
| 142 |
|
| 143 |
OstreamWithFormat& operator<< (OstreamWithFormat& (*_f)(OstreamWithFormat&)) |
| 144 |
{ _f(*this); return *this; } |
| 145 |
|
| 146 |
const Format& format() const { return f; } |
| 147 |
|
| 148 |
friend OstreamWithFormat |
| 149 |
operator<<(OstreamWithFormat& osx, const Format& fx); |
| 150 |
|
| 151 |
friend OstreamWithFormat& endl(OstreamWithFormat&); |
| 152 |
friend OstreamWithFormat& ends(OstreamWithFormat&); |
| 153 |
friend OstreamWithFormat& flush(OstreamWithFormat&); |
| 154 |
|
| 155 |
}; |
| 156 |
|
| 157 |
inline OstreamWithFormat operator<<(ostream& osx, const Format& fx) |
| 158 |
{ return OstreamWithFormat(osx, fx); } |
| 159 |
|
| 160 |
inline OstreamWithFormat operator<<(OstreamWithFormat& osx, const Format& fx) |
| 161 |
{ return OstreamWithFormat(osx.os, fx); } |
| 162 |
|
| 163 |
OstreamWithFormat& endl(OstreamWithFormat&); |
| 164 |
OstreamWithFormat& ends(OstreamWithFormat&); |
| 165 |
OstreamWithFormat& flush(OstreamWithFormat&); |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
#ifdef use_namespace |
| 170 |
} |
| 171 |
#endif |
| 172 |
|
| 173 |
|
| 174 |
#endif |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|