////////////////////////////////////////////////////////////////////// // $Id: IMsgFormat.h,v 1.1 2011/01/18 05:49:19 finch Exp $ // // IMsgFormat // // A class for handling formatted numerical output to streams. // // messier@huhepl.harvard.edu ////////////////////////////////////////////////////////////////////// #ifndef MSGFORMAT_H #define MSGFORMAT_H #ifndef IOSTREAM #include #define IOSTREAM #endif #ifndef IOMANIP #include #define IOMANIP #endif #ifndef MSGBOUNDFORMAT_H #include #endif using namespace std; // declare function so we can make it a friend ostream& operator<<(ostream&, const IMsgBoundFormat&); //...................................................................... class IMsgFormat { friend ostream& operator<<(ostream&, const IMsgBoundFormat&); public: // These definitions changed from gcc-v2 to gcc-v3 #if __GNUC__ == 3 && __GNUC_MINOR__ >= 4 typedef ios_base::fmtflags fmtflags; #elif __GNUC__ == 4 typedef ios_base::fmtflags fmtflags; #else #ifdef _CPP_BITS_IOSBASE_H typedef ios_base::fmtflags fmtflags; #else typedef int fmtflags; #endif #endif IMsgFormat(int p=6) : prc(p), wdt(0), fmt(static_cast(0)), flc(' ') { } IMsgFormat(int p, int w) : prc(p), wdt(w), fmt(static_cast(0)), flc(' ') { } IMsgFormat(const char* f); IMsgBoundFormat operator()(double d) const { return IMsgBoundFormat(*this,d); } IMsgFormat& fixed() { fmt=ios::fixed; return *this;} IMsgFormat& general() { fmt=ios::dec; return *this;} IMsgFormat& hex() { fmt=ios::hex; return *this;} IMsgFormat& oct() { fmt=ios::oct; return *this;} IMsgFormat& scientific() { fmt=ios::scientific; return *this;} IMsgFormat& precision(int p) { prc=p; return *this;} IMsgFormat& width(int w) { wdt=w; return *this;} IMsgFormat& set_fill(char c) { flc = c; return *this; } IMsgFormat& left_justify(int b=1) { if (b) { fmt |= ios::left; fmt &= (~ios::right); } else fmt &= ~ios::left; return *this; } IMsgFormat& right_justify(int b=1) { if (b) { fmt |= ios::right; fmt &= (~ios::left); } else fmt &= ~ios::right; return *this; } IMsgFormat& show_base(int b=1) { if (b) fmt |= ios::showbase; else fmt &= ~ios::showbase; return *this; } IMsgFormat& plus(int b=1) { if (b) fmt |= ios::showpos; else fmt &= ~ios::showpos; return *this; } IMsgFormat& trailing_zeros(int b=1) { if (b) fmt |= ios::showpoint; else fmt &= ~ios::showpoint; return *this; } private: int prc; // Precision int wdt; // Width fmtflags fmt; // Format char flc; // Fill character }; #endif // MSGFORMAT_H