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 <string>
47
48namespace Imf {
49
50//-----------------------------------------------------------
51// class IStream -- an abstract base class for input streams.
52//-----------------------------------------------------------
53
54class IStream
55{
56  public:
57
58    //-----------
59    // Destructor
60    //-----------
61
62    virtual ~IStream ();
63
64
65    //-------------------------------------------------
66    // Does this input stream support memory-mapped IO?
67    //
68    // Memory-mapped streams can avoid an extra copy;
69    // memory-mapped read operations return a pointer
70    // to an internal buffer instead of copying data
71    // into a buffer supplied by the caller.
72    //-------------------------------------------------
73
74    virtual bool        isMemoryMapped () const;
75
76
77    //------------------------------------------------------
78    // Read from the stream:
79    //
80    // read(c,n) reads n bytes from the stream, and stores
81    // them in array c.  If the stream contains less than n
82    // bytes, or if an I/O error occurs, read(c,n) throws
83    // an exception.  If read(c,n) reads the last byte from
84    // the file it returns false, otherwise it returns true.
85    //------------------------------------------------------
86
87    virtual bool	read (char c[/*n*/], int n) = 0;
88
89
90    //---------------------------------------------------
91    // Read from a memory-mapped stream:
92    //
93    // readMemoryMapped(n) reads n bytes from the stream
94    // and returns a pointer to the first byte.  The
95    // returned pointer remains valid until the stream
96    // is closed.  If there are less than n byte left to
97    // read in the stream or if the stream is not memory-
98    // mapped, readMemoryMapped(n) throws an exception.
99    //---------------------------------------------------
100
101    virtual char *	readMemoryMapped (int n);
102
103
104    //--------------------------------------------------------
105    // Get the current reading position, in bytes from the
106    // beginning of the file.  If the next call to read() will
107    // read the first byte in the file, tellg() returns 0.
108    //--------------------------------------------------------
109
110    virtual Int64	tellg () = 0;
111
112
113    //-------------------------------------------
114    // Set the current reading position.
115    // After calling seekg(i), tellg() returns i.
116    //-------------------------------------------
117
118    virtual void	seekg (Int64 pos) = 0;
119
120
121    //------------------------------------------------------
122    // Clear error conditions after an operation has failed.
123    //------------------------------------------------------
124
125    virtual void	clear ();
126
127
128    //------------------------------------------------------
129    // Get the name of the file associated with this stream.
130    //------------------------------------------------------
131
132    const char *	fileName () const;
133
134  protected:
135
136    IStream (const char fileName[]);
137
138  private:
139
140    IStream (const IStream &);			// not implemented
141    IStream & operator = (const IStream &);	// not implemented
142
143    std::string		_fileName;
144};
145
146
147//-----------------------------------------------------------
148// class OStream -- an abstract base class for output streams
149//-----------------------------------------------------------
150
151class OStream
152{
153  public:
154
155    //-----------
156    // Destructor
157    //-----------
158
159    virtual ~OStream ();
160
161
162    //----------------------------------------------------------
163    // Write to the stream:
164    //
165    // write(c,n) takes n bytes from array c, and stores them
166    // in the stream.  If an I/O error occurs, write(c,n) throws
167    // an exception.
168    //----------------------------------------------------------
169
170    virtual void	write (const char c[/*n*/], int n) = 0;
171
172
173    //---------------------------------------------------------
174    // Get the current writing position, in bytes from the
175    // beginning of the file.  If the next call to write() will
176    // start writing at the beginning of the file, tellp()
177    // returns 0.
178    //---------------------------------------------------------
179
180    virtual Int64	tellp () = 0;
181
182
183    //-------------------------------------------
184    // Set the current writing position.
185    // After calling seekp(i), tellp() returns i.
186    //-------------------------------------------
187
188    virtual void	seekp (Int64 pos) = 0;
189
190
191    //------------------------------------------------------
192    // Get the name of the file associated with this stream.
193    //------------------------------------------------------
194
195    const char *	fileName () const;
196
197  protected:
198
199    OStream (const char fileName[]);
200
201  private:
202
203    OStream (const OStream &);			// not implemented
204    OStream & operator = (const OStream &);	// not implemented
205
206    std::string		_fileName;
207};
208
209
210//-----------------------
211// Helper classes for Xdr
212//-----------------------
213
214struct StreamIO
215{
216    static void
217    writeChars (OStream &os, const char c[/*n*/], int n)
218    {
219	os.write (c, n);
220    }
221
222    static bool
223    readChars (IStream &is, char c[/*n*/], int n)
224    {
225	return is.read (c, n);
226    }
227};
228
229
230struct CharPtrIO
231{
232    static void
233    writeChars (char *&op, const char c[/*n*/], int n)
234    {
235	while (n--)
236	    *op++ = *c++;
237    }
238
239    static bool
240    readChars (const char *&ip, char c[/*n*/], int n)
241    {
242	while (n--)
243	    *c++ = *ip++;
244
245	return true;
246    }
247};
248
249
250} // namespace Imf
251
252#endif
253