]>
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
59 #elif (defined(__WXMAC__))
60 int access( const char *path
, int mode
) { return 0 ; }
61 char* mktemp( char * path
) { return path
;}
67 #error "Please specify the header with file functions declarations."
70 #include <stdio.h> // SEEK_xxx constants
71 #include <fcntl.h> // O_RDONLY &c
73 #include <sys/types.h> // needed for stat
74 #include <sys/stat.h> // stat
77 // Microsoft compiler loves underscores, feed them to it
89 #define access _access
97 #define O_RDONLY _O_RDONLY
98 #define O_WRONLY _O_WRONLY
99 #define O_RDWR _O_RDWR
100 #define O_EXCL _O_EXCL
101 #define O_CREAT _O_CREAT
102 #define O_BINARY _O_BINARY
104 #define S_IFDIR _S_IFDIR
105 #define S_IFREG _S_IFREG
112 #define tell(fd) lseek(fd, 0, SEEK_CUR)
120 // there is no distinction between text and binary files under Unix
126 #include <wx/string.h>
135 // ============================================================================
136 // implementation of wxFile
137 // ============================================================================
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
142 bool wxFile::Exists(const char *name
)
145 return !access(name
, 0) && !stat(name
, &st
) && (st
.st_mode
& S_IFREG
);
148 bool wxFile::Access(const char *name
, OpenMode mode
)
162 wxFAIL_MSG("bad wxFile::Access mode parameter.");
165 return access(name
, how
) == 0;
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
173 wxFile::wxFile(const char *szFileName
, OpenMode mode
)
178 Open(szFileName
, mode
);
187 // create the file, fail if it already exists and bOverwrite
188 bool wxFile::Create(const char *szFileName
, bool bOverwrite
, int access
)
190 // if bOverwrite we create a new file or truncate the existing one,
191 // otherwise we only create the new file and fail if it already exists
192 int fd
= open(szFileName
, O_WRONLY
| O_CREAT
|
193 (bOverwrite
? O_TRUNC
: O_EXCL
), access
);
196 wxLogSysError(_("can't create file '%s'"), szFileName
);
206 bool wxFile::Open(const char *szFileName
, OpenMode mode
, int access
)
208 int flags
= O_BINARY
;
216 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
220 flags
|= O_WRONLY
| O_APPEND
;
228 int fd
= open(szFileName
, flags
, access
);
231 wxLogSysError(_("can't open file '%s'"), szFileName
);
244 if ( close(m_fd
) == -1 ) {
245 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
261 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
263 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
266 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
268 int iRc
= ::read(m_fd
, pBuf
, nCount
);
271 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
272 return wxInvalidOffset
;
279 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
281 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
284 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
286 int iRc
= ::write(m_fd
, pBuf
, nCount
);
289 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
301 // @@@ fsync() is not ANSI (BSDish)
302 // if ( fsync(m_fd) == -1 ) { // TODO
304 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
312 // ----------------------------------------------------------------------------
314 // ----------------------------------------------------------------------------
317 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
319 wxASSERT( IsOpened() );
336 wxFAIL_MSG(_("unknown seek origin"));
339 int iRc
= lseek(m_fd
, ofs
, flag
);
341 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
342 return wxInvalidOffset
;
349 off_t
wxFile::Tell() const
351 wxASSERT( IsOpened() );
353 int iRc
= tell(m_fd
);
355 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
356 return wxInvalidOffset
;
362 // get current file length
363 off_t
wxFile::Length() const
365 wxASSERT( IsOpened() );
367 #if defined( _MSC_VER ) && !defined( __MWERKS__ )
368 int iRc
= _filelength(m_fd
);
370 int iRc
= tell(m_fd
);
372 // @ have to use const_cast :-(
373 int iLen
= ((wxFile
*)this)->SeekEnd();
375 // restore old position
376 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
388 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
389 return wxInvalidOffset
;
395 // is end of file reached?
396 bool wxFile::Eof() const
398 wxASSERT( IsOpened() );
402 #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ )
403 // @@ this doesn't work, of course, on unseekable file descriptors
404 off_t ofsCur
= Tell(),
406 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
409 iRc
= ofsCur
== ofsMax
;
410 #else // Windows and "native" compiler
412 #endif // Windows/Unix
422 wxLogSysError(_("can't determine if the end of file is reached on \
423 descriptor %d"), m_fd
);
427 wxFAIL_MSG(_("invalid eof() return value."));
433 // ============================================================================
434 // implementation of wxTempFile
435 // ============================================================================
437 // ----------------------------------------------------------------------------
439 // ----------------------------------------------------------------------------
440 wxTempFile::wxTempFile(const wxString
& strName
)
445 bool wxTempFile::Open(const wxString
& strName
)
449 // we want to create the file in the same directory as strName because
450 // otherwise rename() in Commit() might not work (if the files are on
451 // different partitions for example). Unfortunately, the only standard
452 // (POSIX) temp file creation function tmpnam() can't do it.
453 #if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ )
454 static const char *szMktempSuffix
= "XXXXXX";
455 m_strTemp
<< strName
<< szMktempSuffix
;
456 mktemp((char *)m_strTemp
.c_str()); // will do because length doesn't change
459 wxSplitPath(strName
, &strPath
, NULL
, NULL
);
460 if ( strPath
.IsEmpty() )
461 strPath
= '.'; // GetTempFileName will fail if we give it empty string
463 if ( !GetTempFileName(strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
465 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
466 if ( !GetTempFileName((BYTE
) (const char*) strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
468 wxLogLastError("GetTempFileName");
469 m_strTemp
.UngetWriteBuf();
470 #endif // Windows/Unix
472 return m_file
.Open(m_strTemp
, wxFile::write
);
475 // ----------------------------------------------------------------------------
477 // ----------------------------------------------------------------------------
479 wxTempFile::~wxTempFile()
485 bool wxTempFile::Commit()
489 if ( wxFile::Exists(m_strName
) && remove(m_strName
) != 0 ) {
490 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
494 if ( rename(m_strTemp
, m_strName
) != 0 ) {
495 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
502 void wxTempFile::Discard()
505 if ( remove(m_strTemp
) != 0 )
506 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());