Coverage Report

Created: 2026-07-25 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wxwidgets/src/common/wfstream.cpp
Line
Count
Source
1
/////////////////////////////////////////////////////////////////////////////
2
// Name:        src/common/wfstream.cpp
3
// Purpose:     "File stream" classes
4
// Author:      Julian Smart
5
// Created:     11/07/98
6
// Copyright:   (c) Guilhem Lavaux
7
// Licence:     wxWindows licence
8
/////////////////////////////////////////////////////////////////////////////
9
10
// For compilers that support precompilation, includes "wx.h".
11
#include "wx/wxprec.h"
12
13
14
#if wxUSE_STREAMS
15
16
#include "wx/wfstream.h"
17
18
#ifndef WX_PRECOMP
19
    #include "wx/stream.h"
20
#endif
21
22
#include <stdio.h>
23
24
#if wxUSE_FILE
25
26
// ----------------------------------------------------------------------------
27
// wxFileInputStream
28
// ----------------------------------------------------------------------------
29
30
wxFileInputStream::wxFileInputStream(const wxString& fileName)
31
0
  : wxInputStream()
32
0
{
33
0
    m_file = new wxFile(fileName, wxFile::read);
34
0
    m_file_destroy = true;
35
0
    if ( !m_file->IsOpened() )
36
0
        m_lasterror = wxSTREAM_READ_ERROR;
37
0
}
38
39
wxFileInputStream::wxFileInputStream()
40
0
  : wxInputStream()
41
0
{
42
0
    m_file_destroy = false;
43
0
    m_file = nullptr;
44
0
}
45
46
wxFileInputStream::wxFileInputStream(wxFile& file)
47
0
{
48
0
    m_file = &file;
49
0
    m_file_destroy = false;
50
0
}
51
52
wxFileInputStream::wxFileInputStream(int fd)
53
0
{
54
0
    m_file = new wxFile(fd);
55
0
    m_file_destroy = true;
56
0
}
57
58
wxFileInputStream::~wxFileInputStream()
59
0
{
60
0
    if (m_file_destroy)
61
0
        delete m_file;
62
0
}
63
64
wxFileOffset wxFileInputStream::GetLength() const
65
0
{
66
0
    return m_file->Length();
67
0
}
68
69
size_t wxFileInputStream::OnSysRead(void *buffer, size_t size)
70
0
{
71
0
    ssize_t ret = m_file->Read(buffer, size);
72
73
    // NB: we can't use a switch here because HP-UX CC doesn't allow
74
    //     switching over long long (which size_t is in 64bit mode)
75
76
0
    if ( !ret )
77
0
    {
78
        // nothing read, so nothing more to read
79
0
        m_lasterror = wxSTREAM_EOF;
80
0
    }
81
0
    else if ( ret == wxInvalidOffset )
82
0
    {
83
0
        m_lasterror = wxSTREAM_READ_ERROR;
84
0
        ret = 0;
85
0
    }
86
0
    else
87
0
    {
88
        // normal case
89
0
        m_lasterror = wxSTREAM_NO_ERROR;
90
0
    }
91
92
0
    return ret;
93
0
}
94
95
wxFileOffset wxFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
96
0
{
97
0
    return m_file->Seek(pos, mode);
98
0
}
99
100
wxFileOffset wxFileInputStream::OnSysTell() const
101
0
{
102
0
    return m_file->Tell();
103
0
}
104
105
bool wxFileInputStream::IsOk() const
106
0
{
107
0
    return wxInputStream::IsOk() && m_file->IsOpened();
108
0
}
109
110
// ----------------------------------------------------------------------------
111
// wxFileOutputStream
112
// ----------------------------------------------------------------------------
113
114
wxFileOutputStream::wxFileOutputStream(const wxString& fileName)
115
0
{
116
0
    m_file = new wxFile(fileName, wxFile::write);
117
0
    m_file_destroy = true;
118
119
0
    if (!m_file->IsOpened())
120
0
        m_lasterror = wxSTREAM_WRITE_ERROR;
121
0
}
122
123
wxFileOutputStream::wxFileOutputStream(wxFile& file)
124
0
{
125
0
    m_file = &file;
126
0
    m_file_destroy = false;
127
0
}
128
129
wxFileOutputStream::wxFileOutputStream()
130
0
                  : wxOutputStream()
131
0
{
132
0
    m_file_destroy = false;
133
0
    m_file = nullptr;
134
0
}
135
136
wxFileOutputStream::wxFileOutputStream(int fd)
137
0
{
138
0
    m_file = new wxFile(fd);
139
0
    m_file_destroy = true;
140
0
}
141
142
wxFileOutputStream::~wxFileOutputStream()
143
0
{
144
0
    if (m_file_destroy)
145
0
    {
146
0
        Sync();
147
0
        delete m_file;
148
0
    }
149
0
}
150
151
size_t wxFileOutputStream::OnSysWrite(const void *buffer, size_t size)
152
0
{
153
0
    size_t ret = m_file->Write(buffer, size);
154
155
0
    m_lasterror = m_file->Error() ? wxSTREAM_WRITE_ERROR : wxSTREAM_NO_ERROR;
156
157
0
    return ret;
158
0
}
159
160
wxFileOffset wxFileOutputStream::OnSysTell() const
161
0
{
162
0
    return m_file->Tell();
163
0
}
164
165
wxFileOffset wxFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
166
0
{
167
0
    return m_file->Seek(pos, mode);
168
0
}
169
170
void wxFileOutputStream::Sync()
171
0
{
172
0
    wxOutputStream::Sync();
173
0
    m_file->Flush();
174
0
}
175
176
wxFileOffset wxFileOutputStream::GetLength() const
177
0
{
178
0
    return m_file->Length();
179
0
}
180
181
bool wxFileOutputStream::IsOk() const
182
0
{
183
0
    return wxOutputStream::IsOk() && m_file->IsOpened();
184
0
}
185
186
// ----------------------------------------------------------------------------
187
// wxTempFileOutputStream
188
// ----------------------------------------------------------------------------
189
190
wxTempFileOutputStream::wxTempFileOutputStream(const wxString& fileName)
191
0
{
192
0
    m_file = new wxTempFile(fileName);
193
194
0
    if (!m_file->IsOpened())
195
0
        m_lasterror = wxSTREAM_WRITE_ERROR;
196
0
}
197
198
wxTempFileOutputStream::~wxTempFileOutputStream()
199
0
{
200
0
    if (m_file->IsOpened())
201
0
        Discard();
202
0
    delete m_file;
203
0
}
204
205
size_t wxTempFileOutputStream::OnSysWrite(const void *buffer, size_t size)
206
0
{
207
0
    if (IsOk() && m_file->Write(buffer, size))
208
0
        return size;
209
0
    m_lasterror = wxSTREAM_WRITE_ERROR;
210
0
    return 0;
211
0
}
212
213
// ----------------------------------------------------------------------------
214
// wxTempFFileOutputStream
215
// ----------------------------------------------------------------------------
216
217
wxTempFFileOutputStream::wxTempFFileOutputStream(const wxString& fileName)
218
0
{
219
0
    m_file = new wxTempFFile(fileName);
220
221
0
    if (!m_file->IsOpened())
222
0
        m_lasterror = wxSTREAM_WRITE_ERROR;
223
0
}
224
225
wxTempFFileOutputStream::~wxTempFFileOutputStream()
226
0
{
227
0
    if (m_file->IsOpened())
228
0
        Discard();
229
0
    delete m_file;
230
0
}
231
232
size_t wxTempFFileOutputStream::OnSysWrite(const void *buffer, size_t size)
233
0
{
234
0
    if (IsOk() && m_file->Write(buffer, size))
235
0
        return size;
236
0
    m_lasterror = wxSTREAM_WRITE_ERROR;
237
0
    return 0;
238
0
}
239
240
// ----------------------------------------------------------------------------
241
// wxFileStream
242
// ----------------------------------------------------------------------------
243
244
wxFileStream::wxFileStream(const wxString& fileName)
245
0
            : wxFileInputStream(),
246
0
              wxFileOutputStream()
247
0
{
248
0
    wxFileOutputStream::m_file =
249
0
    wxFileInputStream::m_file = new wxFile(fileName, wxFile::read_write);
250
251
    // this is a bit ugly as streams are symmetric but we still have to delete
252
    // the file we created above exactly once so we decide to (arbitrarily) do
253
    // it in wxFileInputStream
254
0
    wxFileInputStream::m_file_destroy = true;
255
0
}
256
257
bool wxFileStream::IsOk() const
258
0
{
259
0
    return wxFileOutputStream::IsOk() && wxFileInputStream::IsOk();
260
0
}
261
262
#endif // wxUSE_FILE
263
264
#if wxUSE_FFILE
265
266
// ----------------------------------------------------------------------------
267
// wxFFileInputStream
268
// ----------------------------------------------------------------------------
269
270
wxFFileInputStream::wxFFileInputStream(const wxString& fileName,
271
                                       const wxString& mode)
272
0
                  : wxInputStream()
273
0
{
274
0
    m_file = new wxFFile(fileName, mode);
275
0
    m_file_destroy = true;
276
277
0
    if (!m_file->IsOpened())
278
0
        m_lasterror = wxSTREAM_WRITE_ERROR;
279
0
}
280
281
wxFFileInputStream::wxFFileInputStream()
282
0
                  : wxInputStream()
283
0
{
284
0
    m_file = nullptr;
285
0
    m_file_destroy = false;
286
0
}
287
288
wxFFileInputStream::wxFFileInputStream(wxFFile& file)
289
0
{
290
0
    m_file = &file;
291
0
    m_file_destroy = false;
292
0
}
293
294
wxFFileInputStream::wxFFileInputStream(FILE *file)
295
0
{
296
0
    m_file = new wxFFile(file);
297
0
    m_file_destroy = true;
298
0
}
299
300
wxFFileInputStream::~wxFFileInputStream()
301
0
{
302
0
    if (m_file_destroy)
303
0
        delete m_file;
304
0
}
305
306
wxFileOffset wxFFileInputStream::GetLength() const
307
0
{
308
0
    return m_file->Length();
309
0
}
310
311
size_t wxFFileInputStream::OnSysRead(void *buffer, size_t size)
312
0
{
313
0
    ssize_t ret = m_file->Read(buffer, size);
314
315
    // It is not safe to call Eof() if the file is not opened.
316
0
    if (!m_file->IsOpened() || m_file->Eof())
317
0
        m_lasterror = wxSTREAM_EOF;
318
0
    if (ret == wxInvalidOffset)
319
0
    {
320
0
        m_lasterror = wxSTREAM_READ_ERROR;
321
0
        ret = 0;
322
0
    }
323
324
0
    return ret;
325
0
}
326
327
wxFileOffset wxFFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
328
0
{
329
0
    return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset;
330
0
}
331
332
wxFileOffset wxFFileInputStream::OnSysTell() const
333
0
{
334
0
    return m_file->Tell();
335
0
}
336
337
bool wxFFileInputStream::IsOk() const
338
0
{
339
0
    return wxStreamBase::IsOk() && m_file->IsOpened();
340
0
}
341
342
// ----------------------------------------------------------------------------
343
// wxFFileOutputStream
344
// ----------------------------------------------------------------------------
345
346
wxFFileOutputStream::wxFFileOutputStream(const wxString& fileName,
347
                                         const wxString& mode)
348
0
{
349
0
    m_file = new wxFFile(fileName, mode);
350
0
    m_file_destroy = true;
351
352
0
    if (!m_file->IsOpened())
353
0
    {
354
0
        m_lasterror = wxSTREAM_WRITE_ERROR;
355
0
    }
356
0
    else
357
0
    {
358
0
        if (m_file->Error())
359
0
            m_lasterror = wxSTREAM_WRITE_ERROR;
360
0
    }
361
0
}
362
363
wxFFileOutputStream::wxFFileOutputStream(wxFFile& file)
364
0
{
365
0
    m_file = &file;
366
0
    m_file_destroy = false;
367
0
}
368
369
wxFFileOutputStream::wxFFileOutputStream()
370
0
                   : wxOutputStream()
371
0
{
372
0
    m_file = nullptr;
373
0
    m_file_destroy = false;
374
0
}
375
376
wxFFileOutputStream::wxFFileOutputStream(FILE *file)
377
0
{
378
0
    m_file = new wxFFile(file);
379
0
    m_file_destroy = true;
380
0
}
381
382
wxFFileOutputStream::~wxFFileOutputStream()
383
0
{
384
0
    if (m_file_destroy)
385
0
    {
386
0
        Sync();
387
0
        delete m_file;
388
0
    }
389
0
}
390
391
size_t wxFFileOutputStream::OnSysWrite(const void *buffer, size_t size)
392
0
{
393
0
    size_t ret = m_file->Write(buffer, size);
394
    // It is not safe to call Error() if the file is not opened.
395
0
    if (!m_file->IsOpened() || m_file->Error())
396
0
        m_lasterror = wxSTREAM_WRITE_ERROR;
397
0
    else
398
0
        m_lasterror = wxSTREAM_NO_ERROR;
399
0
    return ret;
400
0
}
401
402
wxFileOffset wxFFileOutputStream::OnSysTell() const
403
0
{
404
0
    return m_file->Tell();
405
0
}
406
407
wxFileOffset wxFFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
408
0
{
409
0
    return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset;
410
0
}
411
412
void wxFFileOutputStream::Sync()
413
0
{
414
0
    wxOutputStream::Sync();
415
0
    m_file->Flush();
416
0
}
417
418
wxFileOffset wxFFileOutputStream::GetLength() const
419
0
{
420
0
    return m_file->Length();
421
0
}
422
423
bool wxFFileOutputStream::IsOk() const
424
0
{
425
0
    return wxStreamBase::IsOk() && m_file->IsOpened();
426
0
}
427
428
// ----------------------------------------------------------------------------
429
// wxFFileStream
430
// ----------------------------------------------------------------------------
431
432
wxFFileStream::wxFFileStream(const wxString& fileName, const wxString& mode)
433
0
             : wxFFileInputStream(),
434
0
               wxFFileOutputStream()
435
0
{
436
0
    wxASSERT_MSG( mode.find_first_of('+') != wxString::npos,
437
0
                  "must be opened in read-write mode for this class to work" );
438
439
0
    wxFFileOutputStream::m_file =
440
0
    wxFFileInputStream::m_file = new wxFFile(fileName, mode);
441
442
    // see comment in wxFileStream ctor
443
0
    wxFFileInputStream::m_file_destroy = true;
444
0
}
445
446
bool wxFFileStream::IsOk() const
447
0
{
448
0
    return wxFFileOutputStream::IsOk() && wxFFileInputStream::IsOk();
449
0
}
450
451
#endif //wxUSE_FFILE
452
453
#endif // wxUSE_STREAMS