]>
git.saurik.com Git - wxWidgets.git/blob - src/common/file.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFile - encapsulates low-level "file descriptor"
5 // Author: Vadim Zeitlin
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
18 #pragma implementation "file.h"
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
35 #if defined(__WXMSW__) && !defined(__GNUWIN32__) && !defined(__WXWINE__)
39 #define WIN32_LEAN_AND_MEAN
61 #include <windows.h> // for GetTempFileName
62 #elif (defined(__UNIX__) || defined(__GNUWIN32__))
67 #elif (defined(__WXSTUBS__))
68 // Have to ifdef this for different environments
70 #elif (defined(__WXMAC__))
71 int access( const char *path
, int mode
) { return 0 ; }
72 char* mktemp( char * path
) { return path
;}
78 #error "Please specify the header with file functions declarations."
81 #include <stdio.h> // SEEK_xxx constants
82 #include <fcntl.h> // O_RDONLY &c
85 #include <sys/types.h> // needed for stat
86 #include <sys/stat.h> // stat
89 // Microsoft compiler loves underscores, feed them to it
98 #define access _access
106 #define O_RDONLY _O_RDONLY
107 #define O_WRONLY _O_WRONLY
108 #define O_RDWR _O_RDWR
109 #define O_EXCL _O_EXCL
110 #define O_CREAT _O_CREAT
111 #define O_BINARY _O_BINARY
113 #define S_IFDIR _S_IFDIR
114 #define S_IFREG _S_IFREG
116 #define tell(fd) lseek(fd, 0, SEEK_CUR)
119 #if defined(__BORLANDC__) || defined(_MSC_VER)
124 // there is no distinction between text and binary files under Unix
134 #include <wx/string.h>
144 char gwxMacFileName
[ MAX_PATH
] ;
145 char gwxMacFileName2
[ MAX_PATH
] ;
146 char gwxMacFileName3
[ MAX_PATH
] ;
149 // some broken compilers don't have 3rd argument in open() and creat()
151 #define ACCESS(access)
153 #else // normal compiler
154 #define ACCESS(access) , (access)
157 // ============================================================================
158 // implementation of wxFile
159 // ============================================================================
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
164 bool wxFile::Exists(const wxChar
*name
)
167 #if wxUSE_UNICODE && wxMBFILES
168 wxCharBuffer fname
= wxConvFile
.cWC2MB(name
);
170 return !access(fname
, 0) &&
171 !stat(MBSTRINGCAST fname
, &st
) &&
172 (st
.st_mode
& S_IFREG
);
174 return !access(name
, 0) &&
175 !stat((wxChar
*) name
, &st
) &&
176 (st
.st_mode
& S_IFREG
);
180 bool wxFile::Access(const wxChar
*name
, OpenMode mode
)
194 wxFAIL_MSG(_T("bad wxFile::Access mode parameter."));
197 return access(wxFNCONV(name
), how
) == 0;
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
205 wxFile::wxFile(const wxChar
*szFileName
, OpenMode mode
)
210 Open(szFileName
, mode
);
219 // create the file, fail if it already exists and bOverwrite
220 bool wxFile::Create(const wxChar
*szFileName
, bool bOverwrite
, int accessMode
)
222 // if bOverwrite we create a new file or truncate the existing one,
223 // otherwise we only create the new file and fail if it already exists
224 int fd
= open(wxFNCONV(szFileName
),
225 O_WRONLY
| O_CREAT
| (bOverwrite
? O_TRUNC
: O_EXCL
)
229 wxLogSysError(_("can't create file '%s'"), szFileName
);
239 bool wxFile::Open(const wxChar
*szFileName
, OpenMode mode
, int accessMode
)
241 int flags
= O_BINARY
;
249 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
253 flags
|= O_WRONLY
| O_APPEND
;
261 int fd
= open(wxFNCONV(szFileName
), flags
ACCESS(accessMode
));
264 wxLogSysError(_("can't open file '%s'"), szFileName
);
277 if ( close(m_fd
) == -1 ) {
278 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
289 // ----------------------------------------------------------------------------
291 // ----------------------------------------------------------------------------
294 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
296 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
299 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
301 int iRc
= ::read(m_fd
, pBuf
, nCount
);
304 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
305 return wxInvalidOffset
;
312 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
314 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
317 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
319 int iRc
= ::write(m_fd
, pBuf
, nCount
);
322 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
334 #if defined(__VISUALC__) || wxHAVE_FSYNC
335 if ( fsync(m_fd
) == -1 )
337 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
348 // ----------------------------------------------------------------------------
350 // ----------------------------------------------------------------------------
353 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
355 wxASSERT( IsOpened() );
372 wxFAIL_MSG(_("unknown seek origin"));
375 int iRc
= lseek(m_fd
, ofs
, flag
);
377 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
378 return wxInvalidOffset
;
385 off_t
wxFile::Tell() const
387 wxASSERT( IsOpened() );
389 int iRc
= tell(m_fd
);
391 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
392 return wxInvalidOffset
;
398 // get current file length
399 off_t
wxFile::Length() const
401 wxASSERT( IsOpened() );
404 int iRc
= _filelength(m_fd
);
406 int iRc
= tell(m_fd
);
408 // @ have to use const_cast :-(
409 int iLen
= ((wxFile
*)this)->SeekEnd();
411 // restore old position
412 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
423 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
424 return wxInvalidOffset
;
430 // is end of file reached?
431 bool wxFile::Eof() const
433 wxASSERT( IsOpened() );
437 #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
438 // @@ this doesn't work, of course, on unseekable file descriptors
439 off_t ofsCur
= Tell(),
441 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
444 iRc
= ofsCur
== ofsMax
;
445 #else // Windows and "native" compiler
447 #endif // Windows/Unix
457 wxLogSysError(_("can't determine if the end of file is reached on \
458 descriptor %d"), m_fd
);
462 wxFAIL_MSG(_("invalid eof() return value."));
468 // ============================================================================
469 // implementation of wxTempFile
470 // ============================================================================
472 // ----------------------------------------------------------------------------
474 // ----------------------------------------------------------------------------
475 wxTempFile::wxTempFile(const wxString
& strName
)
480 bool wxTempFile::Open(const wxString
& strName
)
484 // we want to create the file in the same directory as strName because
485 // otherwise rename() in Commit() might not work (if the files are on
486 // different partitions for example). Unfortunately, the only standard
487 // (POSIX) temp file creation function tmpnam() can't do it.
488 #if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ )
489 static const wxChar
*szMktempSuffix
= _T("XXXXXX");
490 m_strTemp
<< strName
<< szMktempSuffix
;
491 mktemp(MBSTRINGCAST m_strTemp
.mb_str()); // will do because length doesn't change
494 wxSplitPath(strName
, &strPath
, NULL
, NULL
);
495 if ( strPath
.IsEmpty() )
496 strPath
= _T('.'); // GetTempFileName will fail if we give it empty string
498 if ( !GetTempFileName(strPath
, _T("wx_"),0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
500 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
501 if ( !GetTempFileName((BYTE
) (const wxChar
*) strPath
, _T("wx_"),0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
503 wxLogLastError(_T("GetTempFileName"));
504 m_strTemp
.UngetWriteBuf();
505 #endif // Windows/Unix
507 int access
= wxS_DEFAULT
;
509 // create the file with the same mode as the original one under Unix
510 mode_t umaskOld
= 0; // just to suppress compiler warning
514 if ( stat(strName
.fn_str(), &st
) == 0 )
516 // this assumes that only lower bits of st_mode contain the access
517 // rights, but it's true for at least all Unices which have S_IXXXX()
518 // macros, so should not be less portable than using (not POSIX)
520 access
= st
.st_mode
& 0777;
522 // we want to create the file with exactly the same access rights as
523 // the original one, so disable the user's umask for the moment
529 // file probably didn't exist, just create with default mode _using_
530 // user's umask (new files creation should respet umask)
531 changedUmask
= FALSE
;
535 bool ok
= m_file
.Open(m_strTemp
, wxFile::write
, access
);
540 // restore umask now that the file is created
541 (void)umask(umaskOld
);
548 // ----------------------------------------------------------------------------
550 // ----------------------------------------------------------------------------
552 wxTempFile::~wxTempFile()
558 bool wxTempFile::Commit()
562 if ( wxFile::Exists(m_strName
) && remove(m_strName
.fn_str()) != 0 ) {
563 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
567 if ( rename(m_strTemp
.fn_str(), m_strName
.fn_str()) != 0 ) {
568 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
575 void wxTempFile::Discard()
578 if ( remove(m_strTemp
.fn_str()) != 0 )
579 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());