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