]>
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__)
34 #define WIN32_LEAN_AND_MEAN
56 #include <windows.h> // for GetTempFileName
57 #elif (defined(__UNIX__) || defined(__GNUWIN32__))
62 #elif (defined(__WXSTUBS__))
63 // Have to ifdef this for different environments
65 #elif (defined(__WXMAC__))
66 int access( const char *path
, int mode
) { return 0 ; }
67 char* mktemp( char * path
) { return path
;}
73 #error "Please specify the header with file functions declarations."
76 #include <stdio.h> // SEEK_xxx constants
77 #include <fcntl.h> // O_RDONLY &c
80 #include <sys/types.h> // needed for stat
81 #include <sys/stat.h> // stat
84 // Microsoft compiler loves underscores, feed them to it
96 #define access _access
104 #define O_RDONLY _O_RDONLY
105 #define O_WRONLY _O_WRONLY
106 #define O_RDWR _O_RDWR
107 #define O_EXCL _O_EXCL
108 #define O_CREAT _O_CREAT
109 #define O_BINARY _O_BINARY
111 #define S_IFDIR _S_IFDIR
112 #define S_IFREG _S_IFREG
119 #define tell(fd) lseek(fd, 0, SEEK_CUR)
127 // there is no distinction between text and binary files under Unix
138 #include <wx/string.h>
147 // ============================================================================
148 // implementation of wxFile
149 // ============================================================================
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
154 bool wxFile::Exists(const char *name
)
162 return !access(name
, 0) && !stat((char*) name
, &st
) && (st
.st_mode
& S_IFREG
);
165 bool wxFile::Access(const char *name
, OpenMode mode
)
179 wxFAIL_MSG("bad wxFile::Access mode parameter.");
182 return access(name
, how
) == 0;
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
190 wxFile::wxFile(const char *szFileName
, OpenMode mode
)
195 Open(szFileName
, mode
);
204 // create the file, fail if it already exists and bOverwrite
205 bool wxFile::Create(const char *szFileName
, bool bOverwrite
, int accessMode
)
207 // if bOverwrite we create a new file or truncate the existing one,
208 // otherwise we only create the new file and fail if it already exists
210 int fd
= open(szFileName
, O_WRONLY
| O_CREAT
|
211 (bOverwrite
? O_TRUNC
: O_EXCL
));
213 int fd
= open(szFileName
, O_WRONLY
| O_CREAT
|
214 (bOverwrite
? O_TRUNC
: O_EXCL
), accessMode
);
218 wxLogSysError(_("can't create file '%s'"), szFileName
);
228 bool wxFile::Open(const char *szFileName
, OpenMode mode
, int accessMode
)
230 int flags
= O_BINARY
;
238 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
242 flags
|= O_WRONLY
| O_APPEND
;
251 int fd
= open(szFileName
, flags
);
253 int fd
= open(szFileName
, flags
, accessMode
);
257 wxLogSysError(_("can't open file '%s'"), szFileName
);
270 if ( close(m_fd
) == -1 ) {
271 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
287 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
289 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
292 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
294 int iRc
= ::read(m_fd
, pBuf
, nCount
);
297 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
298 return wxInvalidOffset
;
305 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
307 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
310 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
312 int iRc
= ::write(m_fd
, pBuf
, nCount
);
315 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
327 #if (defined(_MSC_VER) && !defined(__MWERKS__)) || wxHAVE_FSYNC
328 if ( fsync(m_fd
) == -1 )
330 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
341 // ----------------------------------------------------------------------------
343 // ----------------------------------------------------------------------------
346 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
348 wxASSERT( IsOpened() );
365 wxFAIL_MSG(_("unknown seek origin"));
368 int iRc
= lseek(m_fd
, ofs
, flag
);
370 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
371 return wxInvalidOffset
;
378 off_t
wxFile::Tell() const
380 wxASSERT( IsOpened() );
382 int iRc
= tell(m_fd
);
384 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
385 return wxInvalidOffset
;
391 // get current file length
392 off_t
wxFile::Length() const
394 wxASSERT( IsOpened() );
396 #if defined( _MSC_VER ) && !defined( __MWERKS__ )
397 int iRc
= _filelength(m_fd
);
399 int iRc
= tell(m_fd
);
401 // @ have to use const_cast :-(
402 int iLen
= ((wxFile
*)this)->SeekEnd();
404 // restore old position
405 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
417 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
418 return wxInvalidOffset
;
424 // is end of file reached?
425 bool wxFile::Eof() const
427 wxASSERT( IsOpened() );
431 #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
432 // @@ this doesn't work, of course, on unseekable file descriptors
433 off_t ofsCur
= Tell(),
435 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
438 iRc
= ofsCur
== ofsMax
;
439 #else // Windows and "native" compiler
441 #endif // Windows/Unix
451 wxLogSysError(_("can't determine if the end of file is reached on \
452 descriptor %d"), m_fd
);
456 wxFAIL_MSG(_("invalid eof() return value."));
462 // ============================================================================
463 // implementation of wxTempFile
464 // ============================================================================
466 // ----------------------------------------------------------------------------
468 // ----------------------------------------------------------------------------
469 wxTempFile::wxTempFile(const wxString
& strName
)
474 bool wxTempFile::Open(const wxString
& strName
)
478 // we want to create the file in the same directory as strName because
479 // otherwise rename() in Commit() might not work (if the files are on
480 // different partitions for example). Unfortunately, the only standard
481 // (POSIX) temp file creation function tmpnam() can't do it.
482 #if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ )
483 static const char *szMktempSuffix
= "XXXXXX";
484 m_strTemp
<< strName
<< szMktempSuffix
;
485 mktemp((char *)m_strTemp
.c_str()); // will do because length doesn't change
488 wxSplitPath(strName
, &strPath
, NULL
, NULL
);
489 if ( strPath
.IsEmpty() )
490 strPath
= '.'; // GetTempFileName will fail if we give it empty string
492 if ( !GetTempFileName(strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
494 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
495 if ( !GetTempFileName((BYTE
) (const char*) strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
497 wxLogLastError("GetTempFileName");
498 m_strTemp
.UngetWriteBuf();
499 #endif // Windows/Unix
501 return m_file
.Open(m_strTemp
, wxFile::write
);
504 // ----------------------------------------------------------------------------
506 // ----------------------------------------------------------------------------
508 wxTempFile::~wxTempFile()
514 bool wxTempFile::Commit()
518 if ( wxFile::Exists(m_strName
) && remove(m_strName
) != 0 ) {
519 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
523 if ( rename(m_strTemp
, m_strName
) != 0 ) {
524 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
531 void wxTempFile::Discard()
534 if ( remove(m_strTemp
) != 0 )
535 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());