]>
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
93 #define access _access
101 #define O_RDONLY _O_RDONLY
102 #define O_WRONLY _O_WRONLY
103 #define O_RDWR _O_RDWR
104 #define O_EXCL _O_EXCL
105 #define O_CREAT _O_CREAT
106 #define O_BINARY _O_BINARY
108 #define S_IFDIR _S_IFDIR
109 #define S_IFREG _S_IFREG
111 #define tell(fd) lseek(fd, 0, SEEK_CUR)
114 #if defined(__BORLANDC__) || defined(_MSC_VER)
119 // there is no distinction between text and binary files under Unix
129 #include <wx/string.h>
139 char gwxMacFileName
[ MAX_PATH
] ;
140 char gwxMacFileName2
[ MAX_PATH
] ;
141 char gwxMacFileName3
[ MAX_PATH
] ;
144 // ============================================================================
145 // implementation of wxFile
146 // ============================================================================
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
151 bool wxFile::Exists(const char *name
)
159 return !access(name
, 0) && !stat((char*) name
, &st
) && (st
.st_mode
& S_IFREG
);
162 bool wxFile::Access(const char *name
, OpenMode mode
)
176 wxFAIL_MSG("bad wxFile::Access mode parameter.");
179 return access(name
, how
) == 0;
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
187 wxFile::wxFile(const char *szFileName
, OpenMode mode
)
192 Open(szFileName
, mode
);
201 // create the file, fail if it already exists and bOverwrite
202 bool wxFile::Create(const char *szFileName
, bool bOverwrite
, int accessMode
)
204 // if bOverwrite we create a new file or truncate the existing one,
205 // otherwise we only create the new file and fail if it already exists
207 int fd
= open(szFileName
, O_WRONLY
| O_CREAT
|
208 (bOverwrite
? O_TRUNC
: O_EXCL
));
210 int fd
= open(szFileName
, O_WRONLY
| O_CREAT
|
211 (bOverwrite
? O_TRUNC
: O_EXCL
), accessMode
);
215 wxLogSysError(_("can't create file '%s'"), szFileName
);
225 bool wxFile::Open(const char *szFileName
, OpenMode mode
, int accessMode
)
227 int flags
= O_BINARY
;
235 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
239 flags
|= O_WRONLY
| O_APPEND
;
248 int fd
= open(szFileName
, flags
);
250 int fd
= open(szFileName
, flags
, accessMode
);
254 wxLogSysError(_("can't open file '%s'"), szFileName
);
267 if ( close(m_fd
) == -1 ) {
268 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
284 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
286 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
289 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
291 int iRc
= ::read(m_fd
, pBuf
, nCount
);
294 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
295 return wxInvalidOffset
;
302 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
304 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
307 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
309 int iRc
= ::write(m_fd
, pBuf
, nCount
);
312 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
324 #if defined(__VISUALC__) || wxHAVE_FSYNC
325 if ( fsync(m_fd
) == -1 )
327 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
338 // ----------------------------------------------------------------------------
340 // ----------------------------------------------------------------------------
343 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
345 wxASSERT( IsOpened() );
362 wxFAIL_MSG(_("unknown seek origin"));
365 int iRc
= lseek(m_fd
, ofs
, flag
);
367 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
368 return wxInvalidOffset
;
375 off_t
wxFile::Tell() const
377 wxASSERT( IsOpened() );
379 int iRc
= tell(m_fd
);
381 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
382 return wxInvalidOffset
;
388 // get current file length
389 off_t
wxFile::Length() const
391 wxASSERT( IsOpened() );
394 int iRc
= _filelength(m_fd
);
396 int iRc
= tell(m_fd
);
398 // @ have to use const_cast :-(
399 int iLen
= ((wxFile
*)this)->SeekEnd();
401 // restore old position
402 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
413 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
414 return wxInvalidOffset
;
420 // is end of file reached?
421 bool wxFile::Eof() const
423 wxASSERT( IsOpened() );
427 #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
428 // @@ this doesn't work, of course, on unseekable file descriptors
429 off_t ofsCur
= Tell(),
431 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
434 iRc
= ofsCur
== ofsMax
;
435 #else // Windows and "native" compiler
437 #endif // Windows/Unix
447 wxLogSysError(_("can't determine if the end of file is reached on \
448 descriptor %d"), m_fd
);
452 wxFAIL_MSG(_("invalid eof() return value."));
458 // ============================================================================
459 // implementation of wxTempFile
460 // ============================================================================
462 // ----------------------------------------------------------------------------
464 // ----------------------------------------------------------------------------
465 wxTempFile::wxTempFile(const wxString
& strName
)
470 bool wxTempFile::Open(const wxString
& strName
)
474 // we want to create the file in the same directory as strName because
475 // otherwise rename() in Commit() might not work (if the files are on
476 // different partitions for example). Unfortunately, the only standard
477 // (POSIX) temp file creation function tmpnam() can't do it.
478 #if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ )
479 static const char *szMktempSuffix
= "XXXXXX";
480 m_strTemp
<< strName
<< szMktempSuffix
;
481 mktemp((char *)m_strTemp
.c_str()); // will do because length doesn't change
484 wxSplitPath(strName
, &strPath
, NULL
, NULL
);
485 if ( strPath
.IsEmpty() )
486 strPath
= '.'; // GetTempFileName will fail if we give it empty string
488 if ( !GetTempFileName(strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
490 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
491 if ( !GetTempFileName((BYTE
) (const char*) strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
493 wxLogLastError("GetTempFileName");
494 m_strTemp
.UngetWriteBuf();
495 #endif // Windows/Unix
497 return m_file
.Open(m_strTemp
, wxFile::write
);
500 // ----------------------------------------------------------------------------
502 // ----------------------------------------------------------------------------
504 wxTempFile::~wxTempFile()
510 bool wxTempFile::Commit()
514 if ( wxFile::Exists(m_strName
) && remove(m_strName
) != 0 ) {
515 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
519 if ( rename(m_strTemp
, m_strName
) != 0 ) {
520 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
527 void wxTempFile::Discard()
530 if ( remove(m_strTemp
) != 0 )
531 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());