]>
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>
148 char gwxMacFileName
[ MAX_PATH
] ;
149 char gwxMacFileName2
[ MAX_PATH
] ;
150 char gwxMacFileName3
[ MAX_PATH
] ;
153 // ============================================================================
154 // implementation of wxFile
155 // ============================================================================
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
160 bool wxFile::Exists(const char *name
)
168 return !access(name
, 0) && !stat((char*) name
, &st
) && (st
.st_mode
& S_IFREG
);
171 bool wxFile::Access(const char *name
, OpenMode mode
)
185 wxFAIL_MSG("bad wxFile::Access mode parameter.");
188 return access(name
, how
) == 0;
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
196 wxFile::wxFile(const char *szFileName
, OpenMode mode
)
201 Open(szFileName
, mode
);
210 // create the file, fail if it already exists and bOverwrite
211 bool wxFile::Create(const char *szFileName
, bool bOverwrite
, int accessMode
)
213 // if bOverwrite we create a new file or truncate the existing one,
214 // otherwise we only create the new file and fail if it already exists
216 int fd
= open(szFileName
, O_WRONLY
| O_CREAT
|
217 (bOverwrite
? O_TRUNC
: O_EXCL
));
219 int fd
= open(szFileName
, O_WRONLY
| O_CREAT
|
220 (bOverwrite
? O_TRUNC
: O_EXCL
), accessMode
);
224 wxLogSysError(_("can't create file '%s'"), szFileName
);
234 bool wxFile::Open(const char *szFileName
, OpenMode mode
, int accessMode
)
236 int flags
= O_BINARY
;
244 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
248 flags
|= O_WRONLY
| O_APPEND
;
257 int fd
= open(szFileName
, flags
);
259 int fd
= open(szFileName
, flags
, accessMode
);
263 wxLogSysError(_("can't open file '%s'"), szFileName
);
276 if ( close(m_fd
) == -1 ) {
277 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
288 // ----------------------------------------------------------------------------
290 // ----------------------------------------------------------------------------
293 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
295 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
298 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
300 int iRc
= ::read(m_fd
, pBuf
, nCount
);
303 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
304 return wxInvalidOffset
;
311 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
313 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
316 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
318 int iRc
= ::write(m_fd
, pBuf
, nCount
);
321 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
333 #if (defined(_MSC_VER) && !defined(__MWERKS__)) || wxHAVE_FSYNC
334 if ( fsync(m_fd
) == -1 )
336 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
347 // ----------------------------------------------------------------------------
349 // ----------------------------------------------------------------------------
352 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
354 wxASSERT( IsOpened() );
371 wxFAIL_MSG(_("unknown seek origin"));
374 int iRc
= lseek(m_fd
, ofs
, flag
);
376 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
377 return wxInvalidOffset
;
384 off_t
wxFile::Tell() const
386 wxASSERT( IsOpened() );
388 int iRc
= tell(m_fd
);
390 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
391 return wxInvalidOffset
;
397 // get current file length
398 off_t
wxFile::Length() const
400 wxASSERT( IsOpened() );
402 #if defined( _MSC_VER ) && !defined( __MWERKS__ )
403 int iRc
= _filelength(m_fd
);
405 int iRc
= tell(m_fd
);
407 // @ have to use const_cast :-(
408 int iLen
= ((wxFile
*)this)->SeekEnd();
410 // restore old position
411 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
423 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
424 return wxInvalidOffset
;
430 // is end of file reached?
431 bool wxFile::Eof() const
433 wxASSERT( IsOpened() );
437 #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
438 // @@ this doesn't work, of course, on unseekable file descriptors
439 off_t ofsCur
= Tell(),
441 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
444 iRc
= ofsCur
== ofsMax
;
445 #else // Windows and "native" compiler
447 #endif // Windows/Unix
457 wxLogSysError(_("can't determine if the end of file is reached on \
458 descriptor %d"), m_fd
);
462 wxFAIL_MSG(_("invalid eof() return value."));
468 // ============================================================================
469 // implementation of wxTempFile
470 // ============================================================================
472 // ----------------------------------------------------------------------------
474 // ----------------------------------------------------------------------------
475 wxTempFile::wxTempFile(const wxString
& strName
)
480 bool wxTempFile::Open(const wxString
& strName
)
484 // we want to create the file in the same directory as strName because
485 // otherwise rename() in Commit() might not work (if the files are on
486 // different partitions for example). Unfortunately, the only standard
487 // (POSIX) temp file creation function tmpnam() can't do it.
488 #if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ )
489 static const char *szMktempSuffix
= "XXXXXX";
490 m_strTemp
<< strName
<< szMktempSuffix
;
491 mktemp((char *)m_strTemp
.c_str()); // will do because length doesn't change
494 wxSplitPath(strName
, &strPath
, NULL
, NULL
);
495 if ( strPath
.IsEmpty() )
496 strPath
= '.'; // GetTempFileName will fail if we give it empty string
498 if ( !GetTempFileName(strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
500 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
501 if ( !GetTempFileName((BYTE
) (const char*) strPath
, "wx_",0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
503 wxLogLastError("GetTempFileName");
504 m_strTemp
.UngetWriteBuf();
505 #endif // Windows/Unix
507 return m_file
.Open(m_strTemp
, wxFile::write
);
510 // ----------------------------------------------------------------------------
512 // ----------------------------------------------------------------------------
514 wxTempFile::~wxTempFile()
520 bool wxTempFile::Commit()
524 if ( wxFile::Exists(m_strName
) && remove(m_strName
) != 0 ) {
525 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
529 if ( rename(m_strTemp
, m_strName
) != 0 ) {
530 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
537 void wxTempFile::Discard()
540 if ( remove(m_strTemp
) != 0 )
541 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());