| 1 | // |
| 2 | // SPDX-License-Identifier: BSD-3-Clause |
| 3 | // Copyright (c) Contributors to the OpenEXR Project. |
| 4 | // |
| 5 | |
| 6 | #ifndef INCLUDED_IMF_IO_H |
| 7 | #define INCLUDED_IMF_IO_H |
| 8 | |
| 9 | //----------------------------------------------------------------------------- |
| 10 | // |
| 11 | // Low-level file input and output for OpenEXR. |
| 12 | // |
| 13 | //----------------------------------------------------------------------------- |
| 14 | |
| 15 | #include "ImfForward.h" |
| 16 | |
| 17 | #include <string> |
| 18 | #include <cstdint> |
| 19 | |
| 20 | OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER |
| 21 | |
| 22 | //----------------------------------------------------------- |
| 23 | // class IStream -- an abstract base class for input streams. |
| 24 | //----------------------------------------------------------- |
| 25 | |
| 26 | class IMF_EXPORT_TYPE IStream |
| 27 | { |
| 28 | public: |
| 29 | |
| 30 | //----------- |
| 31 | // Destructor |
| 32 | //----------- |
| 33 | |
| 34 | IMF_EXPORT virtual ~IStream (); |
| 35 | |
| 36 | |
| 37 | //------------------------------------------------- |
| 38 | // Does this input stream support memory-mapped IO? |
| 39 | // |
| 40 | // Memory-mapped streams can avoid an extra copy; |
| 41 | // memory-mapped read operations return a pointer |
| 42 | // to an internal buffer instead of copying data |
| 43 | // into a buffer supplied by the caller. |
| 44 | //------------------------------------------------- |
| 45 | |
| 46 | IMF_EXPORT virtual bool isMemoryMapped () const; |
| 47 | |
| 48 | |
| 49 | //------------------------------------------------------ |
| 50 | // Read from the stream: |
| 51 | // |
| 52 | // read(c,n) reads n bytes from the stream, and stores |
| 53 | // them in array c. If the stream contains less than n |
| 54 | // bytes, or if an I/O error occurs, read(c,n) throws |
| 55 | // an exception. If read(c,n) reads the last byte from |
| 56 | // the file it returns false, otherwise it returns true. |
| 57 | //------------------------------------------------------ |
| 58 | |
| 59 | virtual bool read (char c[/*n*/], int n) = 0; |
| 60 | |
| 61 | |
| 62 | //--------------------------------------------------- |
| 63 | // Read from a memory-mapped stream: |
| 64 | // |
| 65 | // readMemoryMapped(n) reads n bytes from the stream |
| 66 | // and returns a pointer to the first byte. The |
| 67 | // returned pointer remains valid until the stream |
| 68 | // is closed. If there are less than n byte left to |
| 69 | // read in the stream or if the stream is not memory- |
| 70 | // mapped, readMemoryMapped(n) throws an exception. |
| 71 | //--------------------------------------------------- |
| 72 | |
| 73 | IMF_EXPORT virtual char * readMemoryMapped (int n); |
| 74 | |
| 75 | |
| 76 | //-------------------------------------------------------- |
| 77 | // Get the current reading position, in bytes from the |
| 78 | // beginning of the file. If the next call to read() will |
| 79 | // read the first byte in the file, tellg() returns 0. |
| 80 | //-------------------------------------------------------- |
| 81 | |
| 82 | virtual uint64_t tellg () = 0; |
| 83 | |
| 84 | |
| 85 | //------------------------------------------- |
| 86 | // Set the current reading position. |
| 87 | // After calling seekg(i), tellg() returns i. |
| 88 | //------------------------------------------- |
| 89 | |
| 90 | virtual void seekg (uint64_t pos) = 0; |
| 91 | |
| 92 | |
| 93 | //------------------------------------------------------ |
| 94 | // Clear error conditions after an operation has failed. |
| 95 | //------------------------------------------------------ |
| 96 | |
| 97 | IMF_EXPORT virtual void clear (); |
| 98 | |
| 99 | |
| 100 | //------------------------------------------------------ |
| 101 | // Get the name of the file associated with this stream. |
| 102 | //------------------------------------------------------ |
| 103 | |
| 104 | IMF_EXPORT const char * fileName () const; |
| 105 | |
| 106 | protected: |
| 107 | |
| 108 | IMF_EXPORT IStream (const char fileName[]); |
| 109 | |
| 110 | private: |
| 111 | |
| 112 | IStream (const IStream &) = delete; |
| 113 | IStream & operator = (const IStream &) = delete; |
| 114 | IStream (IStream &&) = delete; |
| 115 | IStream & operator = (IStream &&) = delete; |
| 116 | |
| 117 | std::string _fileName; |
| 118 | }; |
| 119 | |
| 120 | |
| 121 | //----------------------------------------------------------- |
| 122 | // class OStream -- an abstract base class for output streams |
| 123 | //----------------------------------------------------------- |
| 124 | |
| 125 | class IMF_EXPORT_TYPE OStream |
| 126 | { |
| 127 | public: |
| 128 | |
| 129 | //----------- |
| 130 | // Destructor |
| 131 | //----------- |
| 132 | |
| 133 | IMF_EXPORT virtual ~OStream (); |
| 134 | |
| 135 | |
| 136 | //---------------------------------------------------------- |
| 137 | // Write to the stream: |
| 138 | // |
| 139 | // write(c,n) takes n bytes from array c, and stores them |
| 140 | // in the stream. If an I/O error occurs, write(c,n) throws |
| 141 | // an exception. |
| 142 | //---------------------------------------------------------- |
| 143 | |
| 144 | virtual void write (const char c[/*n*/], int n) = 0; |
| 145 | |
| 146 | |
| 147 | //--------------------------------------------------------- |
| 148 | // Get the current writing position, in bytes from the |
| 149 | // beginning of the file. If the next call to write() will |
| 150 | // start writing at the beginning of the file, tellp() |
| 151 | // returns 0. |
| 152 | //--------------------------------------------------------- |
| 153 | |
| 154 | virtual uint64_t tellp () = 0; |
| 155 | |
| 156 | |
| 157 | //------------------------------------------- |
| 158 | // Set the current writing position. |
| 159 | // After calling seekp(i), tellp() returns i. |
| 160 | //------------------------------------------- |
| 161 | |
| 162 | virtual void seekp (uint64_t pos) = 0; |
| 163 | |
| 164 | |
| 165 | //------------------------------------------------------ |
| 166 | // Get the name of the file associated with this stream. |
| 167 | //------------------------------------------------------ |
| 168 | |
| 169 | IMF_EXPORT const char * fileName () const; |
| 170 | |
| 171 | protected: |
| 172 | |
| 173 | IMF_EXPORT OStream (const char fileName[]); |
| 174 | |
| 175 | private: |
| 176 | |
| 177 | OStream (const OStream &) = delete; |
| 178 | OStream & operator = (const OStream &) = delete; |
| 179 | OStream (OStream &&) = delete; |
| 180 | OStream & operator = (OStream &&) = delete; |
| 181 | |
| 182 | std::string _fileName; |
| 183 | }; |
| 184 | |
| 185 | |
| 186 | //----------------------- |
| 187 | // Helper classes for Xdr |
| 188 | //----------------------- |
| 189 | |
| 190 | struct StreamIO |
| 191 | { |
| 192 | static inline void |
| 193 | writeChars (OStream &os, const char c[/*n*/], int n) |
| 194 | { |
| 195 | os.write (c, n); |
| 196 | } |
| 197 | |
| 198 | static inline bool |
| 199 | readChars (IStream &is, char c[/*n*/], int n) |
| 200 | { |
| 201 | return is.read (c, n); |
| 202 | } |
| 203 | }; |
| 204 | |
| 205 | |
| 206 | struct CharPtrIO |
| 207 | { |
| 208 | static inline void |
| 209 | writeChars (char *&op, const char c[/*n*/], int n) |
| 210 | { |
| 211 | while (n--) |
| 212 | *op++ = *c++; |
| 213 | } |
| 214 | |
| 215 | static inline bool |
| 216 | readChars (const char *&ip, char c[/*n*/], int n) |
| 217 | { |
| 218 | while (n--) |
| 219 | *c++ = *ip++; |
| 220 | |
| 221 | return true; |
| 222 | } |
| 223 | }; |
| 224 | |
| 225 | OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT |
| 226 | |
| 227 | #endif |
| 228 | |