Library of Assembled Shared Sources
binary_o_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_o_file.h"
46
47#include <stdio.h>
48
49#if LASS_PLATFORM_TYPE == LASS_PLATFORM_TYPE_WIN32
50# pragma warning(disable: 4996) // 'fopen': This function or variable may be unsafe
51#endif
52
53namespace lass
54{
55namespace io
56{
57
58// --- public --------------------------------------------------------------------------------------
59
60/** Construct an "empty" closed file..
61 */
63 BinaryOStream(),
64 file_(0)
65{
66}
67
68
69
70/** Construct stream by filename and open it.
71 */
72BinaryOFile::BinaryOFile( const char* path ):
73 BinaryOStream(),
74 file_(0)
75{
76 open(path);
77}
78
79
80
81/** Construct stream by filename and open it.
82 */
83BinaryOFile::BinaryOFile( const std::string& path ):
84 BinaryOStream(),
85 file_(0)
86{
87 open(path);
88}
89
90
91
92#if LASS_HAVE_WCHAR_SUPPORT
93
94/** Construct stream by filename and open it.
95 */
96BinaryOFile::BinaryOFile( const wchar_t* path ):
98 file_(0)
99{
100 open(path);
101}
102
103
104
105/** Construct stream by filename and open it.
106 */
107BinaryOFile::BinaryOFile( const std::wstring& path ):
108 BinaryOStream(),
109 file_(0)
110{
111 open(path);
112}
113
114#endif
115
116
117
118#if LASS_HAVE_STD_FILESYSTEM
119
120/** Construct stream by filename and open it.
121 */
122BinaryOFile::BinaryOFile(std::filesystem::path& path) :
123 BinaryOStream(),
124 file_(0)
125{
126 open(path);
127}
128
129#endif
130
131
132
133/** Close stream on destruction.
134 */
136{
137 close();
138}
139
140
141
142void BinaryOFile::open(const char* path)
143{
144#if LASS_HAVE_WCHAR_SUPPORT && LASS_HAVE_WFOPEN
145 open(util::utf8ToWchar(path));
146#else
147 close();
148 if (good())
149 {
150 file_ = ::fopen(path, "wb");
151 if (!file_)
152 {
153 setstate(std::ios_base::failbit);
154 }
155 }
156#endif
157}
158
159
160
161void BinaryOFile::open(const std::string& path)
162{
163 open(path.c_str());
164}
165
166
167
168#if LASS_HAVE_WCHAR_SUPPORT
169
170void BinaryOFile::open(const wchar_t* path)
171{
172#if LASS_HAVE_WFOPEN
173 close();
174 if (good())
175 {
176 file_ = ::_wfopen(path, L"wb");
177 if (!file_)
178 {
179 setstate(std::ios_base::failbit);
180 }
181 }
182#else
183 open(util::wcharToUtf8(path));
184#endif
185}
186
187
188
189void BinaryOFile::open(const std::wstring& path)
190{
191 open(path.c_str());
192}
193
194#endif
195
196
197
198#if LASS_HAVE_STD_FILESYSTEM
199
200void BinaryOFile::open(const std::filesystem::path& path)
201{
202 close();
203 if (good())
204 {
205#if LASS_HAVE_WFOPEN
206 file_ = ::_wfopen(path.c_str(), L"wb");
207#else
208 file_ = ::fopen(path.c_str(), "wb");
209#endif
210 if (!file_)
211 {
212 setstate(std::ios_base::failbit);
213 }
214 }
215}
216
217#endif
218
219
220
221void BinaryOFile::close()
222{
223 if (is_open())
224 {
225 const int result = ::fclose( file_ );
226 if (result != 0)
227 {
228 setstate(std::ios_base::failbit);
229 }
230 file_ = 0;
231 }
232}
233
234
235
236bool BinaryOFile::is_open() const
237{
238 return file_ != 0;
239}
240
241// --- private -------------------------------------------------------------------------------------
242
243BinaryOFile::pos_type BinaryOFile::doTellp() const
244{
245#if LASS_COMPILER_TYPE == LASS_COMPILER_TYPE_MSVC && LASS_ADDRESS_SIZE == 64
246 const off_type pos = ::_ftelli64(file_);
247#else
248 const off_type pos = ::ftell(file_);
249#endif
250 return static_cast<pos_type>(pos >= 0 ? pos : -1);
251}
252
253
254
255void BinaryOFile::doSeekp(pos_type position)
256{
257 if (position > num::NumTraits<off_type>::max)
258 {
259 setstate(std::ios_base::failbit);
260 return;
261 }
262 doSeekp(static_cast<off_type>(position), std::ios_base::beg);
263}
264
265
266void BinaryOFile::doSeekp(off_type offset, std::ios_base::seekdir direction)
267{
268#if LASS_COMPILER_TYPE == LASS_COMPILER_TYPE_MSVC && LASS_ADDRESS_SIZE == 64
269 const int result = ::_fseeki64(file_, offset, impl::seekdir2stdio(direction));
270#else
271 const int result = ::fseek(file_, offset, impl::seekdir2stdio(direction));
272#endif
273 if (result != 0)
274 {
275 setstate(std::ios_base::failbit);
276 }
277}
278
279
280
281void BinaryOFile::doFlush()
282{
283 if (!file_)
284 {
285 setstate(std::ios_base::failbit);
286 return;
287 }
288 if (!good())
289 {
290 return;
291 }
292 const int result = ::fflush(file_);
293 if (result != 0)
294 {
295 setstate(std::ios_base::badbit);
296 }
297}
298
299
300
301/** write a buffer of bytes to the stream
302 * @par iIn pointer to buffer.
303 * @par iBufferLength length of buffer in bytes.
304 */
305size_t BinaryOFile::doWrite(const void* iBytes, size_t iNumberOfBytes)
306{
307 if (!file_)
308 {
309 setstate(std::ios_base::failbit);
310 return 0;
311 }
312 if (!good())
313 {
314 return 0 ;
315 }
316 const size_t bytesWritten = ::fwrite( iBytes, 1, iNumberOfBytes, file_ );
317 if (bytesWritten != iNumberOfBytes)
318 {
319 setstate(std::ios_base::badbit);
320 }
321 return bytesWritten;
322}
323
324
325}
326
327}
328
329// EOF
~BinaryOFile()
Close stream on destruction.
BinaryOFile()
Construct an "empty" closed file.
base class of binary output streams.
streams, binary streams, vrmlstreams, ...
Library for Assembled Shared Sources.
Definition config.h:53