added write_append mode to wxFile, implemented eof() for Unix

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@31 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
1998-05-26 15:05:56 +00:00
parent 99b55eb078
commit 61b027446e
2 changed files with 35 additions and 25 deletions

View File

@@ -50,7 +50,7 @@ public:
// ------------------- // -------------------
// opening mode // opening mode
enum OpenMode { read, write, read_write }; enum OpenMode { read, write, read_write, write_append };
// standard values for file descriptor // standard values for file descriptor
enum { fd_invalid = -1, fd_stdin, fd_stdout, fd_stderr }; enum { fd_invalid = -1, fd_stdin, fd_stdout, fd_stderr };
// seek type // seek type
@@ -72,8 +72,12 @@ public:
// open/close // open/close
bool Create(const char *szFileName, bool bOverwrite = FALSE); bool Create(const char *szFileName, bool bOverwrite = FALSE);
bool Open(const char *szFileName, OpenMode mode = read); bool Open(const char *szFileName, OpenMode mode = read);
inline bool Close(); // Close is a NOP if not opened
// assign an existing file descriptor and get it back from wxFile object
void Attach(int fd) { Close(); m_fd = fd; } void Attach(int fd) { Close(); m_fd = fd; }
inline void Close(); // Close is a NOP if not opened void Detach() { m_fd = fd_invalid; }
int fd() const { return m_fd; }
// read/write (unbuffered) // read/write (unbuffered)
// returns number of bytes read or ofsInvalid on error // returns number of bytes read or ofsInvalid on error

View File

@@ -120,8 +120,7 @@ bool wxFile::Create(const char *szFileName, bool bOverwrite)
{ {
// if bOverwrite we create a new file or truncate the existing one, // if bOverwrite we create a new file or truncate the existing one,
// otherwise we only create the new file and fail if it already exists // otherwise we only create the new file and fail if it already exists
int fd = bOverwrite ? creat(szFileName, 0) int fd = open(szFileName, O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL));
: open(szFileName, O_CREAT | O_EXCL);
if ( fd == -1 ) { if ( fd == -1 ) {
wxLogSysError("can't create file '%s'", szFileName); wxLogSysError("can't create file '%s'", szFileName);
@@ -144,7 +143,11 @@ bool wxFile::Open(const char *szFileName, OpenMode mode)
break; break;
case write: case write:
flags |= O_WRONLY | O_CREAT; flags |= O_WRONLY | O_CREAT | O_TRUNC;
break;
case write_append:
flags |= O_WRONLY | O_APPEND;
break; break;
case read_write: case read_write:
@@ -165,14 +168,19 @@ bool wxFile::Open(const char *szFileName, OpenMode mode)
} }
// close // close
void wxFile::Close() bool wxFile::Close()
{ {
if ( IsOpened() ) { if ( IsOpened() ) {
if ( close(m_fd) == -1 ) if ( close(m_fd) == -1 ) {
wxLogSysError("can't close file descriptor %d", m_fd); wxLogSysError("can't close file descriptor %d", m_fd);
m_fd = fd_invalid;
return FALSE;
}
else
m_fd = fd_invalid; m_fd = fd_invalid;
} }
return TRUE;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -211,7 +219,7 @@ bool wxFile::Write(const void *pBuf, uint nCount)
bool wxFile::Flush() bool wxFile::Flush()
{ {
if ( IsOpened() ) { if ( IsOpened() ) {
// ## fsync() is not ANSI (BSDish) // @@@ fsync() is not ANSI (BSDish)
// if ( fsync(m_fd) == -1 ) { // TODO // if ( fsync(m_fd) == -1 ) { // TODO
if (TRUE) { if (TRUE) {
wxLogSysError("can't flush file descriptor %d", m_fd); wxLogSysError("can't flush file descriptor %d", m_fd);
@@ -282,7 +290,7 @@ off_t wxFile::Length() const
#else #else
int iRc = tell(m_fd); int iRc = tell(m_fd);
if ( iRc != -1 ) { if ( iRc != -1 ) {
// # have to use const_cast :-( // @ have to use const_cast :-(
int iLen = ((wxFile *)this)->SeekEnd(); int iLen = ((wxFile *)this)->SeekEnd();
if ( iLen != -1 ) { if ( iLen != -1 ) {
// restore old position // restore old position
@@ -310,21 +318,19 @@ bool wxFile::Eof() const
{ {
wxASSERT( IsOpened() ); wxASSERT( IsOpened() );
// TODO: no eof in GnuWin32 int iRc;
#ifdef __LINUX__ #if defined(__UNIX__) || defined(__GNUWIN32__)
// @@ this doesn't work, of course, on unseekable file descriptors
int iRc = Tell() == Length(); off_t ofsCur = Tell(),
ofsMax = Length();
#else if ( ofsCur == ofsInvalid || ofsMax == ofsInvalid )
iRc = -1;
#if defined(__GNUWIN32__) else
int iRc = -1; iRc = ofsCur == ofsMax;
#else #else // Windows and "native" compiler
int iRc = eof(m_fd); iRc = eof(m_fd);
#endif #endif // Windows/Unix
#endif
switch ( iRc ) { switch ( iRc ) {
case 1: case 1: