]>
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__))
59 #elif (defined(__WXSTUBS__))
60 // Have to ifdef this for different environments
62 #elif (defined(__WXMAC__))
63 int access( const char *path
, int mode
) { return 0 ; }
64 char* mktemp( char * path
) { return path
;}
70 #error "Please specify the header with file functions declarations."
73 #include <stdio.h> // SEEK_xxx constants
74 #include <fcntl.h> // O_RDONLY &c
76 #include <sys/types.h> // needed for stat
77 #include <sys/stat.h> // stat
80 // Microsoft compiler loves underscores, feed them to it
92 #define access _access
100 #define O_RDONLY _O_RDONLY
101 #define O_WRONLY _O_WRONLY
102 #define O_RDWR _O_RDWR
103 #define O_EXCL _O_EXCL
104 #define O_CREAT _O_CREAT
105 #define O_BINARY _O_BINARY
107 #define S_IFDIR _S_IFDIR
108 #define S_IFREG _S_IFREG
115 #define tell(fd) lseek(fd, 0, SEEK_CUR)
123 // there is no distinction between text and binary files under Unix
129 #include <wx/string.h>
138 // ============================================================================
139 // implementation of wxFile
140 // ============================================================================
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
145 bool wxFile::Exists(const char *name
)
148 return !access(name
, 0) && !stat(name
, &st
) && (st
.st_mode
& S_IFREG
);
151 bool wxFile::Access(const char *name
, OpenMode mode
)
165 wxFAIL_MSG("bad wxFile::Access mode parameter.");
168 return access(name
, how
) == 0;
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
176 wxFile::wxFile(const char *szFileName
, OpenMode mode
)
181 Open(szFileName
, mode
);
190 // create the file, fail if it already exists and bOverwrite
191 bool wxFile::Create(const char *szFileName
, bool bOverwrite
, int access
)
193 // if bOverwrite we create a new file or truncate the existing one,
194 // otherwise we only create the new file and fail if it already exists
195 int fd
= open(szFileName
, O_WRONLY
| O_CREAT
|
196 (bOverwrite
? O_TRUNC
: O_EXCL
), access
);
199 wxLogSysError(_("can't create file '%s'"), szFileName
);
209 bool wxFile::Open(const char *szFileName
, OpenMode mode
, int access
)
211 int flags
= O_BINARY
;
219 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
223 flags
|= O_WRONLY
| O_APPEND
;
231 int fd
= open(szFileName
, flags
, access
);
234 wxLogSysError(_("can't open file '%s'"), szFileName
);
247 if ( close(m_fd
) == -1 ) {
248 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
264 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
266 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
269 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
271 int iRc
= ::read(m_fd
, pBuf
, nCount
);
274 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
275 return wxInvalidOffset
;
282 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
284 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
287 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
289 int iRc
= ::write(m_fd
, pBuf
, nCount
);
292 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
304 // @@@ fsync() is not ANSI (BSDish)
305 // if ( fsync(m_fd) == -1 ) { // TODO
307 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
320 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
322 wxASSERT( IsOpened() );
339 wxFAIL_MSG(_("unknown seek origin"));
342 int iRc
= lseek(m_fd
, ofs
, flag
);
344 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
345 return wxInvalidOffset
;
352 off_t
wxFile::Tell() const
354 wxASSERT( IsOpened() );
356 int iRc
= tell(m_fd
);
358 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
359 return wxInvalidOffset
;
365 // get current file length
366 off_t
wxFile::Length() const
368 wxASSERT( IsOpened() );
370 #if defined( _MSC_VER ) && !defined( __MWERKS__ )
371 int iRc
= _filelength(m_fd
);
373 int iRc
= tell(m_fd
);
375 // @ have to use const_cast :-(
376 int iLen
= ((wxFile
*)this)->SeekEnd();
378 // restore old position
379 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
391 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
392 return wxInvalidOffset
;
398 // is end of file reached?
399 bool wxFile::Eof() const
401 wxASSERT( IsOpened() );
405 #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ )
406 // @@ this doesn't work, of course, on unseekable file descriptors
407 off_t ofsCur
= Tell(),
409 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
412 iRc
= ofsCur
== ofsMax
;
413 #else // Windows and "native" compiler
415 #endif // Windows/Unix
425 wxLogSysError(_("can't determine if the end of file is reached on \
426 descriptor %d"), m_fd
);
430 wxFAIL_MSG(_("invalid eof() return value."));
436 // ============================================================================
437 // implementation of wxTempFile
438 // ============================================================================
440 // ----------------------------------------------------------------------------
442 // ----------------------------------------------------------------------------
443 wxTempFile::wxTempFile(const wxString
& strName
)
448 bool wxTempFile::Open(const wxString
& strName
)
452 // we want to create the file in the same directory as strName because
453 // otherwise rename() in Commit() might not work (if the files are on
454 // different partitions for example). Unfortunately, the only standard
455 // (POSIX) temp file creation function tmpnam() can't do it.
456 #if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ )
457 static const char *szMktempSuffix
= "XXXXXX";
458 m_strTemp
<< strName
<< szMktempSuffix
;
459 mktemp((char *)m_strTemp
.c_str()); // will do because length doesn't change
462 wxSplitPath(strName
, &strPath
, NULL
, NULL
);
463 if ( strPath
.IsEmpty() )
464 strPath
= '.'; // GetTempFileName will fail if we give it empty string
466 if ( !GetTempFileName(strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
468 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
469 if ( !GetTempFileName((BYTE
) (const char*) strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
471 wxLogLastError("GetTempFileName");
472 m_strTemp
.UngetWriteBuf();
473 #endif // Windows/Unix
475 return m_file
.Open(m_strTemp
, wxFile::write
);
478 // ----------------------------------------------------------------------------
480 // ----------------------------------------------------------------------------
482 wxTempFile::~wxTempFile()
488 bool wxTempFile::Commit()
492 if ( wxFile::Exists(m_strName
) && remove(m_strName
) != 0 ) {
493 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
497 if ( rename(m_strTemp
, m_strName
) != 0 ) {
498 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
505 void wxTempFile::Discard()
508 if ( remove(m_strTemp
) != 0 )
509 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());