1///////////////////////////////////////////////////////////////////////////
2//
3// Copyright (c) 2004, 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//-----------------------------------------------------------------------------
37//
38//	Low-level file input and output for OpenEXR
39//	based on C++ standard iostreams.
40//
41//-----------------------------------------------------------------------------
42
43#include <ImfStdIO.h>
44#include "Iex.h"
45#include <errno.h>
46
47using namespace std;
48
49namespace Imf {
50namespace {
51
52void
53clearError ()
54{
55    errno = 0;
56}
57
58
59bool
60checkError (istream &is)
61{
62    if (!is)
63    {
64	if (errno)
65	    Iex::throwErrnoExc();
66	return false;
67    }
68
69    return true;
70}
71
72
73void
74checkError (ostream &os)
75{
76    if (!os)
77    {
78	if (errno)
79	    Iex::throwErrnoExc();
80
81	throw Iex::ErrnoExc ("File output failed.");
82    }
83}
84
85} // namespace
86
87
88StdIFStream::StdIFStream (const char fileName[]):
89    IStream (fileName),
90    _is (new ifstream (fileName, ios_base::binary)),
91    _deleteStream (true)
92{
93    if (!*_is)
94    {
95	delete _is;
96	Iex::throwErrnoExc();
97    }
98}
99
100
101StdIFStream::StdIFStream (ifstream &is, const char fileName[]):
102    IStream (fileName),
103    _is (&is),
104    _deleteStream (false)
105{
106    // empty
107}
108
109
110StdIFStream::~StdIFStream ()
111{
112    if (_deleteStream)
113	delete _is;
114}
115
116
117bool
118StdIFStream::read (char c[/*n*/], int n)
119{
120    if (!*_is)
121        throw Iex::InputExc ("Unexpected end of file.");
122
123    clearError();
124    _is->read (c, n);
125    return checkError (*_is);
126}
127
128
129Int64
130StdIFStream::tellg ()
131{
132    return std::streamoff (_is->tellg());
133}
134
135
136void
137StdIFStream::seekg (Int64 pos)
138{
139    _is->seekg (pos);
140    checkError (*_is);
141}
142
143
144void
145StdIFStream::clear ()
146{
147    _is->clear();
148}
149
150
151StdOFStream::StdOFStream (const char fileName[]):
152    OStream (fileName),
153    _os (new ofstream (fileName, ios_base::binary)),
154    _deleteStream (true)
155{
156    if (!*_os)
157    {
158	delete _os;
159	Iex::throwErrnoExc();
160    }
161}
162
163
164StdOFStream::StdOFStream (ofstream &os, const char fileName[]):
165    OStream (fileName),
166    _os (&os),
167    _deleteStream (false)
168{
169    // empty
170}
171
172
173StdOFStream::~StdOFStream ()
174{
175    if (_deleteStream)
176	delete _os;
177}
178
179
180void
181StdOFStream::write (const char c[/*n*/], int n)
182{
183    clearError();
184    _os->write (c, n);
185    checkError (*_os);
186}
187
188
189Int64
190StdOFStream::tellp ()
191{
192    return std::streamoff (_os->tellp());
193}
194
195
196void
197StdOFStream::seekp (Int64 pos)
198{
199    _os->seekp (pos);
200    checkError (*_os);
201}
202
203
204StdOSStream::StdOSStream (): OStream ("(string)")
205{
206    // empty
207}
208
209
210void
211StdOSStream::write (const char c[/*n*/], int n)
212{
213    clearError();
214    _os.write (c, n);
215    checkError (_os);
216}
217
218
219Int64
220StdOSStream::tellp ()
221{
222    return std::streamoff (_os.tellp());
223}
224
225
226void
227StdOSStream::seekp (Int64 pos)
228{
229    _os.seekp (pos);
230    checkError (_os);
231}
232
233
234} // namespace Imf
235