]>
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"
30 #if defined(__WXMSW__) && !defined(__GNUWIN32__)
33 #define WIN32_LEAN_AND_MEAN
53 #include <windows.h> // for GetTempFileName
54 #elif (defined(__UNIX__) || defined(__GNUWIN32__))
56 #elif (defined(__WXSTUBS__))
57 // Have to ifdef this for different environments
60 #error "Please specify the header with file functions declarations."
63 #include <stdio.h> // SEEK_xxx constants
64 #include <fcntl.h> // O_RDONLY &c
66 #include <sys/types.h> // needed for stat
67 #include <sys/stat.h> // stat
70 // Microsoft compiler loves underscores, feed them to it
82 #define access _access
90 #define O_RDONLY _O_RDONLY
91 #define O_WRONLY _O_WRONLY
92 #define O_RDWR _O_RDWR
93 #define O_EXCL _O_EXCL
94 #define O_CREAT _O_CREAT
95 #define O_BINARY _O_BINARY
97 #define S_IFDIR _S_IFDIR
98 #define S_IFREG _S_IFREG
105 #define tell(fd) lseek(fd, 0, SEEK_CUR)
113 // there is no distinction between text and binary files under Unix
119 #include <wx/string.h>
128 // ============================================================================
129 // implementation of wxFile
130 // ============================================================================
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
135 bool wxFile::Exists(const char *name
)
138 return !access(name
, 0) && !stat(name
, &st
) && (st
.st_mode
& S_IFREG
);
141 bool wxFile::Access(const char *name
, OpenMode mode
)
155 wxFAIL_MSG("bad wxFile::Access mode parameter.");
158 return access(name
, how
) == 0;
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
166 wxFile::wxFile(const char *szFileName
, OpenMode mode
)
171 Open(szFileName
, mode
);
180 // create the file, fail if it already exists and bOverwrite
181 bool wxFile::Create(const char *szFileName
, bool bOverwrite
, int access
)
183 // if bOverwrite we create a new file or truncate the existing one,
184 // otherwise we only create the new file and fail if it already exists
185 int fd
= open(szFileName
, O_WRONLY
| O_CREAT
|
186 (bOverwrite
? O_TRUNC
: O_EXCL
), access
);
189 wxLogSysError(_("can't create file '%s'"), szFileName
);
199 bool wxFile::Open(const char *szFileName
, OpenMode mode
, int access
)
201 int flags
= O_BINARY
;
209 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
213 flags
|= O_WRONLY
| O_APPEND
;
221 int fd
= open(szFileName
, flags
, access
);
224 wxLogSysError(_("can't open file '%s'"), szFileName
);
237 if ( close(m_fd
) == -1 ) {
238 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
254 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
256 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
259 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
261 int iRc
= ::read(m_fd
, pBuf
, nCount
);
264 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
265 return wxInvalidOffset
;
272 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
274 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
277 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
279 int iRc
= ::write(m_fd
, pBuf
, nCount
);
282 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
294 // @@@ fsync() is not ANSI (BSDish)
295 // if ( fsync(m_fd) == -1 ) { // TODO
297 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
305 // ----------------------------------------------------------------------------
307 // ----------------------------------------------------------------------------
310 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
312 wxASSERT( IsOpened() );
329 wxFAIL_MSG(_("unknown seek origin"));
332 int iRc
= lseek(m_fd
, ofs
, flag
);
334 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
335 return wxInvalidOffset
;
342 off_t
wxFile::Tell() const
344 wxASSERT( IsOpened() );
346 int iRc
= tell(m_fd
);
348 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
349 return wxInvalidOffset
;
355 // get current file length
356 off_t
wxFile::Length() const
358 wxASSERT( IsOpened() );
360 #if defined( _MSC_VER ) && !defined( __MWERKS__ )
361 int iRc
= _filelength(m_fd
);
363 int iRc
= tell(m_fd
);
365 // @ have to use const_cast :-(
366 int iLen
= ((wxFile
*)this)->SeekEnd();
368 // restore old position
369 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
381 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
382 return wxInvalidOffset
;
388 // is end of file reached?
389 bool wxFile::Eof() const
391 wxASSERT( IsOpened() );
395 #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ )
396 // @@ this doesn't work, of course, on unseekable file descriptors
397 off_t ofsCur
= Tell(),
399 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
402 iRc
= ofsCur
== ofsMax
;
403 #else // Windows and "native" compiler
405 #endif // Windows/Unix
415 wxLogSysError(_("can't determine if the end of file is reached on \
416 descriptor %d"), m_fd
);
420 wxFAIL_MSG(_("invalid eof() return value."));
426 // ============================================================================
427 // implementation of wxTempFile
428 // ============================================================================
430 // ----------------------------------------------------------------------------
432 // ----------------------------------------------------------------------------
433 wxTempFile::wxTempFile(const wxString
& strName
)
438 bool wxTempFile::Open(const wxString
& strName
)
442 // we want to create the file in the same directory as strName because
443 // otherwise rename() in Commit() might not work (if the files are on
444 // different partitions for example). Unfortunately, the only standard
445 // (POSIX) temp file creation function tmpnam() can't do it.
446 #if defined(__UNIX__) || defined(__WXSTUBS__)
447 static const char *szMktempSuffix
= "XXXXXX";
448 m_strTemp
<< strName
<< szMktempSuffix
;
449 mktemp((char *)m_strTemp
.c_str()); // will do because length doesn't change
452 wxSplitPath(strName
, &strPath
, NULL
, NULL
);
453 if ( strPath
.IsEmpty() )
454 strPath
= '.'; // GetTempFileName will fail if we give it empty string
456 if ( !GetTempFileName(strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
458 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
459 if ( !GetTempFileName((BYTE
) (const char*) strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
461 wxLogLastError("GetTempFileName");
462 m_strTemp
.UngetWriteBuf();
463 #endif // Windows/Unix
465 return m_file
.Open(m_strTemp
, wxFile::write
);
468 // ----------------------------------------------------------------------------
470 // ----------------------------------------------------------------------------
472 wxTempFile::~wxTempFile()
478 bool wxTempFile::Commit()
482 if ( wxFile::Exists(m_strName
) && remove(m_strName
) != 0 ) {
483 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
487 if ( rename(m_strTemp
, m_strName
) != 0 ) {
488 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
495 void wxTempFile::Discard()
498 if ( remove(m_strTemp
) != 0 )
499 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());