| 1 | #ifndef __MDFN_STREAM_H |
| 2 | #define __MDFN_STREAM_H |
| 3 | |
| 4 | // TODO/WIP |
| 5 | |
| 6 | // TODO?: BufferedStream, no virtual functions, yes inline functions, constructor takes a Stream* argument. |
| 7 | |
| 8 | #include "mednafen.h" |
| 9 | #include <errno.h> |
| 10 | |
| 11 | #include <stdio.h> // For SEEK_* defines, which we will use in Stream out of FORCE OF HABIT. |
| 12 | |
| 13 | #define MODE_READ 0 |
| 14 | #define MODE_WRITE 1 |
| 15 | #define MODE_WRITE_SAFE 2 |
| 16 | |
| 17 | class Stream |
| 18 | { |
| 19 | public: |
| 20 | |
| 21 | Stream(); |
| 22 | virtual ~Stream(); |
| 23 | |
| 24 | enum |
| 25 | { |
| 26 | ATTRIBUTE_READABLE = 0, |
| 27 | ATTRIBUTE_WRITEABLE, |
| 28 | ATTRIBUTE_SEEKABLE |
| 29 | }; |
| 30 | virtual uint64 attributes(void) = 0; |
| 31 | |
| 32 | virtual uint64 read(void *data, uint64 count, bool error_on_eos = true) = 0; |
| 33 | virtual void write(const void *data, uint64 count) = 0; |
| 34 | |
| 35 | virtual void seek(int64 offset, int whence) = 0; |
| 36 | virtual int64 tell(void) = 0; |
| 37 | virtual int64 size(void) = 0; |
| 38 | virtual void close(void) = 0; // Flushes(in the case of writeable streams) and closes the stream. |
| 39 | // Necessary since this operation can fail(running out of disk space, for instance), |
| 40 | // and throw an exception in the destructor would be a Bad Idea(TM). |
| 41 | // |
| 42 | // Manually calling this function isn't strictly necessary, but recommended when the |
| 43 | // stream is writeable; it will be called automatically from the destructor, with any |
| 44 | // exceptions thrown caught and logged. |
| 45 | |
| 46 | // |
| 47 | // Utility functions(TODO): |
| 48 | // |
| 49 | INLINE uint8 get_u8(void) |
| 50 | { |
| 51 | uint8 ret; |
| 52 | |
| 53 | read(&ret, sizeof(ret)); |
| 54 | |
| 55 | return ret; |
| 56 | } |
| 57 | |
| 58 | INLINE void put_u8(uint8 c) |
| 59 | { |
| 60 | write(&c, sizeof(c)); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | template<typename T> |
| 65 | INLINE T get_NE(void) |
| 66 | { |
| 67 | T ret; |
| 68 | |
| 69 | read(&ret, sizeof(ret)); |
| 70 | |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | template<typename T> |
| 75 | INLINE void put_NE(T c) |
| 76 | { |
| 77 | write(&c, sizeof(c)); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | template<typename T> |
| 82 | INLINE T get_RE(void) |
| 83 | { |
| 84 | uint8 tmp[sizeof(T)]; |
| 85 | T ret = 0; |
| 86 | |
| 87 | read(tmp, sizeof(tmp)); |
| 88 | |
| 89 | for(unsigned i = 0; i < sizeof(T); i++) |
| 90 | ret |= (T)tmp[i] << (i * 8); |
| 91 | |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | template<typename T> |
| 96 | INLINE void put_RE(T c) |
| 97 | { |
| 98 | uint8 tmp[sizeof(T)]; |
| 99 | |
| 100 | for(unsigned i = 0; i < sizeof(T); i++) |
| 101 | tmp[i] = ((uint8 *)&c)[sizeof(T) - 1 - i]; |
| 102 | |
| 103 | write(tmp, sizeof(tmp)); |
| 104 | } |
| 105 | |
| 106 | template<typename T> |
| 107 | INLINE T get_LE(void) |
| 108 | { |
| 109 | #ifdef MSB_FIRST |
| 110 | return get_RE<T>(); |
| 111 | #else |
| 112 | return get_NE<T>(); |
| 113 | #endif |
| 114 | } |
| 115 | |
| 116 | template<typename T> |
| 117 | INLINE void put_LE(T c) |
| 118 | { |
| 119 | #ifdef MSB_FIRST |
| 120 | return put_RE<T>(c); |
| 121 | #else |
| 122 | return put_NE<T>(c); |
| 123 | #endif |
| 124 | } |
| 125 | |
| 126 | template<typename T> |
| 127 | INLINE T get_BE(void) |
| 128 | { |
| 129 | #ifdef MSB_FIRST |
| 130 | return get_NE<T>(); |
| 131 | #else |
| 132 | return get_RE<T>(); |
| 133 | #endif |
| 134 | } |
| 135 | |
| 136 | template<typename T> |
| 137 | INLINE void put_BE(T c) |
| 138 | { |
| 139 | #ifdef MSB_FIRST |
| 140 | return put_NE<T>(c); |
| 141 | #else |
| 142 | return put_RE<T>(c); |
| 143 | #endif |
| 144 | } |
| 145 | |
| 146 | // Reads a line into "str", overwriting its contents; returns the line-end char('\n' or '\r' or '\0'), or -1 on EOF. |
| 147 | // The line-end char won't be added to "str". |
| 148 | // It's up to the caller to handle extraneous empty lines caused by DOS-format text lines(\r\n). |
| 149 | // ("str" is passed by reference for the possibility of improved performance by reusing alloced memory for the std::string, though part |
| 150 | // of it would be up to the STL implementation). |
| 151 | // Implemented as virtual so that a higher-performance version can be implemented if possible(IE with MemoryStream) |
| 152 | virtual int get_line(std::string &str); |
| 153 | }; |
| 154 | #endif |