]>
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_CREAT
| (bOverwrite
? O_TRUNC
: O_EXCL
), access
);
180 wxLogSysError(_("can't create file '%s'"), szFileName
);
190 bool wxFile::Open(const char *szFileName
, OpenMode mode
, int access
)
192 int flags
= O_BINARY
;
200 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
204 flags
|= O_WRONLY
| O_APPEND
;
212 int fd
= open(szFileName
, flags
, access
);
215 wxLogSysError(_("can't open file '%s'"), szFileName
);
228 if ( close(m_fd
) == -1 ) {
229 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
245 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
247 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
249 int iRc
= ::read(m_fd
, pBuf
, nCount
);
251 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
252 return wxInvalidOffset
;
259 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
261 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
263 int iRc
= ::write(m_fd
, pBuf
, nCount
);
265 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
277 // @@@ fsync() is not ANSI (BSDish)
278 // if ( fsync(m_fd) == -1 ) { // TODO
280 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
288 // ----------------------------------------------------------------------------
290 // ----------------------------------------------------------------------------
293 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
295 wxASSERT( IsOpened() );
312 wxFAIL_MSG(_("unknown seek origin"));
315 int iRc
= lseek(m_fd
, ofs
, flag
);
317 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
318 return wxInvalidOffset
;
325 off_t
wxFile::Tell() const
327 wxASSERT( IsOpened() );
329 int iRc
= tell(m_fd
);
331 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
332 return wxInvalidOffset
;
338 // get current file length
339 off_t
wxFile::Length() const
341 wxASSERT( IsOpened() );
344 int iRc
= _filelength(m_fd
);
346 int iRc
= tell(m_fd
);
348 // @ have to use const_cast :-(
349 int iLen
= ((wxFile
*)this)->SeekEnd();
351 // restore old position
352 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
364 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
365 return wxInvalidOffset
;
371 // is end of file reached?
372 bool wxFile::Eof() const
374 wxASSERT( IsOpened() );
378 #if defined(__UNIX__) || defined(__GNUWIN32__)
379 // @@ this doesn't work, of course, on unseekable file descriptors
380 off_t ofsCur
= Tell(),
382 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
385 iRc
= ofsCur
== ofsMax
;
386 #else // Windows and "native" compiler
388 #endif // Windows/Unix
398 wxLogSysError(_("can't determine if the end of file is reached on \
399 descriptor %d"), m_fd
);
403 wxFAIL_MSG(_("invalid eof() return value."));
409 // ============================================================================
410 // implementation of wxTempFile
411 // ============================================================================
413 // ----------------------------------------------------------------------------
415 // ----------------------------------------------------------------------------
416 wxTempFile::wxTempFile(const wxString
& strName
)
421 bool wxTempFile::Open(const wxString
& strName
)
425 // we want to create the file in the same directory as strName because
426 // otherwise rename() in Commit() might not work (if the files are on
427 // different partitions for example). Unfortunately, the only standard
428 // (POSIX) temp file creation function tmpnam() can't do it.
429 #if defined(__UNIX__) || defined(__WXSTUBS__)
430 static const char *szMktempSuffix
= "XXXXXX";
431 m_strTemp
<< strName
<< szMktempSuffix
;
432 mktemp((char *)m_strTemp
.c_str()); // will do because length doesn't change
435 wxSplitPath(strName
, &strPath
, NULL
, NULL
);
436 if ( strPath
.IsEmpty() )
437 strPath
= '.'; // GetTempFileName will fail if we give it empty string
439 if ( !GetTempFileName(strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
441 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
442 if ( !GetTempFileName((BYTE
) (const char*) strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
444 wxLogLastError("GetTempFileName");
445 m_strTemp
.UngetWriteBuf();
446 #endif // Windows/Unix
448 return m_file
.Open(m_strTemp
, wxFile::write
);
451 // ----------------------------------------------------------------------------
453 // ----------------------------------------------------------------------------
455 wxTempFile::~wxTempFile()
461 bool wxTempFile::Commit()
465 if ( wxFile::Exists(m_strName
) && remove(m_strName
) != 0 ) {
466 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
470 if ( rename(m_strTemp
, m_strName
) != 0 ) {
471 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
478 void wxTempFile::Discard()
481 if ( remove(m_strTemp
) != 0 )
482 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());