00001
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <ctype.h>
00015
00016 #include "CTB.hxx"
00017 #include "CTBioState.hxx"
00018
00024
00026
00027 CTBioState::CTBioState(ios& stream)
00028 : m_stream(stream)
00029 {
00030 ml_old_flags = m_stream.flags();
00031 mi_old_precision = -1;
00032 mc_old_fill = 0;
00033 mc_ctype = 0;
00034 }
00035
00036
00038
00039 CTBioState::CTBioState(ios& stream, const char* c_format, int i_precision)
00040 : m_stream(stream)
00041 {
00042 ml_old_flags = m_stream.flags();
00043 mi_old_precision = -1;
00044 mc_old_fill = 0;
00045 SetFormat(c_format, i_precision);
00046 }
00047
00048
00050
00051 CTBioState::~CTBioState()
00052 {
00053 m_stream.flags(ml_old_flags);
00054 if (mi_old_precision >= 0) m_stream.precision(mi_old_precision);
00055 if (mc_old_fill != 0) m_stream.fill(mc_old_fill);
00056 }
00057
00058
00060
00061 void CTBioState::SetFormat(const char* c_format, int i_precision)
00062 {
00063 bool b_plus = false;
00064 bool b_minus = false;
00065 bool b_point = false;
00066 bool b_dollar = false;
00067 bool b_internal = false;
00068 char c_ctype = 0;
00069 char c_fill = 0;
00070 char c;
00071
00072 if (c_format == 0) c_format = "";
00073
00074 for (c = *c_format++; ; c = *c_format++) {
00075 if (c == '+') { b_plus = true; continue;}
00076 if (c == '-') { b_minus = true; continue;}
00077 if (c == '.') { b_point = true; continue;}
00078 if (c == '$') { b_dollar = true; continue;}
00079 break;
00080 }
00081
00082 if (c != 0 && isalpha(c)) { c_ctype = c; c = *c_format++; }
00083 if (c != 0) c_fill = c;
00084
00085 if (i_precision >= 0) {
00086 int i_old_precision = m_stream.precision(i_precision);
00087 if (mi_old_precision < 0) mi_old_precision = i_old_precision;
00088 }
00089 if (c_fill != 0) {
00090 char c_old_fill = m_stream.fill(c_fill);
00091 if (mc_old_fill == 0) mc_old_fill = c_old_fill;
00092 }
00093
00094 mc_ctype = c_ctype;
00095
00096 switch(c_ctype) {
00097 case 'd':
00098 b_internal = !b_minus & (c_fill == '0');
00099 m_stream.setf(ios::dec,ios::basefield);
00100 break;
00101 case 'o':
00102 b_internal = !b_minus & (c_fill == '0');
00103 m_stream.setf(ios::oct,ios::basefield);
00104 break;
00105 case 'x':
00106 case 'X':
00107 b_internal = !b_minus & (c_fill == '0');
00108 m_stream.setf(ios::hex,ios::basefield);
00109 if (isupper(c_ctype)) m_stream.setf(ios::uppercase);
00110 break;
00111 case 'g':
00112 case 'G':
00113 b_internal = !b_minus & (c_fill == '0');
00114 m_stream.setf(0,ios::floatfield);
00115 if (isupper(c_ctype)) m_stream.setf(ios::uppercase);
00116 break;
00117 case 'f':
00118 b_internal = !b_minus & (c_fill == '0');
00119 m_stream.setf(ios::fixed,ios::floatfield);
00120 break;
00121 case 'e':
00122 case 'E':
00123 b_internal = !b_minus & (c_fill == '0');
00124 m_stream.setf(ios::scientific,ios::floatfield);
00125 if (isupper(c_ctype)) m_stream.setf(ios::uppercase);
00126 break;
00127 case 's':
00128 case 'p':
00129 case 'c':
00130 break;
00131 }
00132
00133 {
00134 long l_flags = 0;
00135 if (b_plus) l_flags |= ios::showpos;
00136 if (b_point) l_flags |= ios::showpoint;
00137 if (b_dollar) l_flags |= ios::showbase;
00138 m_stream.setf(l_flags);
00139 m_stream.setf( b_internal ? ios::internal :
00140 b_minus ? ios::left : ios::right, ios::adjustfield);
00141 }
00142 }
00143
00144
00145 #if (defined(CTB__OutLine) || defined(CTBioState__OutLine))
00146 #define inline
00147 #include "CTBioState.icc"
00148 #undef inline
00149 #endif