]>
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
65 #include <sys/types.h> // needed for stat
66 #include <sys/stat.h> // stat
68 // Microsoft compiler loves underscores, feed them to it
77 #define access _access
84 #define O_RDONLY _O_RDONLY
85 #define O_WRONLY _O_WRONLY
86 #define O_RDWR _O_RDWR
87 #define O_EXCL _O_EXCL
88 #define O_CREAT _O_CREAT
89 #define O_BINARY _O_BINARY
91 #define S_IFDIR _S_IFDIR
92 #define S_IFREG _S_IFREG
97 #define tell(fd) lseek(fd, 0, SEEK_CUR)
105 // there is no distinction between text and binary files under Unix
111 #include <wx/string.h>
120 // ============================================================================
121 // implementation of wxFile
122 // ============================================================================
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
127 bool wxFile::Exists(const char *name
)
130 return !access(name
, 0) && !stat(name
, &st
) && (st
.st_mode
& S_IFREG
);
133 bool wxFile::Access(const char *name
, OpenMode mode
)
147 wxFAIL_MSG("bad wxFile::Access mode parameter.");
150 return access(name
, how
) == 0;
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
158 wxFile::wxFile(const char *szFileName
, OpenMode mode
)
163 Open(szFileName
, mode
);
172 // create the file, fail if it already exists and bOverwrite
173 bool wxFile::Create(const char *szFileName
, bool bOverwrite
, int access
)
175 // if bOverwrite we create a new file or truncate the existing one,
176 // otherwise we only create the new file and fail if it already exists
177 int fd
= open(szFileName
, O_WRONLY
| O_CREAT
|
178 (bOverwrite
? O_TRUNC
: O_EXCL
), access
);
181 wxLogSysError(_("can't create file '%s'"), szFileName
);
191 bool wxFile::Open(const char *szFileName
, OpenMode mode
, int access
)
193 int flags
= O_BINARY
;
201 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
205 flags
|= O_WRONLY
| O_APPEND
;
213 int fd
= open(szFileName
, flags
, access
);
216 wxLogSysError(_("can't open file '%s'"), szFileName
);
229 if ( close(m_fd
) == -1 ) {
230 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
241 // ----------------------------------------------------------------------------
243 // ----------------------------------------------------------------------------
246 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
248 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
250 int iRc
= ::read(m_fd
, pBuf
, nCount
);
252 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
253 return wxInvalidOffset
;
260 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
262 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
264 int iRc
= ::write(m_fd
, pBuf
, nCount
);
266 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
278 // @@@ fsync() is not ANSI (BSDish)
279 // if ( fsync(m_fd) == -1 ) { // TODO
281 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
289 // ----------------------------------------------------------------------------
291 // ----------------------------------------------------------------------------
294 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
296 wxASSERT( IsOpened() );
313 wxFAIL_MSG(_("unknown seek origin"));
316 int iRc
= lseek(m_fd
, ofs
, flag
);
318 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
319 return wxInvalidOffset
;
326 off_t
wxFile::Tell() const
328 wxASSERT( IsOpened() );
330 int iRc
= tell(m_fd
);
332 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
333 return wxInvalidOffset
;
339 // get current file length
340 off_t
wxFile::Length() const
342 wxASSERT( IsOpened() );
345 int iRc
= _filelength(m_fd
);
347 int iRc
= tell(m_fd
);
349 // @ have to use const_cast :-(
350 int iLen
= ((wxFile
*)this)->SeekEnd();
352 // restore old position
353 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
365 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
366 return wxInvalidOffset
;
372 // is end of file reached?
373 bool wxFile::Eof() const
375 wxASSERT( IsOpened() );
379 #if defined(__UNIX__) || defined(__GNUWIN32__)
380 // @@ this doesn't work, of course, on unseekable file descriptors
381 off_t ofsCur
= Tell(),
383 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
386 iRc
= ofsCur
== ofsMax
;
387 #else // Windows and "native" compiler
389 #endif // Windows/Unix
399 wxLogSysError(_("can't determine if the end of file is reached on \
400 descriptor %d"), m_fd
);
404 wxFAIL_MSG(_("invalid eof() return value."));
410 // ============================================================================
411 // implementation of wxTempFile
412 // ============================================================================
414 // ----------------------------------------------------------------------------
416 // ----------------------------------------------------------------------------
417 wxTempFile::wxTempFile(const wxString
& strName
)
422 bool wxTempFile::Open(const wxString
& strName
)
426 // we want to create the file in the same directory as strName because
427 // otherwise rename() in Commit() might not work (if the files are on
428 // different partitions for example). Unfortunately, the only standard
429 // (POSIX) temp file creation function tmpnam() can't do it.
430 #if defined(__UNIX__) || defined(__WXSTUBS__)
431 static const char *szMktempSuffix
= "XXXXXX";
432 m_strTemp
<< strName
<< szMktempSuffix
;
433 mktemp((char *)m_strTemp
.c_str()); // will do because length doesn't change
436 wxSplitPath(strName
, &strPath
, NULL
, NULL
);
437 if ( strPath
.IsEmpty() )
438 strPath
= '.'; // GetTempFileName will fail if we give it empty string
440 if ( !GetTempFileName(strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
442 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
443 if ( !GetTempFileName((BYTE
) (const char*) strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
445 wxLogLastError("GetTempFileName");
446 m_strTemp
.UngetWriteBuf();
447 #endif // Windows/Unix
449 return m_file
.Open(m_strTemp
, wxFile::write
);
452 // ----------------------------------------------------------------------------
454 // ----------------------------------------------------------------------------
456 wxTempFile::~wxTempFile()
462 bool wxTempFile::Commit()
466 if ( wxFile::Exists(m_strName
) && remove(m_strName
) != 0 ) {
467 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
471 if ( rename(m_strTemp
, m_strName
) != 0 ) {
472 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
479 void wxTempFile::Discard()
482 if ( remove(m_strTemp
) != 0 )
483 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());