Coverage Report

Created: 2023-02-22 06:06

/src/wxwidgets/include/wx/file.h
Line
Count
Source (jump to first uncovered line)
1
/////////////////////////////////////////////////////////////////////////////
2
// Name:        wx/file.h
3
// Purpose:     wxFile - encapsulates low-level "file descriptor"
4
//              wxTempFile - safely replace the old file
5
// Author:      Vadim Zeitlin
6
// Modified by:
7
// Created:     29/01/98
8
// Copyright:   (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9
// Licence:     wxWindows licence
10
/////////////////////////////////////////////////////////////////////////////
11
12
#ifndef _WX_FILEH__
13
#define _WX_FILEH__
14
15
#include  "wx/defs.h"
16
17
#if wxUSE_FILE
18
19
#include  "wx/string.h"
20
#include  "wx/filefn.h"
21
#include  "wx/convauto.h"
22
23
// ----------------------------------------------------------------------------
24
// class wxFile: raw file 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 wxFile
31
{
32
public:
33
  // more file constants
34
  // -------------------
35
  // suppress Xcode 11 warning about shadowing global read() symbol
36
  wxCLANG_WARNING_SUPPRESS(shadow)
37
38
    // opening mode
39
  enum OpenMode { read, write, read_write, write_append, write_excl };
40
    // standard values for file descriptor
41
  enum { fd_invalid = -1, fd_stdin, fd_stdout, fd_stderr };
42
43
  wxCLANG_WARNING_RESTORE(shadow)
44
45
  // static functions
46
  // ----------------
47
    // check whether a regular file by this name exists
48
  static bool Exists(const wxString& name);
49
    // check whether we can access the given file in given mode
50
    // (only read and write make sense here)
51
  static bool Access(const wxString& name, OpenMode mode);
52
53
  // ctors
54
  // -----
55
    // def ctor
56
0
  wxFile() { m_fd = fd_invalid; m_lasterror = 0; }
57
    // open specified file (may fail, use IsOpened())
58
  wxFile(const wxString& fileName, OpenMode mode = read);
59
    // attach to (already opened) file
60
0
  wxFile(int lfd) { m_fd = lfd; m_lasterror = 0; }
61
62
  // open/close
63
    // create a new file (with the default value of bOverwrite, it will fail if
64
    // the file already exists, otherwise it will overwrite it and succeed)
65
  bool Create(const wxString& fileName, bool bOverwrite = false,
66
              int access = wxS_DEFAULT);
67
  bool Open(const wxString& fileName, OpenMode mode = read,
68
            int access = wxS_DEFAULT);
69
  bool Close();  // Close is a NOP if not opened
70
71
  // assign an existing file descriptor and get it back from wxFile object
72
0
  void Attach(int lfd) { Close(); m_fd = lfd; m_lasterror = 0; }
73
0
  int  Detach() { const int fdOld = m_fd; m_fd = fd_invalid; return fdOld; }
74
0
  int  fd() const { return m_fd; }
75
76
  // read/write (unbuffered)
77
    // read all data from the file into a string (useful for text files)
78
  bool ReadAll(wxString *str, const wxMBConv& conv = wxConvAuto());
79
    // returns number of bytes read or wxInvalidOffset on error
80
  ssize_t Read(void *pBuf, size_t nCount);
81
    // returns the number of bytes written
82
  size_t Write(const void *pBuf, size_t nCount);
83
    // returns true on success
84
  bool Write(const wxString& s, const wxMBConv& conv = wxConvAuto());
85
    // flush data not yet written
86
  bool Flush();
87
88
  // file pointer operations (return wxInvalidOffset on failure)
89
    // move ptr ofs bytes related to start/current offset/end of file
90
  wxFileOffset Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart);
91
    // move ptr to ofs bytes before the end
92
0
  wxFileOffset SeekEnd(wxFileOffset ofs = 0) { return Seek(ofs, wxFromEnd); }
93
    // get current offset
94
  wxFileOffset Tell() const;
95
    // get current file length
96
  wxFileOffset Length() const;
97
98
  // simple accessors
99
    // is file opened?
100
0
  bool IsOpened() const { return m_fd != fd_invalid; }
101
    // is end of file reached?
102
  bool Eof() const;
103
    // has an error occurred?
104
0
  bool Error() const { return m_lasterror != 0; }
105
    // get last errno
106
0
  int GetLastError() const { return m_lasterror; }
107
    // reset error state
108
0
  void ClearLastError() { m_lasterror = 0; }
109
    // type such as disk or pipe
110
0
  wxFileKind GetKind() const { return wxGetFileKind(m_fd); }
111
112
113
  // dtor closes the file if opened
114
0
  ~wxFile() { Close(); }
115
116
private:
117
  // copy ctor and assignment operator are private because
118
  // it doesn't make sense to copy files this way:
119
  // attempt to do it will provoke a compile-time error.
120
  wxFile(const wxFile&);
121
  wxFile& operator=(const wxFile&);
122
123
  // Copy the value of errno into m_lasterror if rc == -1 and return true in
124
  // this case (indicating that we've got an error). Otherwise return false.
125
  //
126
  // Notice that we use the possibly 64 bit wxFileOffset instead of int here so
127
  // that it works for checking the result of functions such as tell() too.
128
  bool CheckForError(wxFileOffset rc) const;
129
130
131
  int m_fd; // file descriptor or INVALID_FD if not opened
132
  int m_lasterror; // errno value of last error
133
};
134
135
// ----------------------------------------------------------------------------
136
// class wxTempFile: if you want to replace another file, create an instance
137
// of wxTempFile passing the name of the file to be replaced to the ctor. Then
138
// you can write to wxTempFile and call Commit() function to replace the old
139
// file (and close this one) or call Discard() to cancel the modification. If
140
// you call neither of them, dtor will call Discard().
141
// ----------------------------------------------------------------------------
142
143
class WXDLLIMPEXP_BASE wxTempFile
144
{
145
public:
146
  // ctors
147
    // default
148
0
  wxTempFile() { }
149
    // associates the temp file with the file to be replaced and opens it
150
  explicit wxTempFile(const wxString& strName);
151
152
  // open the temp file (strName is the name of file to be replaced)
153
  bool Open(const wxString& strName);
154
155
  // is the file opened?
156
0
  bool IsOpened() const { return m_file.IsOpened(); }
157
    // get current file length
158
0
  wxFileOffset Length() const { return m_file.Length(); }
159
    // move ptr ofs bytes related to start/current offset/end of file
160
  wxFileOffset Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart)
161
0
    { return m_file.Seek(ofs, mode); }
162
    // get current offset
163
0
  wxFileOffset Tell() const { return m_file.Tell(); }
164
165
  // I/O (both functions return true on success, false on failure)
166
0
  bool Write(const void *p, size_t n) { return m_file.Write(p, n) == n; }
167
  bool Write(const wxString& str, const wxMBConv& conv = wxMBConvUTF8())
168
0
    { return m_file.Write(str, conv); }
169
170
  // flush data: can be called before closing file to ensure that data was
171
  // correctly written out
172
0
  bool Flush() { return m_file.Flush(); }
173
174
  // different ways to close the file
175
    // validate changes and delete the old file of name m_strName
176
  bool Commit();
177
    // discard changes
178
  void Discard();
179
180
  // dtor calls Discard() if file is still opened
181
 ~wxTempFile();
182
183
private:
184
  // no copy ctor/assignment operator
185
  wxTempFile(const wxTempFile&);
186
  wxTempFile& operator=(const wxTempFile&);
187
188
  wxString  m_strName,  // name of the file to replace in Commit()
189
            m_strTemp;  // temporary file name
190
  wxFile    m_file;     // the temporary file
191
};
192
193
#endif // wxUSE_FILE
194
195
#endif // _WX_FILEH__