Coverage Report

Created: 2025-12-31 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wxwidgets/include/wx/ffile.h
Line
Count
Source
1
/////////////////////////////////////////////////////////////////////////////
2
// Name:        wx/ffile.h
3
// Purpose:     wxFFile - encapsulates "FILE *" stream
4
// Author:      Vadim Zeitlin
5
// Created:     14.07.99
6
// Copyright:   (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
7
// Licence:     wxWindows licence
8
/////////////////////////////////////////////////////////////////////////////
9
10
#ifndef   _WX_FFILE_H_
11
#define   _WX_FFILE_H_
12
13
#include "wx/defs.h"        // for wxUSE_FFILE
14
15
#if wxUSE_FFILE
16
17
#include  "wx/string.h"
18
#include  "wx/filefn.h"
19
#include  "wx/convauto.h"
20
21
#include <stdio.h>
22
23
// ----------------------------------------------------------------------------
24
// class wxFFile: standard C stream library IO
25
//
26
// NB: for space efficiency this class has no virtual functions, including
27
//     dtor which is _not_ virtual, so it shouldn't be used as a base class.
28
// ----------------------------------------------------------------------------
29
30
class WXDLLIMPEXP_BASE wxFFile
31
{
32
public:
33
  // ctors
34
  // -----
35
    // def ctor
36
0
  wxFFile() { m_fp = nullptr; }
37
    // open specified file (may fail, use IsOpened())
38
  wxFFile(const wxString& filename, const wxString& mode = wxT("r"));
39
    // attach to (already opened) file
40
0
  wxFFile(FILE *lfp) { m_fp = lfp; }
41
42
  // open/close
43
    // open a file (existing or not - the mode controls what happens)
44
  bool Open(const wxString& filename, const wxString& mode = wxT("r"));
45
    // closes the opened file (this is a NOP if not opened)
46
  bool Close();
47
48
  // assign an existing file descriptor and get it back from wxFFile object
49
  void Attach(FILE *lfp, const wxString& name = wxEmptyString)
50
0
    { Close(); m_fp = lfp; m_name = name; }
51
0
  FILE* Detach() { FILE* fpOld = m_fp; m_fp = nullptr; return fpOld; }
52
0
  FILE *fp() const { return m_fp; }
53
54
  // read/write (unbuffered)
55
    // read all data from the file into a string (useful for text files)
56
  bool ReadAll(wxString *str, const wxMBConv& conv = wxConvAuto());
57
    // returns number of bytes read - use Eof() and Error() to see if an error
58
    // occurred or not
59
  size_t Read(void *pBuf, size_t nCount);
60
    // returns the number of bytes written
61
  size_t Write(const void *pBuf, size_t nCount);
62
    // returns true on success
63
  bool Write(const wxString& s, const wxMBConv& conv = wxConvAuto());
64
    // flush data not yet written
65
  bool Flush();
66
67
  // file pointer operations (return ofsInvalid on failure)
68
    // move ptr ofs bytes related to start/current pos/end of file
69
  bool Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart);
70
    // move ptr to ofs bytes before the end
71
0
  bool SeekEnd(wxFileOffset ofs = 0) { return Seek(ofs, wxFromEnd); }
72
    // get current position in the file
73
  wxFileOffset Tell() const;
74
    // get current file length
75
  wxFileOffset Length() const;
76
77
  // simple accessors: note that Eof() and Error() may only be called if
78
  // IsOpened(). Otherwise they assert and return false.
79
    // is file opened?
80
0
  bool IsOpened() const { return m_fp != nullptr; }
81
    // is end of file reached?
82
  bool Eof() const;
83
    // has an error occurred?
84
  bool Error() const;
85
    // get the file name
86
0
  const wxString& GetName() const { return m_name; }
87
    // type such as disk or pipe
88
0
  wxFileKind GetKind() const { return wxGetFileKind(m_fp); }
89
90
  // dtor closes the file if opened
91
0
  ~wxFFile() { Close(); }
92
93
private:
94
  // copy ctor and assignment operator are private because it doesn't make
95
  // sense to copy files this way: attempt to do it will provoke a compile-time
96
  // error.
97
  wxFFile(const wxFFile&);
98
  wxFFile& operator=(const wxFFile&);
99
100
  FILE *m_fp;       // IO stream or nullptr if not opened
101
102
  wxString m_name;  // the name of the file (for diagnostic messages)
103
};
104
105
// ----------------------------------------------------------------------------
106
// class wxTempFFile: if you want to replace another file, create an instance
107
// of wxTempFFile passing the name of the file to be replaced to the ctor. Then
108
// you can write to wxTempFFile and call Commit() function to replace the old
109
// file (and close this one) or call Discard() to cancel the modification. If
110
// you call neither of them, dtor will call Discard().
111
// ----------------------------------------------------------------------------
112
113
class WXDLLIMPEXP_BASE wxTempFFile
114
{
115
public:
116
  // ctors
117
    // default
118
  wxTempFFile() = default;
119
    // associates the temp file with the file to be replaced and opens it
120
  explicit wxTempFFile(const wxString& strName);
121
122
  // open the temp file (strName is the name of file to be replaced)
123
  bool Open(const wxString& strName);
124
125
  // is the file opened?
126
0
  bool IsOpened() const { return m_file.IsOpened(); }
127
    // get current file length
128
0
  wxFileOffset Length() const { return m_file.Length(); }
129
    // move ptr ofs bytes related to start/current pos/end of file
130
  bool Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart)
131
0
    { return m_file.Seek(ofs, mode); }
132
    // get current position in the file
133
0
  wxFileOffset Tell() const { return m_file.Tell(); }
134
135
  // I/O (both functions return true on success, false on failure)
136
0
  bool Write(const void *p, size_t n) { return m_file.Write(p, n) == n; }
137
  bool Write(const wxString& str, const wxMBConv& conv = wxMBConvUTF8())
138
0
    { return m_file.Write(str, conv); }
139
140
  // flush data: can be called before closing file to ensure that data was
141
  // correctly written out
142
0
  bool Flush() { return m_file.Flush(); }
143
144
  // different ways to close the file
145
    // validate changes and delete the old file of name m_strName
146
  bool Commit();
147
    // discard changes
148
  void Discard();
149
150
  // dtor calls Discard() if file is still opened
151
 ~wxTempFFile();
152
153
private:
154
  // no copy ctor/assignment operator
155
  wxTempFFile(const wxTempFFile&);
156
  wxTempFFile& operator=(const wxTempFFile&);
157
158
  wxString  m_strName,  // name of the file to replace in Commit()
159
            m_strTemp;  // temporary file name
160
  wxFFile   m_file;     // the temporary file
161
};
162
163
#endif // wxUSE_FFILE
164
165
#endif // _WX_FFILE_H_
166