///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////333435#ifndef INCLUDED_IMF_IO_H36#define INCLUDED_IMF_IO_H3738//-----------------------------------------------------------------------------39//40// Low-level file input and output for OpenEXR.41//42//-----------------------------------------------------------------------------4344#include <ImfInt64.h>45#include <string>4647namespace Imf {4849//-----------------------------------------------------------50// class IStream -- an abstract base class for input streams.51//-----------------------------------------------------------5253class IStream54{55public:5657//-----------58// Destructor59//-----------6061virtual ~IStream ();626364//-------------------------------------------------65// Does this input stream support memory-mapped IO?66//67// Memory-mapped streams can avoid an extra copy;68// memory-mapped read operations return a pointer69// to an internal buffer instead of copying data70// into a buffer supplied by the caller.71//-------------------------------------------------7273virtual bool isMemoryMapped () const;747576//------------------------------------------------------77// Read from the stream:78//79// read(c,n) reads n bytes from the stream, and stores80// them in array c. If the stream contains less than n81// bytes, or if an I/O error occurs, read(c,n) throws82// an exception. If read(c,n) reads the last byte from83// the file it returns false, otherwise it returns true.84//------------------------------------------------------8586virtual bool read (char c[/*n*/], int n) = 0;878889//---------------------------------------------------90// Read from a memory-mapped stream:91//92// readMemoryMapped(n) reads n bytes from the stream93// and returns a pointer to the first byte. The94// returned pointer remains valid until the stream95// is closed. If there are less than n byte left to96// read in the stream or if the stream is not memory-97// mapped, readMemoryMapped(n) throws an exception.98//---------------------------------------------------99100virtual char * readMemoryMapped (int n);101102103//--------------------------------------------------------104// Get the current reading position, in bytes from the105// beginning of the file. If the next call to read() will106// read the first byte in the file, tellg() returns 0.107//--------------------------------------------------------108109virtual Int64 tellg () = 0;110111112//-------------------------------------------113// Set the current reading position.114// After calling seekg(i), tellg() returns i.115//-------------------------------------------116117virtual void seekg (Int64 pos) = 0;118119120//------------------------------------------------------121// Clear error conditions after an operation has failed.122//------------------------------------------------------123124virtual void clear ();125126127//------------------------------------------------------128// Get the name of the file associated with this stream.129//------------------------------------------------------130131const char * fileName () const;132133protected:134135IStream (const char fileName[]);136137private:138139IStream (const IStream &); // not implemented140IStream & operator = (const IStream &); // not implemented141142std::string _fileName;143};144145146//-----------------------------------------------------------147// class OStream -- an abstract base class for output streams148//-----------------------------------------------------------149150class OStream151{152public:153154//-----------155// Destructor156//-----------157158virtual ~OStream ();159160161//----------------------------------------------------------162// Write to the stream:163//164// write(c,n) takes n bytes from array c, and stores them165// in the stream. If an I/O error occurs, write(c,n) throws166// an exception.167//----------------------------------------------------------168169virtual void write (const char c[/*n*/], int n) = 0;170171172//---------------------------------------------------------173// Get the current writing position, in bytes from the174// beginning of the file. If the next call to write() will175// start writing at the beginning of the file, tellp()176// returns 0.177//---------------------------------------------------------178179virtual Int64 tellp () = 0;180181182//-------------------------------------------183// Set the current writing position.184// After calling seekp(i), tellp() returns i.185//-------------------------------------------186187virtual void seekp (Int64 pos) = 0;188189190//------------------------------------------------------191// Get the name of the file associated with this stream.192//------------------------------------------------------193194const char * fileName () const;195196protected:197198OStream (const char fileName[]);199200private:201202OStream (const OStream &); // not implemented203OStream & operator = (const OStream &); // not implemented204205std::string _fileName;206};207208209//-----------------------210// Helper classes for Xdr211//-----------------------212213struct StreamIO214{215static void216writeChars (OStream &os, const char c[/*n*/], int n)217{218os.write (c, n);219}220221static bool222readChars (IStream &is, char c[/*n*/], int n)223{224return is.read (c, n);225}226};227228229struct CharPtrIO230{231static void232writeChars (char *&op, const char c[/*n*/], int n)233{234while (n--)235*op++ = *c++;236}237238static bool239readChars (const char *&ip, char c[/*n*/], int n)240{241while (n--)242*c++ = *ip++;243244return true;245}246};247248249} // namespace Imf250251#endif252253254