]> git.saurik.com Git - wxWidgets.git/blame - src/common/file.cpp
fix compilation when one of wxUSE_LISTCTRL and wxUSE_TREECTRL is defined but the...
[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
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 151bool wxFile::Exists(const wxChar *name)
246037e2 152{
4ea2c29f 153 return wxFileExists(name);
d1427b70
VZ
154}
155
50920146 156bool 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 187wxFile::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 196bool 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 221bool 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 273bool 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 294ssize_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 310size_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
326// flush
327bool wxFile::Flush()
328{
c8ccc915 329#ifdef HAVE_FSYNC
608a34bf
VZ
330 // fsync() only works on disk files and returns errors for pipes, don't
331 // call it then
332 if ( IsOpened() && GetKind() == wxFILE_KIND_DISK )
333 {
f6bcfd97 334 if ( wxFsync(m_fd) == -1 )
09914df7
VZ
335 {
336 wxLogSysError(_("can't flush file descriptor %d"), m_fd);
a62848fd 337 return false;
09914df7 338 }
49d5d881 339 }
c8ccc915 340#endif // HAVE_FSYNC
c801d85f 341
a62848fd 342 return true;
c801d85f
KB
343}
344
345// ----------------------------------------------------------------------------
346// seek
347// ----------------------------------------------------------------------------
348
349// seek
30984dea 350wxFileOffset wxFile::Seek(wxFileOffset ofs, wxSeekMode mode)
c801d85f 351{
686a3ee0
VZ
352 wxASSERT_MSG( IsOpened(), _T("can't seek on closed file") );
353 wxCHECK_MSG( ofs != wxInvalidOffset || mode != wxFromStart,
354 wxInvalidOffset,
355 _T("invalid absolute file offset") );
49d5d881 356
a1b82138 357 int origin;
49d5d881 358 switch ( mode ) {
a1b82138
VZ
359 default:
360 wxFAIL_MSG(_("unknown seek origin"));
361
49d5d881 362 case wxFromStart:
a1b82138 363 origin = SEEK_SET;
49d5d881
VZ
364 break;
365
366 case wxFromCurrent:
a1b82138 367 origin = SEEK_CUR;
49d5d881
VZ
368 break;
369
370 case wxFromEnd:
a1b82138 371 origin = SEEK_END;
49d5d881 372 break;
49d5d881
VZ
373 }
374
30984dea 375 wxFileOffset iRc = wxSeek(m_fd, ofs, origin);
02aef13c
VZ
376 if ( iRc == wxInvalidOffset )
377 {
49d5d881 378 wxLogSysError(_("can't seek on file descriptor %d"), m_fd);
49d5d881 379 }
02aef13c
VZ
380
381 return iRc;
c801d85f
KB
382}
383
02aef13c 384// get current file offset
30984dea 385wxFileOffset wxFile::Tell() const
c801d85f 386{
49d5d881
VZ
387 wxASSERT( IsOpened() );
388
30984dea 389 wxFileOffset iRc = wxTell(m_fd);
02aef13c
VZ
390 if ( iRc == wxInvalidOffset )
391 {
49d5d881 392 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd);
49d5d881 393 }
02aef13c
VZ
394
395 return iRc;
c801d85f
KB
396}
397
398// get current file length
30984dea 399wxFileOffset wxFile::Length() const
c801d85f 400{
49d5d881 401 wxASSERT( IsOpened() );
c801d85f 402
30984dea 403 wxFileOffset iRc = Tell();
02aef13c
VZ
404 if ( iRc != wxInvalidOffset ) {
405 // have to use const_cast :-(
30984dea 406 wxFileOffset iLen = ((wxFile *)this)->SeekEnd();
02aef13c 407 if ( iLen != wxInvalidOffset ) {
49d5d881 408 // restore old position
02aef13c 409 if ( ((wxFile *)this)->Seek(iRc) == wxInvalidOffset ) {
49d5d881 410 // error
02aef13c 411 iLen = wxInvalidOffset;
49d5d881 412 }
c801d85f 413 }
c801d85f 414
49d5d881
VZ
415 iRc = iLen;
416 }
49d5d881 417
02aef13c
VZ
418 if ( iRc == wxInvalidOffset )
419 {
49d5d881 420 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd);
c801d85f 421 }
02aef13c
VZ
422
423 return iRc;
c801d85f
KB
424}
425
426// is end of file reached?
427bool wxFile::Eof() const
428{
49d5d881 429 wxASSERT( IsOpened() );
c801d85f 430
30984dea 431 wxFileOffset iRc;
61b02744 432
d3b4d710 433#if defined(__DOS__) || defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
61b02744 434 // @@ this doesn't work, of course, on unseekable file descriptors
30984dea 435 wxFileOffset ofsCur = Tell(),
49d5d881 436 ofsMax = Length();
1678ad78 437 if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset )
02aef13c 438 iRc = wxInvalidOffset;
61b02744 439 else
49d5d881
VZ
440 iRc = ofsCur == ofsMax;
441#else // Windows and "native" compiler
6294ac2e 442 iRc = wxEof(m_fd);
49d5d881 443#endif // Windows/Unix
c801d85f 444
b9daf00a
WS
445 if ( iRc == 1)
446 {}
447 else if ( iRc == 0 )
448 return false;
449 else if ( iRc == wxInvalidOffset )
450 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd);
451 else
452 wxFAIL_MSG(_("invalid eof() return value."));
c801d85f 453
a62848fd 454 return true;
c801d85f
KB
455}
456
457// ============================================================================
458// implementation of wxTempFile
459// ============================================================================
460
461// ----------------------------------------------------------------------------
462// construction
463// ----------------------------------------------------------------------------
44b62d54 464
c801d85f
KB
465wxTempFile::wxTempFile(const wxString& strName)
466{
49d5d881 467 Open(strName);
c801d85f
KB
468}
469
470bool wxTempFile::Open(const wxString& strName)
471{
44b62d54
VZ
472 // we must have an absolute filename because otherwise CreateTempFileName()
473 // would create the temp file in $TMP (i.e. the system standard location
474 // for the temp files) which might be on another volume/drive/mount and
475 // wxRename()ing it later to m_strName from Commit() would then fail
476 //
477 // with the absolute filename, the temp file is created in the same
478 // directory as this one which ensures that wxRename() may work later
479 wxFileName fn(strName);
480 if ( !fn.IsAbsolute() )
481 {
482 fn.Normalize(wxPATH_NORM_ABSOLUTE);
483 }
484
485 m_strName = fn.GetFullPath();
246037e2 486
44b62d54 487 m_strTemp = wxFileName::CreateTempFileName(m_strName, &m_file);
ade35f11
VZ
488
489 if ( m_strTemp.empty() )
490 {
491 // CreateTempFileName() failed
a62848fd 492 return false;
ade35f11 493 }
49d5d881 494
49d5d881 495#ifdef __UNIX__
ade35f11
VZ
496 // the temp file should have the same permissions as the original one
497 mode_t mode;
a62848fd 498
f6bcfd97 499 wxStructStat st;
ca11abde 500 if ( stat( (const char*) m_strName.fn_str(), &st) == 0 )
49d5d881 501 {
ade35f11 502 mode = st.st_mode;
49d5d881
VZ
503 }
504 else
505 {
ade35f11 506 // file probably didn't exist, just give it the default mode _using_
68164137 507 // user's umask (new files creation should respect umask)
ade35f11
VZ
508 mode_t mask = umask(0777);
509 mode = 0666 & ~mask;
510 umask(mask);
49d5d881 511 }
49d5d881 512
ca11abde 513 if ( chmod( (const char*) m_strTemp.fn_str(), mode) == -1 )
fe99e285 514 {
5a3912f2 515#ifndef __OS2__
ade35f11 516 wxLogSysError(_("Failed to set temporary file permissions"));
5a3912f2 517#endif
fe99e285 518 }
49d5d881 519#endif // Unix
246037e2 520
a62848fd 521 return true;
c801d85f
KB
522}
523
524// ----------------------------------------------------------------------------
525// destruction
526// ----------------------------------------------------------------------------
527
528wxTempFile::~wxTempFile()
529{
49d5d881
VZ
530 if ( IsOpened() )
531 Discard();
c801d85f
KB
532}
533
534bool wxTempFile::Commit()
535{
49d5d881 536 m_file.Close();
c801d85f 537
f6bcfd97 538 if ( wxFile::Exists(m_strName) && wxRemove(m_strName) != 0 ) {
49d5d881 539 wxLogSysError(_("can't remove file '%s'"), m_strName.c_str());
a62848fd 540 return false;
49d5d881 541 }
c801d85f 542
f35746ce 543 if ( !wxRenameFile(m_strTemp, m_strName) ) {
49d5d881 544 wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str());
a62848fd 545 return false;
49d5d881 546 }
c801d85f 547
a62848fd 548 return true;
c801d85f
KB
549}
550
551void wxTempFile::Discard()
552{
49d5d881 553 m_file.Close();
f6bcfd97 554 if ( wxRemove(m_strTemp) != 0 )
49d5d881 555 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str());
c801d85f 556}
ce4169a4 557
ade35f11 558#endif // wxUSE_FILE
cc985fac 559