| 1 | /////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas |
| 4 | // Digital Ltd. LLC |
| 5 | // |
| 6 | // All rights reserved. |
| 7 | // |
| 8 | // Redistribution and use in source and binary forms, with or without |
| 9 | // modification, are permitted provided that the following conditions are |
| 10 | // met: |
| 11 | // * Redistributions of source code must retain the above copyright |
| 12 | // notice, this list of conditions and the following disclaimer. |
| 13 | // * Redistributions in binary form must reproduce the above |
| 14 | // copyright notice, this list of conditions and the following disclaimer |
| 15 | // in the documentation and/or other materials provided with the |
| 16 | // distribution. |
| 17 | // * Neither the name of Industrial Light & Magic nor the names of |
| 18 | // its contributors may be used to endorse or promote products derived |
| 19 | // from this software without specific prior written permission. |
| 20 | // |
| 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | // |
| 33 | /////////////////////////////////////////////////////////////////////////// |
| 34 | |
| 35 | |
| 36 | #ifndef INCLUDED_IMF_IO_H |
| 37 | #define INCLUDED_IMF_IO_H |
| 38 | |
| 39 | //----------------------------------------------------------------------------- |
| 40 | // |
| 41 | // Low-level file input and output for OpenEXR. |
| 42 | // |
| 43 | //----------------------------------------------------------------------------- |
| 44 | |
| 45 | #include "ImfInt64.h" |
| 46 | #include "ImfNamespace.h" |
| 47 | #include "ImfExport.h" |
| 48 | |
| 49 | #include <string> |
| 50 | |
| 51 | |
| 52 | OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER |
| 53 | |
| 54 | //----------------------------------------------------------- |
| 55 | // class IStream -- an abstract base class for input streams. |
| 56 | //----------------------------------------------------------- |
| 57 | |
| 58 | class IStream |
| 59 | { |
| 60 | public: |
| 61 | |
| 62 | //----------- |
| 63 | // Destructor |
| 64 | //----------- |
| 65 | |
| 66 | IMF_EXPORT |
| 67 | virtual ~IStream (); |
| 68 | |
| 69 | |
| 70 | //------------------------------------------------- |
| 71 | // Does this input stream support memory-mapped IO? |
| 72 | // |
| 73 | // Memory-mapped streams can avoid an extra copy; |
| 74 | // memory-mapped read operations return a pointer |
| 75 | // to an internal buffer instead of copying data |
| 76 | // into a buffer supplied by the caller. |
| 77 | //------------------------------------------------- |
| 78 | |
| 79 | IMF_EXPORT |
| 80 | virtual bool isMemoryMapped () const; |
| 81 | |
| 82 | |
| 83 | //------------------------------------------------------ |
| 84 | // Read from the stream: |
| 85 | // |
| 86 | // read(c,n) reads n bytes from the stream, and stores |
| 87 | // them in array c. If the stream contains less than n |
| 88 | // bytes, or if an I/O error occurs, read(c,n) throws |
| 89 | // an exception. If read(c,n) reads the last byte from |
| 90 | // the file it returns false, otherwise it returns true. |
| 91 | //------------------------------------------------------ |
| 92 | |
| 93 | virtual bool read (char c[/*n*/], int n) = 0; |
| 94 | |
| 95 | |
| 96 | //--------------------------------------------------- |
| 97 | // Read from a memory-mapped stream: |
| 98 | // |
| 99 | // readMemoryMapped(n) reads n bytes from the stream |
| 100 | // and returns a pointer to the first byte. The |
| 101 | // returned pointer remains valid until the stream |
| 102 | // is closed. If there are less than n byte left to |
| 103 | // read in the stream or if the stream is not memory- |
| 104 | // mapped, readMemoryMapped(n) throws an exception. |
| 105 | //--------------------------------------------------- |
| 106 | |
| 107 | IMF_EXPORT |
| 108 | virtual char * readMemoryMapped (int n); |
| 109 | |
| 110 | |
| 111 | //-------------------------------------------------------- |
| 112 | // Get the current reading position, in bytes from the |
| 113 | // beginning of the file. If the next call to read() will |
| 114 | // read the first byte in the file, tellg() returns 0. |
| 115 | //-------------------------------------------------------- |
| 116 | |
| 117 | virtual Int64 tellg () = 0; |
| 118 | |
| 119 | |
| 120 | //------------------------------------------- |
| 121 | // Set the current reading position. |
| 122 | // After calling seekg(i), tellg() returns i. |
| 123 | //------------------------------------------- |
| 124 | |
| 125 | virtual void seekg (Int64 pos) = 0; |
| 126 | |
| 127 | |
| 128 | //------------------------------------------------------ |
| 129 | // Clear error conditions after an operation has failed. |
| 130 | //------------------------------------------------------ |
| 131 | |
| 132 | IMF_EXPORT |
| 133 | virtual void clear (); |
| 134 | |
| 135 | |
| 136 | //------------------------------------------------------ |
| 137 | // Get the name of the file associated with this stream. |
| 138 | //------------------------------------------------------ |
| 139 | |
| 140 | IMF_EXPORT |
| 141 | const char * fileName () const; |
| 142 | |
| 143 | protected: |
| 144 | |
| 145 | IMF_EXPORT |
| 146 | IStream (const char fileName[]); |
| 147 | |
| 148 | private: |
| 149 | |
| 150 | IStream (const IStream &) = delete; |
| 151 | IStream & operator = (const IStream &) = delete; |
| 152 | IStream (IStream &&) = delete; |
| 153 | IStream & operator = (IStream &&) = delete; |
| 154 | |
| 155 | std::string _fileName; |
| 156 | }; |
| 157 | |
| 158 | |
| 159 | //----------------------------------------------------------- |
| 160 | // class OStream -- an abstract base class for output streams |
| 161 | //----------------------------------------------------------- |
| 162 | |
| 163 | class OStream |
| 164 | { |
| 165 | public: |
| 166 | |
| 167 | //----------- |
| 168 | // Destructor |
| 169 | //----------- |
| 170 | |
| 171 | IMF_EXPORT |
| 172 | virtual ~OStream (); |
| 173 | |
| 174 | |
| 175 | //---------------------------------------------------------- |
| 176 | // Write to the stream: |
| 177 | // |
| 178 | // write(c,n) takes n bytes from array c, and stores them |
| 179 | // in the stream. If an I/O error occurs, write(c,n) throws |
| 180 | // an exception. |
| 181 | //---------------------------------------------------------- |
| 182 | |
| 183 | virtual void write (const char c[/*n*/], int n) = 0; |
| 184 | |
| 185 | |
| 186 | //--------------------------------------------------------- |
| 187 | // Get the current writing position, in bytes from the |
| 188 | // beginning of the file. If the next call to write() will |
| 189 | // start writing at the beginning of the file, tellp() |
| 190 | // returns 0. |
| 191 | //--------------------------------------------------------- |
| 192 | |
| 193 | virtual Int64 tellp () = 0; |
| 194 | |
| 195 | |
| 196 | //------------------------------------------- |
| 197 | // Set the current writing position. |
| 198 | // After calling seekp(i), tellp() returns i. |
| 199 | //------------------------------------------- |
| 200 | |
| 201 | virtual void seekp (Int64 pos) = 0; |
| 202 | |
| 203 | |
| 204 | //------------------------------------------------------ |
| 205 | // Get the name of the file associated with this stream. |
| 206 | //------------------------------------------------------ |
| 207 | |
| 208 | IMF_EXPORT |
| 209 | const char * fileName () const; |
| 210 | |
| 211 | protected: |
| 212 | |
| 213 | IMF_EXPORT |
| 214 | OStream (const char fileName[]); |
| 215 | |
| 216 | private: |
| 217 | |
| 218 | OStream (const OStream &) = delete; |
| 219 | OStream & operator = (const OStream &) = delete; |
| 220 | OStream (OStream &&) = delete; |
| 221 | OStream & operator = (OStream &&) = delete; |
| 222 | |
| 223 | std::string _fileName; |
| 224 | }; |
| 225 | |
| 226 | |
| 227 | //----------------------- |
| 228 | // Helper classes for Xdr |
| 229 | //----------------------- |
| 230 | |
| 231 | struct StreamIO |
| 232 | { |
| 233 | static void |
| 234 | writeChars (OStream &os, const char c[/*n*/], int n) |
| 235 | { |
| 236 | os.write (c, n); |
| 237 | } |
| 238 | |
| 239 | static bool |
| 240 | readChars (IStream &is, char c[/*n*/], int n) |
| 241 | { |
| 242 | return is.read (c, n); |
| 243 | } |
| 244 | }; |
| 245 | |
| 246 | |
| 247 | struct CharPtrIO |
| 248 | { |
| 249 | static void |
| 250 | writeChars (char *&op, const char c[/*n*/], int n) |
| 251 | { |
| 252 | while (n--) |
| 253 | *op++ = *c++; |
| 254 | } |
| 255 | |
| 256 | static bool |
| 257 | readChars (const char *&ip, char c[/*n*/], int n) |
| 258 | { |
| 259 | while (n--) |
| 260 | *c++ = *ip++; |
| 261 | |
| 262 | return true; |
| 263 | } |
| 264 | }; |
| 265 | |
| 266 | OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT |
| 267 | |
| 268 | #endif |
| 269 | |