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