]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: file.cpp | |
3 | // Purpose: wxFile - encapsulates low-level "file descriptor" | |
4 | // wxTempFile | |
5 | // Author: Vadim Zeitlin | |
6 | // Modified by: | |
7 | // Created: 29/01/98 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
65571936 | 10 | // Licence: wxWindows licence |
c801d85f KB |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
13 | // ---------------------------------------------------------------------------- | |
14 | // headers | |
15 | // ---------------------------------------------------------------------------- | |
16 | ||
c801d85f KB |
17 | // For compilers that support precompilation, includes "wx.h". |
18 | #include "wx/wxprec.h" | |
c801d85f KB |
19 | |
20 | #ifdef __BORLANDC__ | |
ce4169a4 | 21 | #pragma hdrstop |
c801d85f KB |
22 | #endif |
23 | ||
ce4169a4 RR |
24 | #if wxUSE_FILE |
25 | ||
c801d85f | 26 | // standard |
1c193821 | 27 | #if defined(__WXMSW__) && !defined(__GNUWIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__) |
30a5be97 | 28 | |
a3ef5bf5 | 29 | #ifndef __SALFORDC__ |
49d5d881 VZ |
30 | #define WIN32_LEAN_AND_MEAN |
31 | #define NOSERVICE | |
32 | #define NOIME | |
33 | #define NOATOM | |
34 | #define NOGDI | |
35 | #define NOGDICAPMASKS | |
36 | #define NOMETAFILE | |
37 | #define NOMINMAX | |
38 | #define NOMSG | |
39 | #define NOOPENFILE | |
40 | #define NORASTEROPS | |
41 | #define NOSCROLL | |
42 | #define NOSOUND | |
43 | #define NOSYSMETRICS | |
44 | #define NOTEXTMETRIC | |
45 | #define NOWH | |
46 | #define NOCOMM | |
47 | #define NOKANJI | |
48 | #define NOCRYPT | |
49 | #define NOMCX | |
a3ef5bf5 JS |
50 | #endif |
51 | ||
1c193821 | 52 | #elif defined(__WXMSW__) && defined(__WXWINCE__) |
d109c584 | 53 | #include "wx/msw/missing.h" |
5a3912f2 SN |
54 | #elif (defined(__OS2__)) |
55 | #include <io.h> | |
c801d85f | 56 | #elif (defined(__UNIX__) || defined(__GNUWIN32__)) |
3f4a0c5b | 57 | #include <unistd.h> |
fd938b11 | 58 | #include <time.h> |
732b8386 | 59 | #include <sys/stat.h> |
3f4a0c5b | 60 | #ifdef __GNUWIN32__ |
9ed0d735 | 61 | #include "wx/msw/wrapwin.h" |
3f4a0c5b | 62 | #endif |
d3b4d710 VS |
63 | #elif defined(__DOS__) |
64 | #if defined(__WATCOMC__) | |
65 | #include <io.h> | |
66 | #elif defined(__DJGPP__) | |
67 | #include <io.h> | |
68 | #include <unistd.h> | |
69 | #include <stdio.h> | |
70 | #else | |
71 | #error "Please specify the header with file functions declarations." | |
72 | #endif | |
34138703 | 73 | #elif (defined(__WXSTUBS__)) |
3f4a0c5b VZ |
74 | // Have to ifdef this for different environments |
75 | #include <io.h> | |
17dff81c | 76 | #elif (defined(__WXMAC__)) |
5b781a67 | 77 | #if __MSL__ < 0x6000 |
3f4a0c5b | 78 | int access( const char *path, int mode ) { return 0 ; } |
5b781a67 SC |
79 | #else |
80 | int _access( const char *path, int mode ) { return 0 ; } | |
81 | #endif | |
3f4a0c5b | 82 | char* mktemp( char * path ) { return path ;} |
5b781a67 | 83 | #include <stat.h> |
6294ac2e | 84 | #include <unistd.h> |
c801d85f | 85 | #else |
3f4a0c5b | 86 | #error "Please specify the header with file functions declarations." |
c801d85f KB |
87 | #endif //Win/UNIX |
88 | ||
89 | #include <stdio.h> // SEEK_xxx constants | |
1c193821 | 90 | |
4ea2c29f VZ |
91 | // Windows compilers don't have these constants |
92 | #ifndef W_OK | |
93 | enum | |
94 | { | |
95 | F_OK = 0, // test for existence | |
96 | X_OK = 1, // execute permission | |
97 | W_OK = 2, // write | |
98 | R_OK = 4 // read | |
99 | }; | |
100 | #endif // W_OK | |
34138703 | 101 | |
a3ef5bf5 | 102 | #ifdef __SALFORDC__ |
3f4a0c5b | 103 | #include <unix.h> |
a3ef5bf5 JS |
104 | #endif |
105 | ||
49d5d881 VZ |
106 | // some broken compilers don't have 3rd argument in open() and creat() |
107 | #ifdef __SALFORDC__ | |
108 | #define ACCESS(access) | |
109 | #define stat _stat | |
110 | #else // normal compiler | |
111 | #define ACCESS(access) , (access) | |
112 | #endif // Salford C | |
113 | ||
77ffb593 | 114 | // wxWidgets |
ade35f11 VZ |
115 | #ifndef WX_PRECOMP |
116 | #include "wx/string.h" | |
117 | #include "wx/intl.h" | |
ade35f11 VZ |
118 | #include "wx/log.h" |
119 | #endif // !WX_PRECOMP | |
120 | ||
121 | #include "wx/filename.h" | |
44d568b6 | 122 | #include "wx/file.h" |
4ea2c29f | 123 | #include "wx/filefn.h" |
f6bcfd97 | 124 | |
7cbe148e SN |
125 | // there is no distinction between text and binary files under Unix, so define |
126 | // O_BINARY as 0 if the system headers don't do it already | |
127 | #if defined(__UNIX__) && !defined(O_BINARY) | |
128 | #define O_BINARY (0) | |
129 | #endif //__UNIX__ | |
130 | ||
28f5082b VS |
131 | #ifdef __WXMSW__ |
132 | #include "wx/msw/mslu.h" | |
133 | #endif | |
134 | ||
1c193821 JS |
135 | #ifdef __WXWINCE__ |
136 | #include "wx/msw/private.h" | |
137 | #endif | |
138 | ||
7cbe148e SN |
139 | #ifndef MAX_PATH |
140 | #define MAX_PATH 512 | |
141 | #endif | |
30984dea | 142 | |
c801d85f KB |
143 | // ============================================================================ |
144 | // implementation of wxFile | |
145 | // ============================================================================ | |
146 | ||
147 | // ---------------------------------------------------------------------------- | |
148 | // static functions | |
149 | // ---------------------------------------------------------------------------- | |
4ea2c29f | 150 | |
50920146 | 151 | bool wxFile::Exists(const wxChar *name) |
246037e2 | 152 | { |
4ea2c29f | 153 | return wxFileExists(name); |
d1427b70 VZ |
154 | } |
155 | ||
50920146 | 156 | bool wxFile::Access(const wxChar *name, OpenMode mode) |
d1427b70 | 157 | { |
4ea2c29f VZ |
158 | int how; |
159 | ||
160 | switch ( mode ) | |
161 | { | |
162 | default: | |
163 | wxFAIL_MSG(wxT("bad wxFile::Access mode parameter.")); | |
164 | // fall through | |
d1427b70 | 165 | |
49d5d881 VZ |
166 | case read: |
167 | how = R_OK; | |
168 | break; | |
d1427b70 | 169 | |
49d5d881 VZ |
170 | case write: |
171 | how = W_OK; | |
172 | break; | |
d1427b70 | 173 | |
4ea2c29f VZ |
174 | case read_write: |
175 | how = R_OK | W_OK; | |
176 | break; | |
49d5d881 | 177 | } |
d1427b70 | 178 | |
4ea2c29f | 179 | return wxAccess(name, how) == 0; |
c801d85f KB |
180 | } |
181 | ||
182 | // ---------------------------------------------------------------------------- | |
183 | // opening/closing | |
184 | // ---------------------------------------------------------------------------- | |
185 | ||
186 | // ctors | |
50920146 | 187 | wxFile::wxFile(const wxChar *szFileName, OpenMode mode) |
c801d85f | 188 | { |
49d5d881 | 189 | m_fd = fd_invalid; |
a62848fd | 190 | m_error = false; |
c801d85f | 191 | |
49d5d881 | 192 | Open(szFileName, mode); |
c801d85f KB |
193 | } |
194 | ||
c801d85f | 195 | // create the file, fail if it already exists and bOverwrite |
50920146 | 196 | bool wxFile::Create(const wxChar *szFileName, bool bOverwrite, int accessMode) |
c801d85f | 197 | { |
49d5d881 VZ |
198 | // if bOverwrite we create a new file or truncate the existing one, |
199 | // otherwise we only create the new file and fail if it already exists | |
c4e41ce3 | 200 | #if defined(__WXMAC__) && !defined(__UNIX__) && !wxUSE_UNICODE |
92980e90 | 201 | // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace |
a2b77260 | 202 | // int fd = open( szFileName , O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access); |
92980e90 | 203 | int fd = creat( szFileName , accessMode); |
7c74e7fe | 204 | #else |
92980e90 RR |
205 | int fd = wxOpen( szFileName, |
206 | O_BINARY | O_WRONLY | O_CREAT | | |
207 | (bOverwrite ? O_TRUNC : O_EXCL) | |
208 | ACCESS(accessMode) ); | |
7c74e7fe | 209 | #endif |
92980e90 RR |
210 | if ( fd == -1 ) |
211 | { | |
49d5d881 | 212 | wxLogSysError(_("can't create file '%s'"), szFileName); |
a62848fd | 213 | return false; |
49d5d881 | 214 | } |
769627d7 DS |
215 | |
216 | Attach(fd); | |
217 | return true; | |
c801d85f KB |
218 | } |
219 | ||
220 | // open the file | |
50920146 | 221 | bool wxFile::Open(const wxChar *szFileName, OpenMode mode, int accessMode) |
c801d85f | 222 | { |
49d5d881 | 223 | int flags = O_BINARY; |
c801d85f | 224 | |
92980e90 RR |
225 | switch ( mode ) |
226 | { | |
49d5d881 VZ |
227 | case read: |
228 | flags |= O_RDONLY; | |
229 | break; | |
c801d85f | 230 | |
f6bcfd97 BP |
231 | case write_append: |
232 | if ( wxFile::Exists(szFileName) ) | |
233 | { | |
234 | flags |= O_WRONLY | O_APPEND; | |
235 | break; | |
236 | } | |
237 | //else: fall through as write_append is the same as write if the | |
238 | // file doesn't exist | |
239 | ||
49d5d881 VZ |
240 | case write: |
241 | flags |= O_WRONLY | O_CREAT | O_TRUNC; | |
242 | break; | |
61b02744 | 243 | |
68164137 RL |
244 | case write_excl: |
245 | flags |= O_WRONLY | O_CREAT | O_EXCL; | |
246 | break; | |
247 | ||
49d5d881 VZ |
248 | case read_write: |
249 | flags |= O_RDWR; | |
250 | break; | |
251 | } | |
c801d85f | 252 | |
cc8cc54f VZ |
253 | #ifdef __WINDOWS__ |
254 | // only read/write bits for "all" are supported by this function under | |
255 | // Windows, and VC++ 8 returns EINVAL if any other bits are used in | |
256 | // accessMode, so clear them as they have at best no effect anyhow | |
257 | accessMode &= wxS_IRUSR | wxS_IWUSR; | |
258 | #endif // __WINDOWS__ | |
259 | ||
92980e90 | 260 | int fd = wxOpen( szFileName, flags ACCESS(accessMode)); |
6294ac2e | 261 | |
92980e90 RR |
262 | if ( fd == -1 ) |
263 | { | |
49d5d881 | 264 | wxLogSysError(_("can't open file '%s'"), szFileName); |
a62848fd | 265 | return false; |
49d5d881 | 266 | } |
769627d7 DS |
267 | |
268 | Attach(fd); | |
269 | return true; | |
c801d85f KB |
270 | } |
271 | ||
272 | // close | |
61b02744 | 273 | bool wxFile::Close() |
c801d85f | 274 | { |
49d5d881 | 275 | if ( IsOpened() ) { |
6294ac2e | 276 | if (wxClose(m_fd) == -1) |
1c193821 | 277 | { |
49d5d881 VZ |
278 | wxLogSysError(_("can't close file descriptor %d"), m_fd); |
279 | m_fd = fd_invalid; | |
a62848fd | 280 | return false; |
49d5d881 VZ |
281 | } |
282 | else | |
283 | m_fd = fd_invalid; | |
61b02744 | 284 | } |
61b02744 | 285 | |
a62848fd | 286 | return true; |
c801d85f KB |
287 | } |
288 | ||
289 | // ---------------------------------------------------------------------------- | |
290 | // read/write | |
291 | // ---------------------------------------------------------------------------- | |
292 | ||
293 | // read | |
f8a586e0 | 294 | ssize_t wxFile::Read(void *pBuf, size_t nCount) |
c801d85f | 295 | { |
49d5d881 | 296 | wxCHECK( (pBuf != NULL) && IsOpened(), 0 ); |
c801d85f | 297 | |
30984dea | 298 | ssize_t iRc = wxRead(m_fd, pBuf, nCount); |
6294ac2e | 299 | |
30984dea | 300 | if ( iRc == -1 ) |
02aef13c | 301 | { |
49d5d881 | 302 | wxLogSysError(_("can't read from file descriptor %d"), m_fd); |
f8a586e0 | 303 | return wxInvalidOffset; |
49d5d881 | 304 | } |
02aef13c VZ |
305 | |
306 | return iRc; | |
c801d85f KB |
307 | } |
308 | ||
309 | // write | |
30984dea | 310 | size_t wxFile::Write(const void *pBuf, size_t nCount) |
c801d85f | 311 | { |
49d5d881 | 312 | wxCHECK( (pBuf != NULL) && IsOpened(), 0 ); |
c801d85f | 313 | |
30984dea | 314 | ssize_t iRc = wxWrite(m_fd, pBuf, nCount); |
6294ac2e | 315 | |
30984dea | 316 | if ( iRc == -1 ) |
02aef13c | 317 | { |
49d5d881 | 318 | wxLogSysError(_("can't write to file descriptor %d"), m_fd); |
a62848fd | 319 | m_error = true; |
02aef13c | 320 | iRc = 0; |
49d5d881 | 321 | } |
02aef13c | 322 | |
30984dea | 323 | return iRc; |
c801d85f KB |
324 | } |
325 | ||
b1c67394 VZ |
326 | bool wxFile::Write(const wxString& s, const wxMBConv& conv) |
327 | { | |
328 | const wxWX2MBbuf buf = s.mb_str(conv); | |
329 | if ( !buf ) | |
330 | return false; | |
331 | ||
332 | const size_t size = strlen(buf); // FIXME: use buf.length() when available | |
333 | return Write(buf, size) == size; | |
334 | } | |
335 | ||
c801d85f KB |
336 | // flush |
337 | bool wxFile::Flush() | |
338 | { | |
c8ccc915 | 339 | #ifdef HAVE_FSYNC |
608a34bf VZ |
340 | // fsync() only works on disk files and returns errors for pipes, don't |
341 | // call it then | |
342 | if ( IsOpened() && GetKind() == wxFILE_KIND_DISK ) | |
343 | { | |
f6bcfd97 | 344 | if ( wxFsync(m_fd) == -1 ) |
09914df7 VZ |
345 | { |
346 | wxLogSysError(_("can't flush file descriptor %d"), m_fd); | |
a62848fd | 347 | return false; |
09914df7 | 348 | } |
49d5d881 | 349 | } |
c8ccc915 | 350 | #endif // HAVE_FSYNC |
c801d85f | 351 | |
a62848fd | 352 | return true; |
c801d85f KB |
353 | } |
354 | ||
355 | // ---------------------------------------------------------------------------- | |
356 | // seek | |
357 | // ---------------------------------------------------------------------------- | |
358 | ||
359 | // seek | |
30984dea | 360 | wxFileOffset wxFile::Seek(wxFileOffset ofs, wxSeekMode mode) |
c801d85f | 361 | { |
686a3ee0 VZ |
362 | wxASSERT_MSG( IsOpened(), _T("can't seek on closed file") ); |
363 | wxCHECK_MSG( ofs != wxInvalidOffset || mode != wxFromStart, | |
364 | wxInvalidOffset, | |
365 | _T("invalid absolute file offset") ); | |
49d5d881 | 366 | |
a1b82138 | 367 | int origin; |
49d5d881 | 368 | switch ( mode ) { |
a1b82138 VZ |
369 | default: |
370 | wxFAIL_MSG(_("unknown seek origin")); | |
371 | ||
49d5d881 | 372 | case wxFromStart: |
a1b82138 | 373 | origin = SEEK_SET; |
49d5d881 VZ |
374 | break; |
375 | ||
376 | case wxFromCurrent: | |
a1b82138 | 377 | origin = SEEK_CUR; |
49d5d881 VZ |
378 | break; |
379 | ||
380 | case wxFromEnd: | |
a1b82138 | 381 | origin = SEEK_END; |
49d5d881 | 382 | break; |
49d5d881 VZ |
383 | } |
384 | ||
30984dea | 385 | wxFileOffset iRc = wxSeek(m_fd, ofs, origin); |
02aef13c VZ |
386 | if ( iRc == wxInvalidOffset ) |
387 | { | |
49d5d881 | 388 | wxLogSysError(_("can't seek on file descriptor %d"), m_fd); |
49d5d881 | 389 | } |
02aef13c VZ |
390 | |
391 | return iRc; | |
c801d85f KB |
392 | } |
393 | ||
02aef13c | 394 | // get current file offset |
30984dea | 395 | wxFileOffset wxFile::Tell() const |
c801d85f | 396 | { |
49d5d881 VZ |
397 | wxASSERT( IsOpened() ); |
398 | ||
30984dea | 399 | wxFileOffset iRc = wxTell(m_fd); |
02aef13c VZ |
400 | if ( iRc == wxInvalidOffset ) |
401 | { | |
49d5d881 | 402 | wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd); |
49d5d881 | 403 | } |
02aef13c VZ |
404 | |
405 | return iRc; | |
c801d85f KB |
406 | } |
407 | ||
408 | // get current file length | |
30984dea | 409 | wxFileOffset wxFile::Length() const |
c801d85f | 410 | { |
49d5d881 | 411 | wxASSERT( IsOpened() ); |
c801d85f | 412 | |
30984dea | 413 | wxFileOffset iRc = Tell(); |
02aef13c VZ |
414 | if ( iRc != wxInvalidOffset ) { |
415 | // have to use const_cast :-( | |
30984dea | 416 | wxFileOffset iLen = ((wxFile *)this)->SeekEnd(); |
02aef13c | 417 | if ( iLen != wxInvalidOffset ) { |
49d5d881 | 418 | // restore old position |
02aef13c | 419 | if ( ((wxFile *)this)->Seek(iRc) == wxInvalidOffset ) { |
49d5d881 | 420 | // error |
02aef13c | 421 | iLen = wxInvalidOffset; |
49d5d881 | 422 | } |
c801d85f | 423 | } |
c801d85f | 424 | |
49d5d881 VZ |
425 | iRc = iLen; |
426 | } | |
49d5d881 | 427 | |
02aef13c VZ |
428 | if ( iRc == wxInvalidOffset ) |
429 | { | |
49d5d881 | 430 | wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd); |
c801d85f | 431 | } |
02aef13c VZ |
432 | |
433 | return iRc; | |
c801d85f KB |
434 | } |
435 | ||
436 | // is end of file reached? | |
437 | bool wxFile::Eof() const | |
438 | { | |
49d5d881 | 439 | wxASSERT( IsOpened() ); |
c801d85f | 440 | |
30984dea | 441 | wxFileOffset iRc; |
61b02744 | 442 | |
d3b4d710 | 443 | #if defined(__DOS__) || defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__) |
61b02744 | 444 | // @@ this doesn't work, of course, on unseekable file descriptors |
30984dea | 445 | wxFileOffset ofsCur = Tell(), |
49d5d881 | 446 | ofsMax = Length(); |
1678ad78 | 447 | if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset ) |
02aef13c | 448 | iRc = wxInvalidOffset; |
61b02744 | 449 | else |
49d5d881 VZ |
450 | iRc = ofsCur == ofsMax; |
451 | #else // Windows and "native" compiler | |
6294ac2e | 452 | iRc = wxEof(m_fd); |
49d5d881 | 453 | #endif // Windows/Unix |
c801d85f | 454 | |
b9daf00a WS |
455 | if ( iRc == 1) |
456 | {} | |
457 | else if ( iRc == 0 ) | |
458 | return false; | |
459 | else if ( iRc == wxInvalidOffset ) | |
460 | wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd); | |
461 | else | |
462 | wxFAIL_MSG(_("invalid eof() return value.")); | |
c801d85f | 463 | |
a62848fd | 464 | return true; |
c801d85f KB |
465 | } |
466 | ||
467 | // ============================================================================ | |
468 | // implementation of wxTempFile | |
469 | // ============================================================================ | |
470 | ||
471 | // ---------------------------------------------------------------------------- | |
472 | // construction | |
473 | // ---------------------------------------------------------------------------- | |
44b62d54 | 474 | |
c801d85f KB |
475 | wxTempFile::wxTempFile(const wxString& strName) |
476 | { | |
49d5d881 | 477 | Open(strName); |
c801d85f KB |
478 | } |
479 | ||
480 | bool wxTempFile::Open(const wxString& strName) | |
481 | { | |
44b62d54 VZ |
482 | // we must have an absolute filename because otherwise CreateTempFileName() |
483 | // would create the temp file in $TMP (i.e. the system standard location | |
484 | // for the temp files) which might be on another volume/drive/mount and | |
485 | // wxRename()ing it later to m_strName from Commit() would then fail | |
486 | // | |
487 | // with the absolute filename, the temp file is created in the same | |
488 | // directory as this one which ensures that wxRename() may work later | |
489 | wxFileName fn(strName); | |
490 | if ( !fn.IsAbsolute() ) | |
491 | { | |
492 | fn.Normalize(wxPATH_NORM_ABSOLUTE); | |
493 | } | |
494 | ||
495 | m_strName = fn.GetFullPath(); | |
246037e2 | 496 | |
44b62d54 | 497 | m_strTemp = wxFileName::CreateTempFileName(m_strName, &m_file); |
ade35f11 VZ |
498 | |
499 | if ( m_strTemp.empty() ) | |
500 | { | |
501 | // CreateTempFileName() failed | |
a62848fd | 502 | return false; |
ade35f11 | 503 | } |
49d5d881 | 504 | |
49d5d881 | 505 | #ifdef __UNIX__ |
ade35f11 VZ |
506 | // the temp file should have the same permissions as the original one |
507 | mode_t mode; | |
a62848fd | 508 | |
f6bcfd97 | 509 | wxStructStat st; |
ca11abde | 510 | if ( stat( (const char*) m_strName.fn_str(), &st) == 0 ) |
49d5d881 | 511 | { |
ade35f11 | 512 | mode = st.st_mode; |
49d5d881 VZ |
513 | } |
514 | else | |
515 | { | |
ade35f11 | 516 | // file probably didn't exist, just give it the default mode _using_ |
68164137 | 517 | // user's umask (new files creation should respect umask) |
ade35f11 VZ |
518 | mode_t mask = umask(0777); |
519 | mode = 0666 & ~mask; | |
520 | umask(mask); | |
49d5d881 | 521 | } |
49d5d881 | 522 | |
ca11abde | 523 | if ( chmod( (const char*) m_strTemp.fn_str(), mode) == -1 ) |
fe99e285 | 524 | { |
5a3912f2 | 525 | #ifndef __OS2__ |
ade35f11 | 526 | wxLogSysError(_("Failed to set temporary file permissions")); |
5a3912f2 | 527 | #endif |
fe99e285 | 528 | } |
49d5d881 | 529 | #endif // Unix |
246037e2 | 530 | |
a62848fd | 531 | return true; |
c801d85f KB |
532 | } |
533 | ||
534 | // ---------------------------------------------------------------------------- | |
535 | // destruction | |
536 | // ---------------------------------------------------------------------------- | |
537 | ||
538 | wxTempFile::~wxTempFile() | |
539 | { | |
49d5d881 VZ |
540 | if ( IsOpened() ) |
541 | Discard(); | |
c801d85f KB |
542 | } |
543 | ||
544 | bool wxTempFile::Commit() | |
545 | { | |
49d5d881 | 546 | m_file.Close(); |
c801d85f | 547 | |
f6bcfd97 | 548 | if ( wxFile::Exists(m_strName) && wxRemove(m_strName) != 0 ) { |
49d5d881 | 549 | wxLogSysError(_("can't remove file '%s'"), m_strName.c_str()); |
a62848fd | 550 | return false; |
49d5d881 | 551 | } |
c801d85f | 552 | |
f35746ce | 553 | if ( !wxRenameFile(m_strTemp, m_strName) ) { |
49d5d881 | 554 | wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str()); |
a62848fd | 555 | return false; |
49d5d881 | 556 | } |
c801d85f | 557 | |
a62848fd | 558 | return true; |
c801d85f KB |
559 | } |
560 | ||
561 | void wxTempFile::Discard() | |
562 | { | |
49d5d881 | 563 | m_file.Close(); |
f6bcfd97 | 564 | if ( wxRemove(m_strTemp) != 0 ) |
49d5d881 | 565 | wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str()); |
c801d85f | 566 | } |
ce4169a4 | 567 | |
ade35f11 | 568 | #endif // wxUSE_FILE |
cc985fac | 569 |