PBRT
|
00001 00002 // 00003 // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas 00004 // Digital Ltd. LLC 00005 // 00006 // All rights reserved. 00007 // 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are 00010 // met: 00011 // * Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // * Redistributions in binary form must reproduce the above 00014 // copyright notice, this list of conditions and the following disclaimer 00015 // in the documentation and/or other materials provided with the 00016 // distribution. 00017 // * Neither the name of Industrial Light & Magic nor the names of 00018 // its contributors may be used to endorse or promote products derived 00019 // from this software without specific prior written permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00024 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00025 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00027 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00028 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00029 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00030 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00031 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00034 00035 00036 #ifndef INCLUDED_IMF_IO_H 00037 #define INCLUDED_IMF_IO_H 00038 00039 //----------------------------------------------------------------------------- 00040 // 00041 // Low-level file input and output for OpenEXR. 00042 // 00043 //----------------------------------------------------------------------------- 00044 00045 #include <ImfInt64.h> 00046 #include <string> 00047 00048 namespace Imf { 00049 00050 //----------------------------------------------------------- 00051 // class IStream -- an abstract base class for input streams. 00052 //----------------------------------------------------------- 00053 00054 class IStream 00055 { 00056 public: 00057 00058 //----------- 00059 // Destructor 00060 //----------- 00061 00062 virtual ~IStream (); 00063 00064 00065 //------------------------------------------------- 00066 // Does this input stream support memory-mapped IO? 00067 // 00068 // Memory-mapped streams can avoid an extra copy; 00069 // memory-mapped read operations return a pointer 00070 // to an internal buffer instead of copying data 00071 // into a buffer supplied by the caller. 00072 //------------------------------------------------- 00073 00074 virtual bool isMemoryMapped () const; 00075 00076 00077 //------------------------------------------------------ 00078 // Read from the stream: 00079 // 00080 // read(c,n) reads n bytes from the stream, and stores 00081 // them in array c. If the stream contains less than n 00082 // bytes, or if an I/O error occurs, read(c,n) throws 00083 // an exception. If read(c,n) reads the last byte from 00084 // the file it returns false, otherwise it returns true. 00085 //------------------------------------------------------ 00086 00087 virtual bool read (char c[/*n*/], int n) = 0; 00088 00089 00090 //--------------------------------------------------- 00091 // Read from a memory-mapped stream: 00092 // 00093 // readMemoryMapped(n) reads n bytes from the stream 00094 // and returns a pointer to the first byte. The 00095 // returned pointer remains valid until the stream 00096 // is closed. If there are less than n byte left to 00097 // read in the stream or if the stream is not memory- 00098 // mapped, readMemoryMapped(n) throws an exception. 00099 //--------------------------------------------------- 00100 00101 virtual char * readMemoryMapped (int n); 00102 00103 00104 //-------------------------------------------------------- 00105 // Get the current reading position, in bytes from the 00106 // beginning of the file. If the next call to read() will 00107 // read the first byte in the file, tellg() returns 0. 00108 //-------------------------------------------------------- 00109 00110 virtual Int64 tellg () = 0; 00111 00112 00113 //------------------------------------------- 00114 // Set the current reading position. 00115 // After calling seekg(i), tellg() returns i. 00116 //------------------------------------------- 00117 00118 virtual void seekg (Int64 pos) = 0; 00119 00120 00121 //------------------------------------------------------ 00122 // Clear error conditions after an operation has failed. 00123 //------------------------------------------------------ 00124 00125 virtual void clear (); 00126 00127 00128 //------------------------------------------------------ 00129 // Get the name of the file associated with this stream. 00130 //------------------------------------------------------ 00131 00132 const char * fileName () const; 00133 00134 protected: 00135 00136 IStream (const char fileName[]); 00137 00138 private: 00139 00140 IStream (const IStream &); // not implemented 00141 IStream & operator = (const IStream &); // not implemented 00142 00143 std::string _fileName; 00144 }; 00145 00146 00147 //----------------------------------------------------------- 00148 // class OStream -- an abstract base class for output streams 00149 //----------------------------------------------------------- 00150 00151 class OStream 00152 { 00153 public: 00154 00155 //----------- 00156 // Destructor 00157 //----------- 00158 00159 virtual ~OStream (); 00160 00161 00162 //---------------------------------------------------------- 00163 // Write to the stream: 00164 // 00165 // write(c,n) takes n bytes from array c, and stores them 00166 // in the stream. If an I/O error occurs, write(c,n) throws 00167 // an exception. 00168 //---------------------------------------------------------- 00169 00170 virtual void write (const char c[/*n*/], int n) = 0; 00171 00172 00173 //--------------------------------------------------------- 00174 // Get the current writing position, in bytes from the 00175 // beginning of the file. If the next call to write() will 00176 // start writing at the beginning of the file, tellp() 00177 // returns 0. 00178 //--------------------------------------------------------- 00179 00180 virtual Int64 tellp () = 0; 00181 00182 00183 //------------------------------------------- 00184 // Set the current writing position. 00185 // After calling seekp(i), tellp() returns i. 00186 //------------------------------------------- 00187 00188 virtual void seekp (Int64 pos) = 0; 00189 00190 00191 //------------------------------------------------------ 00192 // Get the name of the file associated with this stream. 00193 //------------------------------------------------------ 00194 00195 const char * fileName () const; 00196 00197 protected: 00198 00199 OStream (const char fileName[]); 00200 00201 private: 00202 00203 OStream (const OStream &); // not implemented 00204 OStream & operator = (const OStream &); // not implemented 00205 00206 std::string _fileName; 00207 }; 00208 00209 00210 //----------------------- 00211 // Helper classes for Xdr 00212 //----------------------- 00213 00214 struct StreamIO 00215 { 00216 static void 00217 writeChars (OStream &os, const char c[/*n*/], int n) 00218 { 00219 os.write (c, n); 00220 } 00221 00222 static bool 00223 readChars (IStream &is, char c[/*n*/], int n) 00224 { 00225 return is.read (c, n); 00226 } 00227 }; 00228 00229 00230 struct CharPtrIO 00231 { 00232 static void 00233 writeChars (char *&op, const char c[/*n*/], int n) 00234 { 00235 while (n--) 00236 *op++ = *c++; 00237 } 00238 00239 static bool 00240 readChars (const char *&ip, char c[/*n*/], int n) 00241 { 00242 while (n--) 00243 *c++ = *ip++; 00244 00245 return true; 00246 } 00247 }; 00248 00249 00250 } // namespace Imf 00251 00252 #endif