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