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