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