|
-
- #ifndef ostream_h
- #define ostream_h
-
- #include "ios.h"
-
-
- #define pstr(str) pgm(PSTR(str))
-
- struct pgm {
-
- char *ptr;
-
-
- explicit pgm(char* str) : ptr(str) {}
-
-
- explicit pgm(const char *str) : ptr(const_cast<char*>(str)) {}
- };
-
-
- class ostream : public virtual ios {
- public:
- ostream() {}
-
-
-
- ostream& operator<< (ostream& (*pf)(ostream& str)) {
- return pf(*this);
- }
-
-
- ostream& operator<< (ios_base& (*pf)(ios_base& str)) {
- pf(*this);
- return *this;
- }
-
-
- ostream &operator<< (bool arg) {
- putBool(arg);
- return *this;
- }
-
-
- ostream &operator<< (const char *arg) {
- putStr(arg);
- return *this;
- }
-
-
- ostream &operator<< (const signed char *arg) {
- putStr((const char*)arg);
- return *this;
- }
-
-
- ostream &operator<< (const unsigned char *arg) {
- putStr((const char*)arg);
- return *this;
- }
-
-
- ostream &operator<< (char arg) {
- putChar(arg);
- return *this;
- }
-
-
- ostream &operator<< (signed char arg) {
- putChar(static_cast<char>(arg));
- return *this;
- }
-
-
- ostream &operator<< (unsigned char arg) {
- putChar(static_cast<char>(arg));
- return *this;
- }
-
-
- ostream &operator<< (double arg) {
- putDouble(arg);
- return *this;
- }
-
-
- ostream &operator<< (float arg) {
- putDouble(arg);
- return *this;
- }
-
-
- ostream &operator<< (short arg) {
- putNum((int32_t)arg);
- return *this;
- }
-
-
- ostream &operator<< (unsigned short arg) {
- putNum((uint32_t)arg);
- return *this;
- }
-
-
- ostream &operator<< (int arg) {
- putNum((int32_t)arg);
- return *this;
- }
-
-
- ostream &operator<< (unsigned int arg) {
- putNum((uint32_t)arg);
- return *this;
- }
-
-
- ostream &operator<< (long arg) {
- putNum((int32_t)arg);
- return *this;
- }
-
-
- ostream &operator<< (unsigned long arg) {
- putNum((uint32_t)arg);
- return *this;
- }
-
-
- ostream& operator<< (const void* arg) {
- putNum(reinterpret_cast<uint32_t>(arg));
- return *this;
- }
-
-
- ostream &operator<< (pgm arg) {
- putPgm(arg.ptr);
- return *this;
- }
-
-
- ostream &operator<< (const __FlashStringHelper *arg) {
- putPgm(reinterpret_cast<const char*>(arg));
- return *this;
- }
-
-
- ostream& put(char ch) {
- putch(ch);
- return *this;
- }
-
-
-
- ostream& flush() {
- if (!sync()) setstate(badbit);
- return *this;
- }
-
-
- pos_type tellp() {return tellpos();}
-
-
- ostream& seekp(pos_type pos) {
- if (!seekpos(pos)) setstate(failbit);
- return *this;
- }
-
-
- ostream& seekp(off_type off, seekdir way) {
- if (!seekoff(off, way)) setstate(failbit);
- return *this;
- }
-
- protected:
-
-
-
- virtual void putch(char ch) = 0;
- virtual void putstr(const char *str) = 0;
- virtual bool seekoff(off_type pos, seekdir way) = 0;
- virtual bool seekpos(pos_type pos) = 0;
- virtual bool sync() = 0;
-
- virtual pos_type tellpos() = 0;
-
- private:
- void do_fill(unsigned len);
- void fill_not_left(unsigned len);
- char* fmtNum(uint32_t n, char *ptr, uint8_t base);
- void putBool(bool b);
- void putChar(char c);
- void putDouble(double n);
- void putNum(uint32_t n, bool neg = false);
- void putNum(int32_t n);
- void putPgm(const char* str);
- void putStr(const char* str);
- };
- #endif
|