]>
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__))
57 #error "Please specify the header with file functions declarations."
60 #include <stdio.h> // SEEK_xxx constants
61 #include <fcntl.h> // O_RDONLY &c
62 #include <sys/types.h> // needed for stat
63 #include <sys/stat.h> // stat
65 // Microsoft compiler loves underscores, feed them to it
74 #define access _access
81 #define O_RDONLY _O_RDONLY
82 #define O_WRONLY _O_WRONLY
83 #define O_RDWR _O_RDWR
84 #define O_EXCL _O_EXCL
85 #define O_CREAT _O_CREAT
86 #define O_BINARY _O_BINARY
88 #define S_IFDIR _S_IFDIR
89 #define S_IFREG _S_IFREG
91 #define tell(fd) lseek(fd, 0, SEEK_CUR)
94 // there is no distinction between text and binary files under Unix
100 #include <wx/string.h>
109 // ============================================================================
110 // implementation of wxFile
111 // ============================================================================
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
116 bool wxFile::Exists(const char *sz
)
119 return !access(sz
, 0) && !stat(sz
, &st
) && (st
.st_mode
& S_IFREG
);
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
127 wxFile::wxFile(const char *szFileName
, OpenMode mode
)
132 Open(szFileName
, mode
);
141 // create the file, fail if it already exists and bOverwrite
142 bool wxFile::Create(const char *szFileName
, bool bOverwrite
, int access
)
144 // if bOverwrite we create a new file or truncate the existing one,
145 // otherwise we only create the new file and fail if it already exists
146 int fd
= open(szFileName
, O_CREAT
| (bOverwrite
? O_TRUNC
: O_EXCL
), access
);
149 wxLogSysError("can't create file '%s'", szFileName
);
159 bool wxFile::Open(const char *szFileName
, OpenMode mode
, int access
)
161 int flags
= O_BINARY
;
169 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
173 flags
|= O_WRONLY
| O_APPEND
;
181 int fd
= open(szFileName
, flags
, access
);
184 wxLogSysError("can't open file '%s'", szFileName
);
197 if ( close(m_fd
) == -1 ) {
198 wxLogSysError("can't close file descriptor %d", m_fd
);
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
214 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
216 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
218 int iRc
= ::read(m_fd
, pBuf
, nCount
);
220 wxLogSysError("can't read from file descriptor %d", m_fd
);
221 return wxInvalidOffset
;
228 uint
wxFile::Write(const void *pBuf
, uint nCount
)
230 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
232 int iRc
= ::write(m_fd
, pBuf
, nCount
);
234 wxLogSysError("can't write to file descriptor %d", m_fd
);
246 // @@@ fsync() is not ANSI (BSDish)
247 // if ( fsync(m_fd) == -1 ) { // TODO
249 wxLogSysError("can't flush file descriptor %d", m_fd
);
257 // ----------------------------------------------------------------------------
259 // ----------------------------------------------------------------------------
262 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
264 wxASSERT( IsOpened() );
281 wxFAIL_MSG("unknown seek origin");
284 int iRc
= lseek(m_fd
, ofs
, flag
);
286 wxLogSysError("can't seek on file descriptor %d", m_fd
);
287 return wxInvalidOffset
;
294 off_t
wxFile::Tell() const
296 wxASSERT( IsOpened() );
298 int iRc
= tell(m_fd
);
300 wxLogSysError("can't get seek position on file descriptor %d", m_fd
);
301 return wxInvalidOffset
;
307 // get current file length
308 off_t
wxFile::Length() const
310 wxASSERT( IsOpened() );
313 int iRc
= _filelength(m_fd
);
315 int iRc
= tell(m_fd
);
317 // @ have to use const_cast :-(
318 int iLen
= ((wxFile
*)this)->SeekEnd();
320 // restore old position
321 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
333 wxLogSysError("can't find length of file on file descriptor %d", m_fd
);
334 return wxInvalidOffset
;
340 // is end of file reached?
341 bool wxFile::Eof() const
343 wxASSERT( IsOpened() );
347 #if defined(__UNIX__) || defined(__GNUWIN32__)
348 // @@ this doesn't work, of course, on unseekable file descriptors
349 off_t ofsCur
= Tell(),
351 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
354 iRc
= ofsCur
== ofsMax
;
355 #else // Windows and "native" compiler
357 #endif // Windows/Unix
367 wxLogSysError("can't determine if the end of file is reached on "
368 "descriptor %d", m_fd
);
372 wxFAIL_MSG("invalid eof() return value.");
378 // ============================================================================
379 // implementation of wxTempFile
380 // ============================================================================
382 // ----------------------------------------------------------------------------
384 // ----------------------------------------------------------------------------
385 wxTempFile::wxTempFile(const wxString
& strName
)
390 bool wxTempFile::Open(const wxString
& strName
)
394 // we want to create the file in the same directory as strName because
395 // otherwise rename() in Commit() might not work (if the files are on
396 // different partitions for example). Unfortunately, the only standard
397 // (POSIX) temp file creation function tmpnam() can't do it.
399 static const char *szMktempSuffix
= "XXXXXX";
400 m_strTemp
<< strName
<< szMktempSuffix
;
401 mktemp((char *)m_strTemp
.c_str()); // will do because length doesn't change
404 wxSplitPath(strName
, &strPath
, NULL
, NULL
);
405 if ( strPath
.IsEmpty() )
406 strPath
= '.'; // GetTempFileName will fail if we give it empty string
408 if ( !GetTempFileName(strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
410 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
411 if ( !GetTempFileName((BYTE
) (const char*) strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
413 wxLogLastError("GetTempFileName");
414 m_strTemp
.UngetWriteBuf();
415 #endif // Windows/Unix
417 return m_file
.Open(m_strTemp
, wxFile::write
);
420 // ----------------------------------------------------------------------------
422 // ----------------------------------------------------------------------------
424 wxTempFile::~wxTempFile()
430 bool wxTempFile::Commit()
434 if ( wxFile::Exists(m_strName
) && remove(m_strName
) != 0 ) {
435 wxLogSysError("can't remove file '%s'", m_strName
.c_str());
439 if ( rename(m_strTemp
, m_strName
) != 0 ) {
440 wxLogSysError("can't commit changes to file '%s'", m_strName
.c_str());
447 void wxTempFile::Discard()
450 if ( remove(m_strTemp
) != 0 )
451 wxLogSysError("can't remove temporary file '%s'", m_strTemp
.c_str());