Library of Assembled Shared Sources
binary_i_file.cpp
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2024 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#include "lass_common.h"
44#include "binary_i_file.h"
45#include "../meta/meta_assert.h"
46#include <stdio.h>
47
48#if LASS_PLATFORM_TYPE == LASS_PLATFORM_TYPE_WIN32
49# pragma warning(disable: 4996) // 'fopen': This function or variable may be unsafe
50#endif
51
52namespace lass
53{
54
55namespace io
56{
57
58/** Construct an "empty" closed stream..
59 */
61 BinaryIStream(),
62 file_(0)
63{
64}
65
66
67
68/** Construct stream by filename and open it.
69 */
70BinaryIFile::BinaryIFile(const char* path):
71 BinaryIStream(),
72 file_(0)
73{
74 open(path);
75}
76
77
78
79/** Construct stream by filename and open it.
80 */
81BinaryIFile::BinaryIFile(const std::string& path):
82 BinaryIStream(),
83 file_(0)
84{
85 open(path);
86}
87
88
89
90#if LASS_HAVE_WCHAR_SUPPORT
91
92/** Construct stream by filename and open it.
93 */
94BinaryIFile::BinaryIFile(const wchar_t* path):
96 file_(0)
97{
98 open(path);
99}
100
101
102
103/** Construct stream by filename and open it.
104 */
105BinaryIFile::BinaryIFile(const std::wstring& path):
106 BinaryIStream(),
107 file_(0)
108{
109 open(path);
110}
111
112#endif
113
114
115
116#if LASS_HAVE_STD_FILESYSTEM
117
118/** Construct stream by filename and open it.
119 */
120BinaryIFile::BinaryIFile(const std::filesystem::path& path):
121 BinaryIStream(),
122 file_(0)
123{
124 open(path);
125}
126
127#endif
128
129
130
131/** Close stream on destruction.
132 */
134{
135 close();
136}
137
138
139
140void BinaryIFile::open(const char* path)
141{
142#if LASS_HAVE_WCHAR_SUPPORT && LASS_HAVE_WFOPEN
143 open(util::utf8ToWchar(path));
144#else
145 close();
146 file_ = ::fopen(path, "rb");
147 if (!file_)
148 {
149 setstate(std::ios_base::failbit);
150 }
151#endif
152}
153
154
155
156void BinaryIFile::open(const std::string& path)
157{
158 open(path.c_str());
159}
160
161
162
163#if LASS_HAVE_WCHAR_SUPPORT
164
165void BinaryIFile::open(const wchar_t* path)
166{
167# if LASS_HAVE_WFOPEN
168 close();
169 file_ = ::_wfopen(path, L"rb");
170 if (!file_)
171 {
172 setstate(std::ios_base::failbit);
173 }
174# else
175 open(util::wcharToUtf8(path));
176# endif
177}
178
179
180
181void BinaryIFile::open(const std::wstring& path)
182{
183 open(path.c_str());
184}
185
186#endif
187
188
189
190#if LASS_HAVE_STD_FILESYSTEM
191
192void BinaryIFile::open(const std::filesystem::path& path)
193{
194 close();
195 if (good())
196 {
197#if LASS_HAVE_WFOPEN
198 file_ = ::_wfopen(path.c_str(), L"rb");
199#else
200 file_ = ::fopen(path.c_str(), "rb");
201#endif
202 if (!file_)
203 {
204 setstate(std::ios_base::failbit);
205 }
206 }
207}
208
209#endif
210
211
212
213void BinaryIFile::close()
214{
215 if (is_open())
216 {
217 const int result = ::fclose( file_ );
218 if (result != 0)
219 {
220 setstate(std::ios_base::failbit);
221 }
222 file_ = 0;
223 }
224}
225
226bool BinaryIFile::is_open() const
227{
228 return file_ != 0;
229}
230
231
232
233// --- private -------------------------------------------------------------------------------------
234
235BinaryIFile::pos_type BinaryIFile::doTellg() const
236{
237#if LASS_COMPILER_TYPE == LASS_COMPILER_TYPE_MSVC && LASS_ADDRESS_SIZE == 64
238 const off_type pos = ::_ftelli64(file_);
239#else
240 const off_type pos = ::ftell(file_);
241#endif
242 return static_cast<pos_type>(pos >= 0 ? pos : -1);
243}
244
245
246
247void BinaryIFile::doSeekg(pos_type position)
248{
249 if (position > num::NumTraits<off_type>::max)
250 {
251 setstate(std::ios_base::failbit);
252 return;
253 }
254 doSeekg(static_cast<off_type>(position), std::ios_base::beg);
255}
256
257
258
259void BinaryIFile::doSeekg(off_type offset, std::ios_base::seekdir direction)
260{
261#if LASS_COMPILER_TYPE == LASS_COMPILER_TYPE_MSVC && LASS_ADDRESS_SIZE == 64
262 const int result = ::_fseeki64(file_, offset, impl::seekdir2stdio(direction));
263#else
264 const int result = ::fseek(file_, offset, impl::seekdir2stdio(direction));
265#endif
266 if (result != 0)
267 {
268 setstate(std::ios_base::failbit);
269 }
270}
271
272
273
274size_t BinaryIFile::doRead(void* output, size_t numberOfBytes)
275{
276 if (!file_)
277 {
278 setstate(std::ios_base::failbit);
279 return 0;
280 }
281 if (!good())
282 {
283 return 0;
284 }
285 const size_t bytesRead = ::fread(output, 1, numberOfBytes, file_);
286 if (bytesRead != numberOfBytes)
287 {
288 setstate(std::ios_base::eofbit);
289 }
290 return bytesRead;
291}
292
293
294
295}
296
297}
BinaryIFile()
Construct an "empty" closed stream.
~BinaryIFile()
Close stream on destruction.
base class of binary input streams.
streams, binary streams, vrmlstreams, ...
Library for Assembled Shared Sources.
Definition config.h:53