]>
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__))
59 #elif (defined(__WXSTUBS__))
60 // Have to ifdef this for different environments
62 #elif (defined(__WXMAC__))
63 int access( const char *path
, int mode
) { return 0 ; }
64 char* mktemp( char * path
) { return path
;}
70 #error "Please specify the header with file functions declarations."
73 #include <stdio.h> // SEEK_xxx constants
74 #include <fcntl.h> // O_RDONLY &c
76 #include <sys/types.h> // needed for stat
77 #include <sys/stat.h> // stat
80 // Microsoft compiler loves underscores, feed them to it
92 #define access _access
100 #define O_RDONLY _O_RDONLY
101 #define O_WRONLY _O_WRONLY
102 #define O_RDWR _O_RDWR
103 #define O_EXCL _O_EXCL
104 #define O_CREAT _O_CREAT
105 #define O_BINARY _O_BINARY
107 #define S_IFDIR _S_IFDIR
108 #define S_IFREG _S_IFREG
115 #define tell(fd) lseek(fd, 0, SEEK_CUR)
123 // there is no distinction between text and binary files under Unix
129 #include <wx/string.h>
138 // ============================================================================
139 // implementation of wxFile
140 // ============================================================================
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
145 bool wxFile::Exists(const char *name
)
148 return !access(name
, 0) && !stat(name
, &st
) && (st
.st_mode
& S_IFREG
);
151 bool wxFile::Access(const char *name
, OpenMode mode
)
165 wxFAIL_MSG("bad wxFile::Access mode parameter.");
168 return access(name
, how
) == 0;
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
176 wxFile::wxFile(const char *szFileName
, OpenMode mode
)
181 Open(szFileName
, mode
);
190 // create the file, fail if it already exists and bOverwrite
191 bool wxFile::Create(const char *szFileName
, bool bOverwrite
, int access
)
193 // if bOverwrite we create a new file or truncate the existing one,
194 // otherwise we only create the new file and fail if it already exists
195 int fd
= open(szFileName
, O_WRONLY
| O_CREAT
|
196 (bOverwrite
? O_TRUNC
: O_EXCL
), access
);
199 wxLogSysError(_("can't create file '%s'"), szFileName
);
209 bool wxFile::Open(const char *szFileName
, OpenMode mode
, int access
)
211 int flags
= O_BINARY
;
219 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
223 flags
|= O_WRONLY
| O_APPEND
;
231 int fd
= open(szFileName
, flags
, access
);
234 wxLogSysError(_("can't open file '%s'"), szFileName
);
247 if ( close(m_fd
) == -1 ) {
248 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
264 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
266 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
269 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
271 int iRc
= ::read(m_fd
, pBuf
, nCount
);
274 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
275 return wxInvalidOffset
;
282 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
284 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
287 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
289 int iRc
= ::write(m_fd
, pBuf
, nCount
);
292 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
304 #if defined(_MSC_VER) || wxHAVE_FSYNC
305 if ( fsync(m_fd
) == -1 )
307 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
323 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
325 wxASSERT( IsOpened() );
342 wxFAIL_MSG(_("unknown seek origin"));
345 int iRc
= lseek(m_fd
, ofs
, flag
);
347 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
348 return wxInvalidOffset
;
355 off_t
wxFile::Tell() const
357 wxASSERT( IsOpened() );
359 int iRc
= tell(m_fd
);
361 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
362 return wxInvalidOffset
;
368 // get current file length
369 off_t
wxFile::Length() const
371 wxASSERT( IsOpened() );
373 #if defined( _MSC_VER ) && !defined( __MWERKS__ )
374 int iRc
= _filelength(m_fd
);
376 int iRc
= tell(m_fd
);
378 // @ have to use const_cast :-(
379 int iLen
= ((wxFile
*)this)->SeekEnd();
381 // restore old position
382 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
394 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
395 return wxInvalidOffset
;
401 // is end of file reached?
402 bool wxFile::Eof() const
404 wxASSERT( IsOpened() );
408 #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ )
409 // @@ this doesn't work, of course, on unseekable file descriptors
410 off_t ofsCur
= Tell(),
412 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
415 iRc
= ofsCur
== ofsMax
;
416 #else // Windows and "native" compiler
418 #endif // Windows/Unix
428 wxLogSysError(_("can't determine if the end of file is reached on \
429 descriptor %d"), m_fd
);
433 wxFAIL_MSG(_("invalid eof() return value."));
439 // ============================================================================
440 // implementation of wxTempFile
441 // ============================================================================
443 // ----------------------------------------------------------------------------
445 // ----------------------------------------------------------------------------
446 wxTempFile::wxTempFile(const wxString
& strName
)
451 bool wxTempFile::Open(const wxString
& strName
)
455 // we want to create the file in the same directory as strName because
456 // otherwise rename() in Commit() might not work (if the files are on
457 // different partitions for example). Unfortunately, the only standard
458 // (POSIX) temp file creation function tmpnam() can't do it.
459 #if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ )
460 static const char *szMktempSuffix
= "XXXXXX";
461 m_strTemp
<< strName
<< szMktempSuffix
;
462 mktemp((char *)m_strTemp
.c_str()); // will do because length doesn't change
465 wxSplitPath(strName
, &strPath
, NULL
, NULL
);
466 if ( strPath
.IsEmpty() )
467 strPath
= '.'; // GetTempFileName will fail if we give it empty string
469 if ( !GetTempFileName(strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
471 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
472 if ( !GetTempFileName((BYTE
) (const char*) strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
474 wxLogLastError("GetTempFileName");
475 m_strTemp
.UngetWriteBuf();
476 #endif // Windows/Unix
478 return m_file
.Open(m_strTemp
, wxFile::write
);
481 // ----------------------------------------------------------------------------
483 // ----------------------------------------------------------------------------
485 wxTempFile::~wxTempFile()
491 bool wxTempFile::Commit()
495 if ( wxFile::Exists(m_strName
) && remove(m_strName
) != 0 ) {
496 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
500 if ( rename(m_strTemp
, m_strName
) != 0 ) {
501 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
508 void wxTempFile::Discard()
511 if ( remove(m_strTemp
) != 0 )
512 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());