]>
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> | |
246037e2 | 10 | // Licence: wxWindows license |
c801d85f KB |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
13 | // ---------------------------------------------------------------------------- | |
14 | // headers | |
15 | // ---------------------------------------------------------------------------- | |
16 | ||
17 | #ifdef __GNUG__ | |
3f4a0c5b | 18 | #pragma implementation "file.h" |
c801d85f KB |
19 | #endif |
20 | ||
21 | // For compilers that support precompilation, includes "wx.h". | |
22 | #include "wx/wxprec.h" | |
c801d85f KB |
23 | |
24 | #ifdef __BORLANDC__ | |
ce4169a4 | 25 | #pragma hdrstop |
c801d85f KB |
26 | #endif |
27 | ||
ce4169a4 RR |
28 | #if wxUSE_FILE |
29 | ||
c801d85f | 30 | // standard |
5ea105e0 | 31 | #if defined(__WXMSW__) && !defined(__GNUWIN32__) && !defined(__WXWINE__) |
c801d85f | 32 | #include <io.h> |
30a5be97 | 33 | |
a3ef5bf5 | 34 | #ifndef __SALFORDC__ |
49d5d881 VZ |
35 | #define WIN32_LEAN_AND_MEAN |
36 | #define NOSERVICE | |
37 | #define NOIME | |
38 | #define NOATOM | |
39 | #define NOGDI | |
40 | #define NOGDICAPMASKS | |
41 | #define NOMETAFILE | |
42 | #define NOMINMAX | |
43 | #define NOMSG | |
44 | #define NOOPENFILE | |
45 | #define NORASTEROPS | |
46 | #define NOSCROLL | |
47 | #define NOSOUND | |
48 | #define NOSYSMETRICS | |
49 | #define NOTEXTMETRIC | |
50 | #define NOWH | |
51 | #define NOCOMM | |
52 | #define NOKANJI | |
53 | #define NOCRYPT | |
54 | #define NOMCX | |
a3ef5bf5 JS |
55 | #endif |
56 | ||
49d5d881 | 57 | #include <windows.h> // for GetTempFileName |
c801d85f | 58 | #elif (defined(__UNIX__) || defined(__GNUWIN32__)) |
3f4a0c5b VZ |
59 | #include <unistd.h> |
60 | #ifdef __GNUWIN32__ | |
61 | #include <windows.h> | |
62 | #endif | |
34138703 | 63 | #elif (defined(__WXSTUBS__)) |
3f4a0c5b VZ |
64 | // Have to ifdef this for different environments |
65 | #include <io.h> | |
17dff81c | 66 | #elif (defined(__WXMAC__)) |
3f4a0c5b VZ |
67 | int access( const char *path, int mode ) { return 0 ; } |
68 | char* mktemp( char * path ) { return path ;} | |
69 | #include <unistd.h> | |
70 | #include <unix.h> | |
71 | #define W_OK 2 | |
72 | #define R_OK 4 | |
c801d85f | 73 | #else |
3f4a0c5b | 74 | #error "Please specify the header with file functions declarations." |
c801d85f KB |
75 | #endif //Win/UNIX |
76 | ||
77 | #include <stdio.h> // SEEK_xxx constants | |
78 | #include <fcntl.h> // O_RDONLY &c | |
a3ef5bf5 | 79 | |
469e1e5c | 80 | #ifndef __MWERKS__ |
3f4a0c5b VZ |
81 | #include <sys/types.h> // needed for stat |
82 | #include <sys/stat.h> // stat | |
469e1e5c | 83 | #endif |
c801d85f KB |
84 | |
85 | // Microsoft compiler loves underscores, feed them to it | |
3f4a0c5b | 86 | #ifdef __VISUALC__ |
49d5d881 VZ |
87 | // functions |
88 | #define open _open | |
89 | #define close _close | |
90 | #define read _read | |
91 | #define write _write | |
92 | #define lseek _lseek | |
93 | #define fsync _commit | |
94 | #define access _access | |
95 | #define eof _eof | |
96 | ||
97 | // types | |
98 | #define stat _stat | |
99 | ||
100 | // constants | |
101 | ||
102 | #define O_RDONLY _O_RDONLY | |
103 | #define O_WRONLY _O_WRONLY | |
104 | #define O_RDWR _O_RDWR | |
105 | #define O_EXCL _O_EXCL | |
106 | #define O_CREAT _O_CREAT | |
107 | #define O_BINARY _O_BINARY | |
108 | ||
109 | #define S_IFDIR _S_IFDIR | |
110 | #define S_IFREG _S_IFREG | |
c801d85f | 111 | #else |
49d5d881 | 112 | #define tell(fd) lseek(fd, 0, SEEK_CUR) |
3f4a0c5b | 113 | #endif // VC++ |
c801d85f | 114 | |
3f4a0c5b | 115 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
49d5d881 VZ |
116 | #define W_OK 2 |
117 | #define R_OK 4 | |
34138703 JS |
118 | #endif |
119 | ||
c801d85f | 120 | // there is no distinction between text and binary files under Unix |
246037e2 | 121 | #ifdef __UNIX__ |
49d5d881 | 122 | #define O_BINARY (0) |
246037e2 | 123 | #endif //__UNIX__ |
c801d85f | 124 | |
a3ef5bf5 | 125 | #ifdef __SALFORDC__ |
3f4a0c5b | 126 | #include <unix.h> |
a3ef5bf5 JS |
127 | #endif |
128 | ||
c801d85f KB |
129 | // wxWindows |
130 | #include <wx/string.h> | |
131 | #include <wx/intl.h> | |
132 | #include <wx/file.h> | |
133 | #include <wx/log.h> | |
134 | ||
81d66cf3 | 135 | #ifndef MAX_PATH |
3f4a0c5b | 136 | #define MAX_PATH 512 |
81d66cf3 | 137 | #endif |
c801d85f | 138 | |
169935ad | 139 | #ifdef __WXMAC__ |
3f4a0c5b VZ |
140 | char gwxMacFileName[ MAX_PATH ] ; |
141 | char gwxMacFileName2[ MAX_PATH ] ; | |
142 | char gwxMacFileName3[ MAX_PATH ] ; | |
169935ad SC |
143 | #endif |
144 | ||
49d5d881 VZ |
145 | // some broken compilers don't have 3rd argument in open() and creat() |
146 | #ifdef __SALFORDC__ | |
147 | #define ACCESS(access) | |
148 | #define stat _stat | |
149 | #else // normal compiler | |
150 | #define ACCESS(access) , (access) | |
151 | #endif // Salford C | |
152 | ||
c801d85f KB |
153 | // ============================================================================ |
154 | // implementation of wxFile | |
155 | // ============================================================================ | |
156 | ||
157 | // ---------------------------------------------------------------------------- | |
158 | // static functions | |
159 | // ---------------------------------------------------------------------------- | |
50920146 | 160 | bool wxFile::Exists(const wxChar *name) |
246037e2 | 161 | { |
49d5d881 | 162 | struct stat st; |
50920146 | 163 | #if wxUSE_UNICODE && wxMBFILES |
dcf924a3 | 164 | wxCharBuffer fname = wxConvFile.cWC2MB(name); |
a3ef5bf5 | 165 | |
50920146 OK |
166 | return !access(fname, 0) && |
167 | !stat(MBSTRINGCAST fname, &st) && | |
168 | (st.st_mode & S_IFREG); | |
169 | #else | |
49d5d881 | 170 | return !access(name, 0) && |
50920146 | 171 | !stat((wxChar*) name, &st) && |
49d5d881 | 172 | (st.st_mode & S_IFREG); |
50920146 | 173 | #endif |
d1427b70 VZ |
174 | } |
175 | ||
50920146 | 176 | bool wxFile::Access(const wxChar *name, OpenMode mode) |
d1427b70 | 177 | { |
49d5d881 | 178 | int how = 0; |
d1427b70 | 179 | |
49d5d881 VZ |
180 | switch ( mode ) { |
181 | case read: | |
182 | how = R_OK; | |
183 | break; | |
d1427b70 | 184 | |
49d5d881 VZ |
185 | case write: |
186 | how = W_OK; | |
187 | break; | |
d1427b70 | 188 | |
49d5d881 | 189 | default: |
50920146 | 190 | wxFAIL_MSG(_T("bad wxFile::Access mode parameter.")); |
49d5d881 | 191 | } |
d1427b70 | 192 | |
50920146 | 193 | return access(wxFNCONV(name), how) == 0; |
c801d85f KB |
194 | } |
195 | ||
196 | // ---------------------------------------------------------------------------- | |
197 | // opening/closing | |
198 | // ---------------------------------------------------------------------------- | |
199 | ||
200 | // ctors | |
50920146 | 201 | wxFile::wxFile(const wxChar *szFileName, OpenMode mode) |
c801d85f | 202 | { |
49d5d881 VZ |
203 | m_fd = fd_invalid; |
204 | m_error = FALSE; | |
c801d85f | 205 | |
49d5d881 | 206 | Open(szFileName, mode); |
c801d85f KB |
207 | } |
208 | ||
209 | // dtor | |
210 | wxFile::~wxFile() | |
211 | { | |
49d5d881 | 212 | Close(); |
c801d85f KB |
213 | } |
214 | ||
215 | // create the file, fail if it already exists and bOverwrite | |
50920146 | 216 | bool wxFile::Create(const wxChar *szFileName, bool bOverwrite, int accessMode) |
c801d85f | 217 | { |
49d5d881 VZ |
218 | // if bOverwrite we create a new file or truncate the existing one, |
219 | // otherwise we only create the new file and fail if it already exists | |
50920146 | 220 | int fd = open(wxFNCONV(szFileName), |
49d5d881 VZ |
221 | O_WRONLY | O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL) |
222 | ACCESS(accessMode)); | |
223 | ||
224 | if ( fd == -1 ) { | |
225 | wxLogSysError(_("can't create file '%s'"), szFileName); | |
226 | return FALSE; | |
227 | } | |
228 | else { | |
229 | Attach(fd); | |
230 | return TRUE; | |
231 | } | |
c801d85f KB |
232 | } |
233 | ||
234 | // open the file | |
50920146 | 235 | bool wxFile::Open(const wxChar *szFileName, OpenMode mode, int accessMode) |
c801d85f | 236 | { |
49d5d881 | 237 | int flags = O_BINARY; |
c801d85f | 238 | |
49d5d881 VZ |
239 | switch ( mode ) { |
240 | case read: | |
241 | flags |= O_RDONLY; | |
242 | break; | |
c801d85f | 243 | |
49d5d881 VZ |
244 | case write: |
245 | flags |= O_WRONLY | O_CREAT | O_TRUNC; | |
246 | break; | |
61b02744 | 247 | |
49d5d881 VZ |
248 | case write_append: |
249 | flags |= O_WRONLY | O_APPEND; | |
250 | break; | |
c801d85f | 251 | |
49d5d881 VZ |
252 | case read_write: |
253 | flags |= O_RDWR; | |
254 | break; | |
255 | } | |
c801d85f | 256 | |
50920146 | 257 | int fd = open(wxFNCONV(szFileName), flags ACCESS(accessMode)); |
c801d85f | 258 | |
49d5d881 VZ |
259 | if ( fd == -1 ) { |
260 | wxLogSysError(_("can't open file '%s'"), szFileName); | |
261 | return FALSE; | |
262 | } | |
263 | else { | |
264 | Attach(fd); | |
265 | return TRUE; | |
266 | } | |
c801d85f KB |
267 | } |
268 | ||
269 | // close | |
61b02744 | 270 | bool wxFile::Close() |
c801d85f | 271 | { |
49d5d881 VZ |
272 | if ( IsOpened() ) { |
273 | if ( close(m_fd) == -1 ) { | |
274 | wxLogSysError(_("can't close file descriptor %d"), m_fd); | |
275 | m_fd = fd_invalid; | |
276 | return FALSE; | |
277 | } | |
278 | else | |
279 | m_fd = fd_invalid; | |
61b02744 | 280 | } |
61b02744 | 281 | |
49d5d881 | 282 | return TRUE; |
c801d85f KB |
283 | } |
284 | ||
285 | // ---------------------------------------------------------------------------- | |
286 | // read/write | |
287 | // ---------------------------------------------------------------------------- | |
288 | ||
289 | // read | |
290 | off_t wxFile::Read(void *pBuf, off_t nCount) | |
291 | { | |
49d5d881 | 292 | wxCHECK( (pBuf != NULL) && IsOpened(), 0 ); |
c801d85f | 293 | |
469e1e5c | 294 | #ifdef __MWERKS__ |
49d5d881 | 295 | int iRc = ::read(m_fd, (char*) pBuf, nCount); |
469e1e5c | 296 | #else |
49d5d881 | 297 | int iRc = ::read(m_fd, pBuf, nCount); |
469e1e5c | 298 | #endif |
49d5d881 VZ |
299 | if ( iRc == -1 ) { |
300 | wxLogSysError(_("can't read from file descriptor %d"), m_fd); | |
301 | return wxInvalidOffset; | |
302 | } | |
303 | else | |
304 | return (size_t)iRc; | |
c801d85f KB |
305 | } |
306 | ||
307 | // write | |
c86f1403 | 308 | size_t wxFile::Write(const void *pBuf, size_t nCount) |
c801d85f | 309 | { |
49d5d881 | 310 | wxCHECK( (pBuf != NULL) && IsOpened(), 0 ); |
c801d85f | 311 | |
469e1e5c | 312 | #ifdef __MWERKS__ |
49d5d881 | 313 | int iRc = ::write(m_fd, (const char*) pBuf, nCount); |
469e1e5c | 314 | #else |
49d5d881 | 315 | int iRc = ::write(m_fd, pBuf, nCount); |
469e1e5c | 316 | #endif |
49d5d881 VZ |
317 | if ( iRc == -1 ) { |
318 | wxLogSysError(_("can't write to file descriptor %d"), m_fd); | |
319 | m_error = TRUE; | |
320 | return 0; | |
321 | } | |
322 | else | |
323 | return iRc; | |
c801d85f KB |
324 | } |
325 | ||
326 | // flush | |
327 | bool wxFile::Flush() | |
328 | { | |
49d5d881 VZ |
329 | if ( IsOpened() ) { |
330 | #if defined(__VISUALC__) || wxHAVE_FSYNC | |
09914df7 VZ |
331 | if ( fsync(m_fd) == -1 ) |
332 | { | |
333 | wxLogSysError(_("can't flush file descriptor %d"), m_fd); | |
334 | return FALSE; | |
335 | } | |
49d5d881 | 336 | #else // no fsync |
09914df7 | 337 | // just do nothing |
49d5d881 VZ |
338 | #endif // fsync |
339 | } | |
c801d85f | 340 | |
49d5d881 | 341 | return TRUE; |
c801d85f KB |
342 | } |
343 | ||
344 | // ---------------------------------------------------------------------------- | |
345 | // seek | |
346 | // ---------------------------------------------------------------------------- | |
347 | ||
348 | // seek | |
79c3e0e1 | 349 | off_t wxFile::Seek(off_t ofs, wxSeekMode mode) |
c801d85f | 350 | { |
49d5d881 VZ |
351 | wxASSERT( IsOpened() ); |
352 | ||
353 | int flag = -1; | |
354 | switch ( mode ) { | |
355 | case wxFromStart: | |
356 | flag = SEEK_SET; | |
357 | break; | |
358 | ||
359 | case wxFromCurrent: | |
360 | flag = SEEK_CUR; | |
361 | break; | |
362 | ||
363 | case wxFromEnd: | |
364 | flag = SEEK_END; | |
365 | break; | |
366 | ||
367 | default: | |
368 | wxFAIL_MSG(_("unknown seek origin")); | |
369 | } | |
370 | ||
371 | int iRc = lseek(m_fd, ofs, flag); | |
372 | if ( iRc == -1 ) { | |
373 | wxLogSysError(_("can't seek on file descriptor %d"), m_fd); | |
374 | return wxInvalidOffset; | |
375 | } | |
376 | else | |
377 | return (off_t)iRc; | |
c801d85f KB |
378 | } |
379 | ||
380 | // get current off_t | |
381 | off_t wxFile::Tell() const | |
382 | { | |
49d5d881 VZ |
383 | wxASSERT( IsOpened() ); |
384 | ||
385 | int iRc = tell(m_fd); | |
386 | if ( iRc == -1 ) { | |
387 | wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd); | |
388 | return wxInvalidOffset; | |
389 | } | |
390 | else | |
391 | return (off_t)iRc; | |
c801d85f KB |
392 | } |
393 | ||
394 | // get current file length | |
395 | off_t wxFile::Length() const | |
396 | { | |
49d5d881 | 397 | wxASSERT( IsOpened() ); |
c801d85f | 398 | |
49d5d881 | 399 | #ifdef __VISUALC__ |
c801d85f | 400 | int iRc = _filelength(m_fd); |
49d5d881 | 401 | #else // !VC++ |
c801d85f KB |
402 | int iRc = tell(m_fd); |
403 | if ( iRc != -1 ) { | |
49d5d881 VZ |
404 | // @ have to use const_cast :-( |
405 | int iLen = ((wxFile *)this)->SeekEnd(); | |
406 | if ( iLen != -1 ) { | |
407 | // restore old position | |
408 | if ( ((wxFile *)this)->Seek(iRc) == -1 ) { | |
409 | // error | |
410 | iLen = -1; | |
411 | } | |
c801d85f | 412 | } |
c801d85f | 413 | |
49d5d881 VZ |
414 | iRc = iLen; |
415 | } | |
416 | #endif // VC++ | |
417 | ||
418 | if ( iRc == -1 ) { | |
419 | wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd); | |
420 | return wxInvalidOffset; | |
c801d85f | 421 | } |
49d5d881 VZ |
422 | else |
423 | return (off_t)iRc; | |
c801d85f KB |
424 | } |
425 | ||
426 | // is end of file reached? | |
427 | bool wxFile::Eof() const | |
428 | { | |
49d5d881 | 429 | wxASSERT( IsOpened() ); |
c801d85f | 430 | |
49d5d881 | 431 | int iRc; |
61b02744 | 432 | |
49d5d881 | 433 | #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__) |
61b02744 VZ |
434 | // @@ this doesn't work, of course, on unseekable file descriptors |
435 | off_t ofsCur = Tell(), | |
49d5d881 | 436 | ofsMax = Length(); |
1678ad78 | 437 | if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset ) |
49d5d881 | 438 | iRc = -1; |
61b02744 | 439 | else |
49d5d881 VZ |
440 | iRc = ofsCur == ofsMax; |
441 | #else // Windows and "native" compiler | |
61b02744 | 442 | iRc = eof(m_fd); |
49d5d881 | 443 | #endif // Windows/Unix |
c801d85f | 444 | |
49d5d881 VZ |
445 | switch ( iRc ) { |
446 | case 1: | |
447 | break; | |
c801d85f | 448 | |
49d5d881 VZ |
449 | case 0: |
450 | return FALSE; | |
c801d85f | 451 | |
49d5d881 VZ |
452 | case -1: |
453 | wxLogSysError(_("can't determine if the end of file is reached on \ | |
454 | descriptor %d"), m_fd); | |
455 | break; | |
c801d85f | 456 | |
49d5d881 VZ |
457 | default: |
458 | wxFAIL_MSG(_("invalid eof() return value.")); | |
459 | } | |
c801d85f | 460 | |
49d5d881 | 461 | return TRUE; |
c801d85f KB |
462 | } |
463 | ||
464 | // ============================================================================ | |
465 | // implementation of wxTempFile | |
466 | // ============================================================================ | |
467 | ||
468 | // ---------------------------------------------------------------------------- | |
469 | // construction | |
470 | // ---------------------------------------------------------------------------- | |
471 | wxTempFile::wxTempFile(const wxString& strName) | |
472 | { | |
49d5d881 | 473 | Open(strName); |
c801d85f KB |
474 | } |
475 | ||
476 | bool wxTempFile::Open(const wxString& strName) | |
477 | { | |
49d5d881 | 478 | m_strName = strName; |
246037e2 | 479 | |
49d5d881 VZ |
480 | // we want to create the file in the same directory as strName because |
481 | // otherwise rename() in Commit() might not work (if the files are on | |
482 | // different partitions for example). Unfortunately, the only standard | |
483 | // (POSIX) temp file creation function tmpnam() can't do it. | |
484 | #if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ ) | |
50920146 | 485 | static const wxChar *szMktempSuffix = _T("XXXXXX"); |
b59650be | 486 | m_strTemp << strName << szMktempSuffix; |
50920146 | 487 | mktemp(MBSTRINGCAST m_strTemp.mb_str()); // will do because length doesn't change |
49d5d881 | 488 | #else // Windows |
30a5be97 VZ |
489 | wxString strPath; |
490 | wxSplitPath(strName, &strPath, NULL, NULL); | |
491 | if ( strPath.IsEmpty() ) | |
50920146 | 492 | strPath = _T('.'); // GetTempFileName will fail if we give it empty string |
81d66cf3 | 493 | #ifdef __WIN32__ |
50920146 | 494 | if ( !GetTempFileName(strPath, _T("wx_"),0, m_strTemp.GetWriteBuf(MAX_PATH)) ) |
81d66cf3 | 495 | #else |
49d5d881 | 496 | // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug? |
50920146 | 497 | if ( !GetTempFileName((BYTE) (const wxChar*) strPath, _T("wx_"),0, m_strTemp.GetWriteBuf(MAX_PATH)) ) |
81d66cf3 | 498 | #endif |
50920146 | 499 | wxLogLastError(_T("GetTempFileName")); |
30a5be97 | 500 | m_strTemp.UngetWriteBuf(); |
49d5d881 VZ |
501 | #endif // Windows/Unix |
502 | ||
503 | int access = wxS_DEFAULT; | |
504 | #ifdef __UNIX__ | |
505 | // create the file with the same mode as the original one under Unix | |
f46f1bc6 | 506 | mode_t umaskOld = 0; // just to suppress compiler warning |
fe99e285 VZ |
507 | bool changedUmask; |
508 | ||
49d5d881 | 509 | struct stat st; |
50920146 | 510 | if ( stat(strName.fn_str(), &st) == 0 ) |
49d5d881 VZ |
511 | { |
512 | // this assumes that only lower bits of st_mode contain the access | |
513 | // rights, but it's true for at least all Unices which have S_IXXXX() | |
514 | // macros, so should not be less portable than using (not POSIX) | |
515 | // S_IFREG &c | |
516 | access = st.st_mode & 0777; | |
fe99e285 VZ |
517 | |
518 | // we want to create the file with exactly the same access rights as | |
519 | // the original one, so disable the user's umask for the moment | |
520 | umaskOld = umask(0); | |
521 | changedUmask = TRUE; | |
49d5d881 VZ |
522 | } |
523 | else | |
524 | { | |
fe99e285 VZ |
525 | // file probably didn't exist, just create with default mode _using_ |
526 | // user's umask (new files creation should respet umask) | |
cedda7e6 | 527 | changedUmask = FALSE; |
49d5d881 | 528 | } |
49d5d881 VZ |
529 | #endif // Unix |
530 | ||
cedda7e6 | 531 | bool ok = m_file.Open(m_strTemp, wxFile::write, access); |
49d5d881 VZ |
532 | |
533 | #ifdef __UNIX__ | |
fe99e285 VZ |
534 | if ( changedUmask ) |
535 | { | |
536 | // restore umask now that the file is created | |
537 | (void)umask(umaskOld); | |
538 | } | |
49d5d881 | 539 | #endif // Unix |
246037e2 | 540 | |
49d5d881 | 541 | return ok; |
c801d85f KB |
542 | } |
543 | ||
544 | // ---------------------------------------------------------------------------- | |
545 | // destruction | |
546 | // ---------------------------------------------------------------------------- | |
547 | ||
548 | wxTempFile::~wxTempFile() | |
549 | { | |
49d5d881 VZ |
550 | if ( IsOpened() ) |
551 | Discard(); | |
c801d85f KB |
552 | } |
553 | ||
554 | bool wxTempFile::Commit() | |
555 | { | |
49d5d881 | 556 | m_file.Close(); |
c801d85f | 557 | |
50920146 | 558 | if ( wxFile::Exists(m_strName) && remove(m_strName.fn_str()) != 0 ) { |
49d5d881 VZ |
559 | wxLogSysError(_("can't remove file '%s'"), m_strName.c_str()); |
560 | return FALSE; | |
561 | } | |
c801d85f | 562 | |
50920146 | 563 | if ( rename(m_strTemp.fn_str(), m_strName.fn_str()) != 0 ) { |
49d5d881 VZ |
564 | wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str()); |
565 | return FALSE; | |
566 | } | |
c801d85f | 567 | |
49d5d881 | 568 | return TRUE; |
c801d85f KB |
569 | } |
570 | ||
571 | void wxTempFile::Discard() | |
572 | { | |
49d5d881 | 573 | m_file.Close(); |
50920146 | 574 | if ( remove(m_strTemp.fn_str()) != 0 ) |
49d5d881 | 575 | wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str()); |
c801d85f | 576 | } |
ce4169a4 | 577 | |
cc985fac PA |
578 | #endif |
579 |