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