]>
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__))
57 #error "Please specify the header with file functions declarations."
60 #include <stdio.h> // SEEK_xxx constants
61 #include <fcntl.h> // O_RDONLY &c
62 #include <sys/types.h> // needed for stat
63 #include <sys/stat.h> // stat
65 // Microsoft compiler loves underscores, feed them to it
74 #define access _access
81 #define O_RDONLY _O_RDONLY
82 #define O_WRONLY _O_WRONLY
83 #define O_RDWR _O_RDWR
84 #define O_EXCL _O_EXCL
85 #define O_CREAT _O_CREAT
86 #define O_BINARY _O_BINARY
88 #define S_IFDIR _S_IFDIR
89 #define S_IFREG _S_IFREG
94 #define tell(fd) lseek(fd, 0, SEEK_CUR)
97 // there is no distinction between text and binary files under Unix
103 #include <wx/string.h>
112 // ============================================================================
113 // implementation of wxFile
114 // ============================================================================
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
119 bool wxFile::Exists(const char *name
)
122 return !access(name
, 0) && !stat(name
, &st
) && (st
.st_mode
& S_IFREG
);
125 bool wxFile::Access(const char *name
, OpenMode mode
)
139 wxFAIL_MSG("bad wxFile::Access mode parameter.");
142 return access(name
, how
) == 0;
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
150 wxFile::wxFile(const char *szFileName
, OpenMode mode
)
155 Open(szFileName
, mode
);
164 // create the file, fail if it already exists and bOverwrite
165 bool wxFile::Create(const char *szFileName
, bool bOverwrite
, int access
)
167 // if bOverwrite we create a new file or truncate the existing one,
168 // otherwise we only create the new file and fail if it already exists
169 int fd
= open(szFileName
, O_CREAT
| (bOverwrite
? O_TRUNC
: O_EXCL
), access
);
172 wxLogSysError(_("can't create file '%s'"), szFileName
);
182 bool wxFile::Open(const char *szFileName
, OpenMode mode
, int access
)
184 int flags
= O_BINARY
;
192 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
196 flags
|= O_WRONLY
| O_APPEND
;
204 int fd
= open(szFileName
, flags
, access
);
207 wxLogSysError(_("can't open file '%s'"), szFileName
);
220 if ( close(m_fd
) == -1 ) {
221 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
232 // ----------------------------------------------------------------------------
234 // ----------------------------------------------------------------------------
237 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
239 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
241 int iRc
= ::read(m_fd
, pBuf
, nCount
);
243 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
244 return wxInvalidOffset
;
251 uint
wxFile::Write(const void *pBuf
, uint nCount
)
253 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
255 int iRc
= ::write(m_fd
, pBuf
, nCount
);
257 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
269 // @@@ fsync() is not ANSI (BSDish)
270 // if ( fsync(m_fd) == -1 ) { // TODO
272 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
285 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
287 wxASSERT( IsOpened() );
304 wxFAIL_MSG(_("unknown seek origin"));
307 int iRc
= lseek(m_fd
, ofs
, flag
);
309 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
310 return wxInvalidOffset
;
317 off_t
wxFile::Tell() const
319 wxASSERT( IsOpened() );
321 int iRc
= tell(m_fd
);
323 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
324 return wxInvalidOffset
;
330 // get current file length
331 off_t
wxFile::Length() const
333 wxASSERT( IsOpened() );
336 int iRc
= _filelength(m_fd
);
338 int iRc
= tell(m_fd
);
340 // @ have to use const_cast :-(
341 int iLen
= ((wxFile
*)this)->SeekEnd();
343 // restore old position
344 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
356 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
357 return wxInvalidOffset
;
363 // is end of file reached?
364 bool wxFile::Eof() const
366 wxASSERT( IsOpened() );
370 #if defined(__UNIX__) || defined(__GNUWIN32__)
371 // @@ this doesn't work, of course, on unseekable file descriptors
372 off_t ofsCur
= Tell(),
374 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
377 iRc
= ofsCur
== ofsMax
;
378 #else // Windows and "native" compiler
380 #endif // Windows/Unix
390 wxLogSysError(_("can't determine if the end of file is reached on \
391 descriptor %d"), m_fd
);
395 wxFAIL_MSG(_("invalid eof() return value."));
401 // ============================================================================
402 // implementation of wxTempFile
403 // ============================================================================
405 // ----------------------------------------------------------------------------
407 // ----------------------------------------------------------------------------
408 wxTempFile::wxTempFile(const wxString
& strName
)
413 bool wxTempFile::Open(const wxString
& strName
)
417 // we want to create the file in the same directory as strName because
418 // otherwise rename() in Commit() might not work (if the files are on
419 // different partitions for example). Unfortunately, the only standard
420 // (POSIX) temp file creation function tmpnam() can't do it.
422 static const char *szMktempSuffix
= "XXXXXX";
423 m_strTemp
<< strName
<< szMktempSuffix
;
424 mktemp((char *)m_strTemp
.c_str()); // will do because length doesn't change
427 wxSplitPath(strName
, &strPath
, NULL
, NULL
);
428 if ( strPath
.IsEmpty() )
429 strPath
= '.'; // GetTempFileName will fail if we give it empty string
431 if ( !GetTempFileName(strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
433 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
434 if ( !GetTempFileName((BYTE
) (const char*) strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
436 wxLogLastError("GetTempFileName");
437 m_strTemp
.UngetWriteBuf();
438 #endif // Windows/Unix
440 return m_file
.Open(m_strTemp
, wxFile::write
);
443 // ----------------------------------------------------------------------------
445 // ----------------------------------------------------------------------------
447 wxTempFile::~wxTempFile()
453 bool wxTempFile::Commit()
457 if ( wxFile::Exists(m_strName
) && remove(m_strName
) != 0 ) {
458 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
462 if ( rename(m_strTemp
, m_strName
) != 0 ) {
463 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
470 void wxTempFile::Discard()
473 if ( remove(m_strTemp
) != 0 )
474 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());