Advanced Computing Platform for Theoretical Physics

commit大文件会使得服务器变得不稳定,请大家尽量只commit代码,不要commit大的文件。

xstream 4.5 KB
Newer Older
Yu-Chen Ding's avatar
Yu-Chen Ding committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
#pragma once
#include <fstream>
#include <string>
#include <vector>
#include <valarray>

// Data is always stored little_endian
#if BYTE_ORDER == BIG_ENDIAN
#define OXSTREAM_NUMERIC_WRITE write_reverse
#define IXSTREAM_NUMERIC_READ read_reverse
#else
#define OXSTREAM_NUMERIC_WRITE write
#define IXSTREAM_NUMERIC_READ read
#endif

namespace yuc {
class oxstream : protected std::ofstream {
  protected:
    static constexpr size_t cell_witdh = 8;

  public:
    using std::ofstream::ofstream;

    // Expose std::ofstream
    using std::ofstream::close;
    using std::ofstream::is_open;
    using std::ofstream::open;
    using std::ofstream::operator=;
    // Expose std::ostream (none)
    // Expose std::ios
    using std::ofstream::bad;
    using std::ofstream::eof;
    using std::ofstream::fail;
    using std::ofstream::good;
    using std::ofstream::operator!;
    using std::ofstream::operator bool;
    using std::ofstream::clear;
    using std::ofstream::rdstate;
    using std::ofstream::setstate;
    using std::ofstream::tie;

    void swap(oxstream& x) { std::ofstream::swap(x); };

  protected:
    using std::ofstream::write;
    oxstream& write_reverse(const char* s, const std::streamsize n) {
        for (const char* c = s + n; c-- != s; this->put(*c))
            ;
        return *this;
    }

  public:
    oxstream& operator<<(double d) {
        OXSTREAM_NUMERIC_WRITE((char*)&d, cell_witdh);
        return *this;
    }
    oxstream& operator<<(size_t n) {
        OXSTREAM_NUMERIC_WRITE((char*)&n, cell_witdh);
        return *this;
    }
    oxstream& operator<<(long l) {
        OXSTREAM_NUMERIC_WRITE((char*)&l, cell_witdh);
        return *this;
    }
    oxstream& operator<<(const char* s) {
        const char* p = s;
        size_t i      = 0;
        while (i < cell_witdh && *p) {
            ++i;
            ++p;
        }
        write(s, i);
        i = cell_witdh - i;
        while (i--) {
            put('\0');
        }
        return *this;
    }
    oxstream& operator<<(const std::string& s) { return *this << s.c_str(); }
    template <typename T> oxstream& operator<<(const std::vector<T>& v) {
        this->operator<<(v.size());
        for (auto it = v.begin(); it != v.end(); ++it) {
            this->operator<<(*it);
        }
        return *this;
    }
    template <typename T> oxstream& operator<<(const std::valarray<T>& v) {
        this->operator<<(v.size());
        for (auto it = v.begin(); it != v.end(); ++it) {
            this->operator<<(*it);
        }
        return *this;
    }
};

class ixstream : protected std::ifstream {
  protected:
    static constexpr size_t cell_witdh = 8;

  public:
    using std::ifstream::ifstream;

    using std::ifstream::close;
    using std::ifstream::is_open;
    using std::ifstream::open;
    using std::ifstream::operator=;

    using std::ifstream::bad;
    using std::ifstream::eof;
    using std::ifstream::fail;
    using std::ifstream::good;
    using std::ifstream::operator!;
    using std::ifstream::operator bool;
    using std::ifstream::clear;
    using std::ifstream::rdstate;
    using std::ifstream::setstate;
    using std::ifstream::tie;

    void swap(ixstream& x) { std::ifstream::swap(x); };

  protected:
    using std::ifstream::read;
    ixstream& read_reverse(char* s, const std::streamsize n) {
        for (char* c = s + n; c-- != s; this->get(*c))
            ;
        return *this;
    }

  public:
    ixstream& operator>>(double& d) {
        IXSTREAM_NUMERIC_READ((char*)&d, cell_witdh);
        return *this;
    }
    ixstream& operator>>(size_t& n) {
        IXSTREAM_NUMERIC_READ((char*)&n, cell_witdh);
        return *this;
    }
    ixstream& operator>>(long& l) {
        IXSTREAM_NUMERIC_READ((char*)&l, cell_witdh);
        return *this;
    }
    ixstream& operator>>(std::string& s) {
        char cstr[cell_witdh + 1] = {};
        this->read(cstr, cell_witdh);
        s = cstr;
        return *this;
    }
    template <typename T> ixstream& operator>>(std::vector<T>& v) {
        size_t n;
        this->operator>>(n);
        v.resize(n);
        for (auto it = v.begin(); it != v.end(); ++it) {
            this->operator>>(*it);
        }
        return *this;
    }
    template <typename T> ixstream& operator>>(std::valarray<T>& v) {
        size_t n;
        this->operator>>(n);
        v.resize(n);
        for (auto it = std::begin(v); it != std::end(v); ++it) {
            this->operator>>(*it);
        }
        return *this;
    }
};

}; // namespace yuc

#undef OXSTREAM_NUMERIC_WRITE
#undef IXSTREAM_NUMERIC_READ

// vi:ft=cpp