1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFile - encapsulates low-level "file descriptor"
4 // wxTempFile - safely replace the old file
5 // Author: Vadim Zeitlin
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
20 #include "wx/string.h"
21 #include "wx/filefn.h"
22 #include "wx/strconv.h"
24 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
28 // we redefine these constants here because S_IREAD &c are _not_ standard
29 // however, we do assume that the values correspond to the Unix umask bits
30 enum wxPosixPermissions
32 // standard Posix names for these permission flags:
45 // longer but more readable synonyms for the constants above:
46 wxPOSIX_USER_READ
= wxS_IRUSR
,
47 wxPOSIX_USER_WRITE
= wxS_IWUSR
,
48 wxPOSIX_USER_EXECUTE
= wxS_IXUSR
,
50 wxPOSIX_GROUP_READ
= wxS_IRGRP
,
51 wxPOSIX_GROUP_WRITE
= wxS_IWGRP
,
52 wxPOSIX_GROUP_EXECUTE
= wxS_IXGRP
,
54 wxPOSIX_OTHERS_READ
= wxS_IROTH
,
55 wxPOSIX_OTHERS_WRITE
= wxS_IWOTH
,
56 wxPOSIX_OTHERS_EXECUTE
= wxS_IXOTH
,
58 // default mode for the new files: allow reading/writing them to everybody but
59 // the effective file mode will be set after anding this value with umask and
60 // so won't include wxS_IW{GRP,OTH} for the default 022 umask value
61 wxS_DEFAULT
= (wxPOSIX_USER_READ
| wxPOSIX_USER_WRITE
| \
62 wxPOSIX_GROUP_READ
| wxPOSIX_GROUP_WRITE
| \
63 wxPOSIX_OTHERS_READ
| wxPOSIX_OTHERS_WRITE
),
65 // default mode for the new directories (see wxFileName::Mkdir): allow
66 // reading/writing/executing them to everybody, but just like wxS_DEFAULT
67 // the effective directory mode will be set after anding this value with umask
68 wxS_DIR_DEFAULT
= (wxPOSIX_USER_READ
| wxPOSIX_USER_WRITE
| wxPOSIX_USER_EXECUTE
| \
69 wxPOSIX_GROUP_READ
| wxPOSIX_GROUP_WRITE
| wxPOSIX_GROUP_EXECUTE
| \
70 wxPOSIX_OTHERS_READ
| wxPOSIX_OTHERS_WRITE
| wxPOSIX_OTHERS_EXECUTE
)
73 // ----------------------------------------------------------------------------
74 // class wxFile: raw file IO
76 // NB: for space efficiency this class has no virtual functions, including
77 // dtor which is _not_ virtual, so it shouldn't be used as a base class.
78 // ----------------------------------------------------------------------------
80 class WXDLLIMPEXP_BASE wxFile
83 // more file constants
84 // -------------------
86 enum OpenMode
{ read
, write
, read_write
, write_append
, write_excl
};
87 // standard values for file descriptor
88 enum { fd_invalid
= -1, fd_stdin
, fd_stdout
, fd_stderr
};
92 // check whether a regular file by this name exists
93 static bool Exists(const wxString
& name
);
94 // check whether we can access the given file in given mode
95 // (only read and write make sense here)
96 static bool Access(const wxString
& name
, OpenMode mode
);
101 wxFile() { m_fd
= fd_invalid
; m_error
= false; }
102 // open specified file (may fail, use IsOpened())
103 wxFile(const wxString
& fileName
, OpenMode mode
= read
);
104 // attach to (already opened) file
105 wxFile(int lfd
) { m_fd
= lfd
; m_error
= false; }
108 // create a new file (with the default value of bOverwrite, it will fail if
109 // the file already exists, otherwise it will overwrite it and succeed)
110 bool Create(const wxString
& fileName
, bool bOverwrite
= false,
111 int access
= wxS_DEFAULT
);
112 bool Open(const wxString
& fileName
, OpenMode mode
= read
,
113 int access
= wxS_DEFAULT
);
114 bool Close(); // Close is a NOP if not opened
116 // assign an existing file descriptor and get it back from wxFile object
117 void Attach(int lfd
) { Close(); m_fd
= lfd
; m_error
= false; }
118 void Detach() { m_fd
= fd_invalid
; }
119 int fd() const { return m_fd
; }
121 // read/write (unbuffered)
122 // returns number of bytes read or wxInvalidOffset on error
123 ssize_t
Read(void *pBuf
, size_t nCount
);
124 // returns the number of bytes written
125 size_t Write(const void *pBuf
, size_t nCount
);
126 // returns true on success
127 bool Write(const wxString
& s
, const wxMBConv
& conv
= wxMBConvUTF8());
128 // flush data not yet written
131 // file pointer operations (return wxInvalidOffset on failure)
132 // move ptr ofs bytes related to start/current offset/end of file
133 wxFileOffset
Seek(wxFileOffset ofs
, wxSeekMode mode
= wxFromStart
);
134 // move ptr to ofs bytes before the end
135 wxFileOffset
SeekEnd(wxFileOffset ofs
= 0) { return Seek(ofs
, wxFromEnd
); }
136 // get current offset
137 wxFileOffset
Tell() const;
138 // get current file length
139 wxFileOffset
Length() const;
143 bool IsOpened() const { return m_fd
!= fd_invalid
; }
144 // is end of file reached?
146 // has an error occurred?
147 bool Error() const { return m_error
; }
148 // type such as disk or pipe
149 wxFileKind
GetKind() const { return wxGetFileKind(m_fd
); }
151 // dtor closes the file if opened
152 ~wxFile() { Close(); }
155 // copy ctor and assignment operator are private because
156 // it doesn't make sense to copy files this way:
157 // attempt to do it will provoke a compile-time error.
158 wxFile(const wxFile
&);
159 wxFile
& operator=(const wxFile
&);
161 int m_fd
; // file descriptor or INVALID_FD if not opened
162 bool m_error
; // error memory
165 // ----------------------------------------------------------------------------
166 // class wxTempFile: if you want to replace another file, create an instance
167 // of wxTempFile passing the name of the file to be replaced to the ctor. Then
168 // you can write to wxTempFile and call Commit() function to replace the old
169 // file (and close this one) or call Discard() to cancel the modification. If
170 // you call neither of them, dtor will call Discard().
171 // ----------------------------------------------------------------------------
173 class WXDLLIMPEXP_BASE wxTempFile
179 // associates the temp file with the file to be replaced and opens it
180 wxTempFile(const wxString
& strName
);
182 // open the temp file (strName is the name of file to be replaced)
183 bool Open(const wxString
& strName
);
185 // is the file opened?
186 bool IsOpened() const { return m_file
.IsOpened(); }
187 // get current file length
188 wxFileOffset
Length() const { return m_file
.Length(); }
189 // move ptr ofs bytes related to start/current offset/end of file
190 wxFileOffset
Seek(wxFileOffset ofs
, wxSeekMode mode
= wxFromStart
)
191 { return m_file
.Seek(ofs
, mode
); }
192 // get current offset
193 wxFileOffset
Tell() const { return m_file
.Tell(); }
195 // I/O (both functions return true on success, false on failure)
196 bool Write(const void *p
, size_t n
) { return m_file
.Write(p
, n
) == n
; }
197 bool Write(const wxString
& str
, const wxMBConv
& conv
= wxMBConvUTF8())
198 { return m_file
.Write(str
, conv
); }
200 // flush data: can be called before closing file to ensure that data was
201 // correctly written out
202 bool Flush() { return m_file
.Flush(); }
204 // different ways to close the file
205 // validate changes and delete the old file of name m_strName
210 // dtor calls Discard() if file is still opened
214 // no copy ctor/assignment operator
215 wxTempFile(const wxTempFile
&);
216 wxTempFile
& operator=(const wxTempFile
&);
218 wxString m_strName
, // name of the file to replace in Commit()
219 m_strTemp
; // temporary file name
220 wxFile m_file
; // the temporary file
225 #endif // _WX_FILEH__