]> git.saurik.com Git - wxWidgets.git/blame - src/common/file.cpp
wxFileSystem now compiles if wxUSE_HTML (required by wxHTML)
[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>
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
c2ff79b1
DW
63#elif (defined(__WXPM__))
64 #include <io.h>
65 #include <direct.h>
66 #define W_OK 2
67 #define R_OK 4
34138703 68#elif (defined(__WXSTUBS__))
3f4a0c5b
VZ
69 // Have to ifdef this for different environments
70 #include <io.h>
17dff81c 71#elif (defined(__WXMAC__))
3f4a0c5b
VZ
72 int access( const char *path, int mode ) { return 0 ; }
73 char* mktemp( char * path ) { return path ;}
74 #include <unistd.h>
75 #include <unix.h>
76 #define W_OK 2
77 #define R_OK 4
c801d85f 78#else
3f4a0c5b 79 #error "Please specify the header with file functions declarations."
c801d85f
KB
80#endif //Win/UNIX
81
82#include <stdio.h> // SEEK_xxx constants
83#include <fcntl.h> // O_RDONLY &c
a3ef5bf5 84
469e1e5c 85#ifndef __MWERKS__
3f4a0c5b
VZ
86 #include <sys/types.h> // needed for stat
87 #include <sys/stat.h> // stat
469e1e5c 88#endif
c801d85f
KB
89
90// Microsoft compiler loves underscores, feed them to it
3f4a0c5b 91#ifdef __VISUALC__
49d5d881
VZ
92 // functions
93 #define open _open
94 #define close _close
95 #define read _read
96 #define write _write
97 #define lseek _lseek
98 #define fsync _commit
99 #define access _access
100 #define eof _eof
101
102 // types
103 #define stat _stat
104
105 // constants
106
107 #define O_RDONLY _O_RDONLY
108 #define O_WRONLY _O_WRONLY
109 #define O_RDWR _O_RDWR
110 #define O_EXCL _O_EXCL
111 #define O_CREAT _O_CREAT
112 #define O_BINARY _O_BINARY
113
114 #define S_IFDIR _S_IFDIR
115 #define S_IFREG _S_IFREG
c801d85f 116#else
49d5d881 117 #define tell(fd) lseek(fd, 0, SEEK_CUR)
3f4a0c5b 118#endif // VC++
c801d85f 119
3f4a0c5b 120#if defined(__BORLANDC__) || defined(_MSC_VER)
49d5d881
VZ
121 #define W_OK 2
122 #define R_OK 4
34138703
JS
123#endif
124
b12915c1
VZ
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)
49d5d881 128 #define O_BINARY (0)
246037e2 129#endif //__UNIX__
c801d85f 130
a3ef5bf5 131#ifdef __SALFORDC__
3f4a0c5b 132 #include <unix.h>
a3ef5bf5
JS
133#endif
134
c801d85f 135// wxWindows
3096bd2f
VZ
136#include "wx/string.h"
137#include "wx/intl.h"
138#include "wx/file.h"
139#include "wx/log.h"
c801d85f 140
81d66cf3 141#ifndef MAX_PATH
3f4a0c5b 142 #define MAX_PATH 512
81d66cf3 143#endif
c801d85f 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 160bool 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
7c74e7fe
SC
166#ifdef __WXMAC__
167 return !access(wxUnix2MacFilename( name ) , 0) && !stat(wxUnix2MacFilename( name ), &st) && (st.st_mode & S_IFREG);
168#else
50920146 169 return !access(fname, 0) &&
e90c1d2a 170 !stat(wxMBSTRINGCAST fname, &st) &&
50920146 171 (st.st_mode & S_IFREG);
7c74e7fe
SC
172#endif
173#else
174#ifdef __WXMAC__
175 return !access(wxUnix2MacFilename( name ) , 0) && !stat(wxUnix2MacFilename( name ), &st) && (st.st_mode & S_IFREG);
50920146 176#else
49d5d881 177 return !access(name, 0) &&
50920146 178 !stat((wxChar*) name, &st) &&
49d5d881 179 (st.st_mode & S_IFREG);
50920146 180#endif
7c74e7fe 181#endif
d1427b70
VZ
182}
183
50920146 184bool wxFile::Access(const wxChar *name, OpenMode mode)
d1427b70 185{
49d5d881 186 int how = 0;
d1427b70 187
49d5d881
VZ
188 switch ( mode ) {
189 case read:
190 how = R_OK;
191 break;
d1427b70 192
49d5d881
VZ
193 case write:
194 how = W_OK;
195 break;
d1427b70 196
49d5d881 197 default:
223d09f6 198 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
49d5d881 199 }
d1427b70 200
50920146 201 return access(wxFNCONV(name), how) == 0;
c801d85f
KB
202}
203
204// ----------------------------------------------------------------------------
205// opening/closing
206// ----------------------------------------------------------------------------
207
208// ctors
50920146 209wxFile::wxFile(const wxChar *szFileName, OpenMode mode)
c801d85f 210{
49d5d881
VZ
211 m_fd = fd_invalid;
212 m_error = FALSE;
c801d85f 213
49d5d881 214 Open(szFileName, mode);
c801d85f
KB
215}
216
c801d85f 217// create the file, fail if it already exists and bOverwrite
50920146 218bool wxFile::Create(const wxChar *szFileName, bool bOverwrite, int accessMode)
c801d85f 219{
49d5d881
VZ
220 // if bOverwrite we create a new file or truncate the existing one,
221 // otherwise we only create the new file and fail if it already exists
7c74e7fe
SC
222#ifdef __WXMAC__
223 int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
224#else
50920146 225 int fd = open(wxFNCONV(szFileName),
49d5d881
VZ
226 O_WRONLY | O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL)
227 ACCESS(accessMode));
7c74e7fe 228#endif
49d5d881
VZ
229 if ( fd == -1 ) {
230 wxLogSysError(_("can't create file '%s'"), szFileName);
231 return FALSE;
232 }
233 else {
234 Attach(fd);
235 return TRUE;
236 }
c801d85f
KB
237}
238
239// open the file
50920146 240bool wxFile::Open(const wxChar *szFileName, OpenMode mode, int accessMode)
c801d85f 241{
49d5d881 242 int flags = O_BINARY;
c801d85f 243
49d5d881
VZ
244 switch ( mode ) {
245 case read:
246 flags |= O_RDONLY;
247 break;
c801d85f 248
49d5d881
VZ
249 case write:
250 flags |= O_WRONLY | O_CREAT | O_TRUNC;
251 break;
61b02744 252
49d5d881
VZ
253 case write_append:
254 flags |= O_WRONLY | O_APPEND;
255 break;
c801d85f 256
49d5d881
VZ
257 case read_write:
258 flags |= O_RDWR;
259 break;
260 }
c801d85f 261
7c74e7fe
SC
262#ifdef __WXMAC__
263 int fd = open(wxUnix2MacFilename( szFileName ), flags, access);
264#else
50920146 265 int fd = open(wxFNCONV(szFileName), flags ACCESS(accessMode));
7c74e7fe 266#endif
49d5d881
VZ
267 if ( fd == -1 ) {
268 wxLogSysError(_("can't open file '%s'"), szFileName);
269 return FALSE;
270 }
271 else {
272 Attach(fd);
273 return TRUE;
274 }
c801d85f
KB
275}
276
277// close
61b02744 278bool wxFile::Close()
c801d85f 279{
49d5d881
VZ
280 if ( IsOpened() ) {
281 if ( close(m_fd) == -1 ) {
282 wxLogSysError(_("can't close file descriptor %d"), m_fd);
283 m_fd = fd_invalid;
284 return FALSE;
285 }
286 else
287 m_fd = fd_invalid;
61b02744 288 }
61b02744 289
49d5d881 290 return TRUE;
c801d85f
KB
291}
292
293// ----------------------------------------------------------------------------
294// read/write
295// ----------------------------------------------------------------------------
296
297// read
298off_t wxFile::Read(void *pBuf, off_t nCount)
299{
49d5d881 300 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 301
469e1e5c 302#ifdef __MWERKS__
49d5d881 303 int iRc = ::read(m_fd, (char*) pBuf, nCount);
469e1e5c 304#else
49d5d881 305 int iRc = ::read(m_fd, pBuf, nCount);
469e1e5c 306#endif
49d5d881
VZ
307 if ( iRc == -1 ) {
308 wxLogSysError(_("can't read from file descriptor %d"), m_fd);
309 return wxInvalidOffset;
310 }
311 else
312 return (size_t)iRc;
c801d85f
KB
313}
314
315// write
c86f1403 316size_t wxFile::Write(const void *pBuf, size_t nCount)
c801d85f 317{
49d5d881 318 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 319
469e1e5c 320#ifdef __MWERKS__
49d5d881 321 int iRc = ::write(m_fd, (const char*) pBuf, nCount);
469e1e5c 322#else
49d5d881 323 int iRc = ::write(m_fd, pBuf, nCount);
469e1e5c 324#endif
49d5d881
VZ
325 if ( iRc == -1 ) {
326 wxLogSysError(_("can't write to file descriptor %d"), m_fd);
327 m_error = TRUE;
328 return 0;
329 }
330 else
331 return iRc;
c801d85f
KB
332}
333
334// flush
335bool wxFile::Flush()
336{
49d5d881
VZ
337 if ( IsOpened() ) {
338#if defined(__VISUALC__) || wxHAVE_FSYNC
09914df7
VZ
339 if ( fsync(m_fd) == -1 )
340 {
341 wxLogSysError(_("can't flush file descriptor %d"), m_fd);
342 return FALSE;
343 }
49d5d881 344#else // no fsync
09914df7 345 // just do nothing
49d5d881
VZ
346#endif // fsync
347 }
c801d85f 348
49d5d881 349 return TRUE;
c801d85f
KB
350}
351
352// ----------------------------------------------------------------------------
353// seek
354// ----------------------------------------------------------------------------
355
356// seek
79c3e0e1 357off_t wxFile::Seek(off_t 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
a1b82138 379 int iRc = lseek(m_fd, ofs, origin);
49d5d881
VZ
380 if ( iRc == -1 ) {
381 wxLogSysError(_("can't seek on file descriptor %d"), m_fd);
382 return wxInvalidOffset;
383 }
384 else
385 return (off_t)iRc;
c801d85f
KB
386}
387
388// get current off_t
389off_t wxFile::Tell() const
390{
49d5d881
VZ
391 wxASSERT( IsOpened() );
392
393 int iRc = tell(m_fd);
394 if ( iRc == -1 ) {
395 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd);
396 return wxInvalidOffset;
397 }
398 else
399 return (off_t)iRc;
c801d85f
KB
400}
401
402// get current file length
403off_t wxFile::Length() const
404{
49d5d881 405 wxASSERT( IsOpened() );
c801d85f 406
49d5d881 407#ifdef __VISUALC__
c801d85f 408 int iRc = _filelength(m_fd);
49d5d881 409#else // !VC++
c801d85f
KB
410 int iRc = tell(m_fd);
411 if ( iRc != -1 ) {
49d5d881
VZ
412 // @ have to use const_cast :-(
413 int iLen = ((wxFile *)this)->SeekEnd();
414 if ( iLen != -1 ) {
415 // restore old position
416 if ( ((wxFile *)this)->Seek(iRc) == -1 ) {
417 // error
418 iLen = -1;
419 }
c801d85f 420 }
c801d85f 421
49d5d881
VZ
422 iRc = iLen;
423 }
424#endif // VC++
425
426 if ( iRc == -1 ) {
427 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd);
428 return wxInvalidOffset;
c801d85f 429 }
49d5d881
VZ
430 else
431 return (off_t)iRc;
c801d85f
KB
432}
433
434// is end of file reached?
435bool wxFile::Eof() const
436{
49d5d881 437 wxASSERT( IsOpened() );
c801d85f 438
49d5d881 439 int iRc;
61b02744 440
49d5d881 441#if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
61b02744
VZ
442 // @@ this doesn't work, of course, on unseekable file descriptors
443 off_t ofsCur = Tell(),
49d5d881 444 ofsMax = Length();
1678ad78 445 if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset )
49d5d881 446 iRc = -1;
61b02744 447 else
49d5d881
VZ
448 iRc = ofsCur == ofsMax;
449#else // Windows and "native" compiler
61b02744 450 iRc = eof(m_fd);
49d5d881 451#endif // Windows/Unix
c801d85f 452
49d5d881
VZ
453 switch ( iRc ) {
454 case 1:
455 break;
c801d85f 456
49d5d881
VZ
457 case 0:
458 return FALSE;
c801d85f 459
49d5d881
VZ
460 case -1:
461 wxLogSysError(_("can't determine if the end of file is reached on \
462 descriptor %d"), m_fd);
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// ----------------------------------------------------------------------------
479wxTempFile::wxTempFile(const wxString& strName)
480{
49d5d881 481 Open(strName);
c801d85f
KB
482}
483
484bool wxTempFile::Open(const wxString& strName)
485{
49d5d881 486 m_strName = strName;
246037e2 487
49d5d881
VZ
488 // we want to create the file in the same directory as strName because
489 // otherwise rename() in Commit() might not work (if the files are on
490 // different partitions for example). Unfortunately, the only standard
491 // (POSIX) temp file creation function tmpnam() can't do it.
492#if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ )
223d09f6 493 static const wxChar *szMktempSuffix = wxT("XXXXXX");
b59650be 494 m_strTemp << strName << szMktempSuffix;
e90c1d2a
VZ
495 // can use the cast because length doesn't change
496 mktemp(wxMBSTRINGCAST m_strTemp.mb_str());
c2ff79b1
DW
497#elif defined(__WXPM__)
498 // for now just create a file
499 // future enhancements can be to set some extended attributes for file systems
500 // OS/2 supports that have them (HPFS, FAT32) and security (HPFS386)
223d09f6 501 static const wxChar *szMktempSuffix = wxT("XXX");
c2ff79b1
DW
502 m_strTemp << strName << szMktempSuffix;
503 mkdir(m_strTemp.GetWriteBuf(MAX_PATH));
49d5d881 504#else // Windows
30a5be97
VZ
505 wxString strPath;
506 wxSplitPath(strName, &strPath, NULL, NULL);
507 if ( strPath.IsEmpty() )
223d09f6 508 strPath = wxT('.'); // GetTempFileName will fail if we give it empty string
81d66cf3 509#ifdef __WIN32__
223d09f6 510 if ( !GetTempFileName(strPath, wxT("wx_"),0, m_strTemp.GetWriteBuf(MAX_PATH)) )
81d66cf3 511#else
49d5d881 512 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
223d09f6 513 if ( !GetTempFileName((BYTE) (DWORD)(const wxChar*) strPath, wxT("wx_"),0, m_strTemp.GetWriteBuf(MAX_PATH)) )
81d66cf3 514#endif
223d09f6 515 wxLogLastError(wxT("GetTempFileName"));
30a5be97 516 m_strTemp.UngetWriteBuf();
49d5d881
VZ
517#endif // Windows/Unix
518
519 int access = wxS_DEFAULT;
520#ifdef __UNIX__
521 // create the file with the same mode as the original one under Unix
f46f1bc6 522 mode_t umaskOld = 0; // just to suppress compiler warning
fe99e285
VZ
523 bool changedUmask;
524
49d5d881 525 struct stat st;
50920146 526 if ( stat(strName.fn_str(), &st) == 0 )
49d5d881
VZ
527 {
528 // this assumes that only lower bits of st_mode contain the access
529 // rights, but it's true for at least all Unices which have S_IXXXX()
530 // macros, so should not be less portable than using (not POSIX)
531 // S_IFREG &c
532 access = st.st_mode & 0777;
fe99e285
VZ
533
534 // we want to create the file with exactly the same access rights as
535 // the original one, so disable the user's umask for the moment
536 umaskOld = umask(0);
537 changedUmask = TRUE;
49d5d881
VZ
538 }
539 else
540 {
fe99e285
VZ
541 // file probably didn't exist, just create with default mode _using_
542 // user's umask (new files creation should respet umask)
cedda7e6 543 changedUmask = FALSE;
49d5d881 544 }
49d5d881
VZ
545#endif // Unix
546
cedda7e6 547 bool ok = m_file.Open(m_strTemp, wxFile::write, access);
49d5d881
VZ
548
549#ifdef __UNIX__
fe99e285
VZ
550 if ( changedUmask )
551 {
552 // restore umask now that the file is created
553 (void)umask(umaskOld);
554 }
49d5d881 555#endif // Unix
246037e2 556
49d5d881 557 return ok;
c801d85f
KB
558}
559
560// ----------------------------------------------------------------------------
561// destruction
562// ----------------------------------------------------------------------------
563
564wxTempFile::~wxTempFile()
565{
49d5d881
VZ
566 if ( IsOpened() )
567 Discard();
c801d85f
KB
568}
569
570bool wxTempFile::Commit()
571{
49d5d881 572 m_file.Close();
c801d85f 573
7c74e7fe 574#ifndef __WXMAC__
50920146 575 if ( wxFile::Exists(m_strName) && remove(m_strName.fn_str()) != 0 ) {
49d5d881
VZ
576 wxLogSysError(_("can't remove file '%s'"), m_strName.c_str());
577 return FALSE;
578 }
c801d85f 579
50920146 580 if ( rename(m_strTemp.fn_str(), m_strName.fn_str()) != 0 ) {
49d5d881
VZ
581 wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str());
582 return FALSE;
583 }
7c74e7fe
SC
584#else
585 if ( wxFile::Exists(m_strName) && remove(wxUnix2MacFilename( m_strName )) != 0 ) {
586 wxLogSysError(_("can't remove file '%s'"), m_strName.c_str());
587 return FALSE;
588 }
589
590 if ( rename(wxUnix2MacFilename( m_strTemp ), wxUnix2MacFilename( m_strName )) != 0 ) {
591 wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str());
592 return FALSE;
593 }
594#endif
c801d85f 595
49d5d881 596 return TRUE;
c801d85f
KB
597}
598
599void wxTempFile::Discard()
600{
49d5d881 601 m_file.Close();
7c74e7fe 602#ifndef __WXMAC__
50920146 603 if ( remove(m_strTemp.fn_str()) != 0 )
49d5d881 604 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str());
7c74e7fe
SC
605#else
606 if ( remove( wxUnix2MacFilename(m_strTemp.fn_str())) != 0 )
607 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str());
608#endif
c801d85f 609}
ce4169a4 610
cc985fac
PA
611#endif
612