]> git.saurik.com Git - wxWidgets.git/blame - src/common/file.cpp
More _T() in asserts...
[wxWidgets.git] / src / common / file.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: file.cpp
3// Purpose: wxFile - encapsulates low-level "file descriptor"
4// wxTempFile
5// Author: Vadim Zeitlin
6// Modified by:
7// Created: 29/01/98
8// RCS-ID: $Id$
9// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
246037e2 10// Licence: wxWindows license
c801d85f
KB
11/////////////////////////////////////////////////////////////////////////////
12
13// ----------------------------------------------------------------------------
14// headers
15// ----------------------------------------------------------------------------
16
17#ifdef __GNUG__
3f4a0c5b 18 #pragma implementation "file.h"
c801d85f
KB
19#endif
20
21// For compilers that support precompilation, includes "wx.h".
22#include "wx/wxprec.h"
c801d85f
KB
23
24#ifdef __BORLANDC__
ce4169a4 25 #pragma hdrstop
c801d85f
KB
26#endif
27
ce4169a4
RR
28#if wxUSE_FILE
29
c801d85f 30// standard
5ea105e0 31#if defined(__WXMSW__) && !defined(__GNUWIN32__) && !defined(__WXWINE__)
c801d85f 32 #include <io.h>
30a5be97 33
a3ef5bf5 34#ifndef __SALFORDC__
49d5d881
VZ
35 #define WIN32_LEAN_AND_MEAN
36 #define NOSERVICE
37 #define NOIME
38 #define NOATOM
39 #define NOGDI
40 #define NOGDICAPMASKS
41 #define NOMETAFILE
42 #define NOMINMAX
43 #define NOMSG
44 #define NOOPENFILE
45 #define NORASTEROPS
46 #define NOSCROLL
47 #define NOSOUND
48 #define NOSYSMETRICS
49 #define NOTEXTMETRIC
50 #define NOWH
51 #define NOCOMM
52 #define NOKANJI
53 #define NOCRYPT
54 #define NOMCX
a3ef5bf5
JS
55#endif
56
49d5d881 57 #include <windows.h> // for GetTempFileName
c801d85f 58#elif (defined(__UNIX__) || defined(__GNUWIN32__))
3f4a0c5b
VZ
59 #include <unistd.h>
60 #ifdef __GNUWIN32__
61 #include <windows.h>
62 #endif
34138703 63#elif (defined(__WXSTUBS__))
3f4a0c5b
VZ
64 // Have to ifdef this for different environments
65 #include <io.h>
17dff81c 66#elif (defined(__WXMAC__))
3f4a0c5b
VZ
67 int access( const char *path, int mode ) { return 0 ; }
68 char* mktemp( char * path ) { return path ;}
69 #include <unistd.h>
70 #include <unix.h>
71 #define W_OK 2
72 #define R_OK 4
c801d85f 73#else
3f4a0c5b 74 #error "Please specify the header with file functions declarations."
c801d85f
KB
75#endif //Win/UNIX
76
77#include <stdio.h> // SEEK_xxx constants
78#include <fcntl.h> // O_RDONLY &c
a3ef5bf5 79
469e1e5c 80#ifndef __MWERKS__
3f4a0c5b
VZ
81 #include <sys/types.h> // needed for stat
82 #include <sys/stat.h> // stat
469e1e5c 83#endif
c801d85f
KB
84
85// Microsoft compiler loves underscores, feed them to it
3f4a0c5b 86#ifdef __VISUALC__
49d5d881
VZ
87 // functions
88 #define open _open
89 #define close _close
90 #define read _read
91 #define write _write
92 #define lseek _lseek
93 #define fsync _commit
94 #define access _access
95 #define eof _eof
96
97 // types
98 #define stat _stat
99
100 // constants
101
102 #define O_RDONLY _O_RDONLY
103 #define O_WRONLY _O_WRONLY
104 #define O_RDWR _O_RDWR
105 #define O_EXCL _O_EXCL
106 #define O_CREAT _O_CREAT
107 #define O_BINARY _O_BINARY
108
109 #define S_IFDIR _S_IFDIR
110 #define S_IFREG _S_IFREG
c801d85f 111#else
49d5d881 112 #define tell(fd) lseek(fd, 0, SEEK_CUR)
3f4a0c5b 113#endif // VC++
c801d85f 114
3f4a0c5b 115#if defined(__BORLANDC__) || defined(_MSC_VER)
49d5d881
VZ
116 #define W_OK 2
117 #define R_OK 4
34138703
JS
118#endif
119
c801d85f 120// there is no distinction between text and binary files under Unix
246037e2 121#ifdef __UNIX__
49d5d881 122 #define O_BINARY (0)
246037e2 123#endif //__UNIX__
c801d85f 124
a3ef5bf5 125#ifdef __SALFORDC__
3f4a0c5b 126 #include <unix.h>
a3ef5bf5
JS
127#endif
128
c801d85f
KB
129// wxWindows
130#include <wx/string.h>
131#include <wx/intl.h>
132#include <wx/file.h>
133#include <wx/log.h>
134
81d66cf3 135#ifndef MAX_PATH
3f4a0c5b 136 #define MAX_PATH 512
81d66cf3 137#endif
c801d85f 138
169935ad 139#ifdef __WXMAC__
3f4a0c5b
VZ
140 char gwxMacFileName[ MAX_PATH ] ;
141 char gwxMacFileName2[ MAX_PATH ] ;
142 char gwxMacFileName3[ MAX_PATH ] ;
169935ad
SC
143#endif
144
49d5d881
VZ
145// some broken compilers don't have 3rd argument in open() and creat()
146#ifdef __SALFORDC__
147 #define ACCESS(access)
148 #define stat _stat
149#else // normal compiler
150 #define ACCESS(access) , (access)
151#endif // Salford C
152
c801d85f
KB
153// ============================================================================
154// implementation of wxFile
155// ============================================================================
156
157// ----------------------------------------------------------------------------
158// static functions
159// ----------------------------------------------------------------------------
50920146 160bool wxFile::Exists(const wxChar *name)
246037e2 161{
49d5d881 162 struct stat st;
50920146 163#if wxUSE_UNICODE && wxMBFILES
dcf924a3 164 wxCharBuffer fname = wxConvFile.cWC2MB(name);
a3ef5bf5 165
50920146
OK
166 return !access(fname, 0) &&
167 !stat(MBSTRINGCAST fname, &st) &&
168 (st.st_mode & S_IFREG);
169#else
49d5d881 170 return !access(name, 0) &&
50920146 171 !stat((wxChar*) name, &st) &&
49d5d881 172 (st.st_mode & S_IFREG);
50920146 173#endif
d1427b70
VZ
174}
175
50920146 176bool wxFile::Access(const wxChar *name, OpenMode mode)
d1427b70 177{
49d5d881 178 int how = 0;
d1427b70 179
49d5d881
VZ
180 switch ( mode ) {
181 case read:
182 how = R_OK;
183 break;
d1427b70 184
49d5d881
VZ
185 case write:
186 how = W_OK;
187 break;
d1427b70 188
49d5d881 189 default:
50920146 190 wxFAIL_MSG(_T("bad wxFile::Access mode parameter."));
49d5d881 191 }
d1427b70 192
50920146 193 return access(wxFNCONV(name), how) == 0;
c801d85f
KB
194}
195
196// ----------------------------------------------------------------------------
197// opening/closing
198// ----------------------------------------------------------------------------
199
200// ctors
50920146 201wxFile::wxFile(const wxChar *szFileName, OpenMode mode)
c801d85f 202{
49d5d881
VZ
203 m_fd = fd_invalid;
204 m_error = FALSE;
c801d85f 205
49d5d881 206 Open(szFileName, mode);
c801d85f
KB
207}
208
c801d85f 209// create the file, fail if it already exists and bOverwrite
50920146 210bool wxFile::Create(const wxChar *szFileName, bool bOverwrite, int accessMode)
c801d85f 211{
49d5d881
VZ
212 // if bOverwrite we create a new file or truncate the existing one,
213 // otherwise we only create the new file and fail if it already exists
50920146 214 int fd = open(wxFNCONV(szFileName),
49d5d881
VZ
215 O_WRONLY | O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL)
216 ACCESS(accessMode));
217
218 if ( fd == -1 ) {
219 wxLogSysError(_("can't create file '%s'"), szFileName);
220 return FALSE;
221 }
222 else {
223 Attach(fd);
224 return TRUE;
225 }
c801d85f
KB
226}
227
228// open the file
50920146 229bool wxFile::Open(const wxChar *szFileName, OpenMode mode, int accessMode)
c801d85f 230{
49d5d881 231 int flags = O_BINARY;
c801d85f 232
49d5d881
VZ
233 switch ( mode ) {
234 case read:
235 flags |= O_RDONLY;
236 break;
c801d85f 237
49d5d881
VZ
238 case write:
239 flags |= O_WRONLY | O_CREAT | O_TRUNC;
240 break;
61b02744 241
49d5d881
VZ
242 case write_append:
243 flags |= O_WRONLY | O_APPEND;
244 break;
c801d85f 245
49d5d881
VZ
246 case read_write:
247 flags |= O_RDWR;
248 break;
249 }
c801d85f 250
50920146 251 int fd = open(wxFNCONV(szFileName), flags ACCESS(accessMode));
c801d85f 252
49d5d881
VZ
253 if ( fd == -1 ) {
254 wxLogSysError(_("can't open file '%s'"), szFileName);
255 return FALSE;
256 }
257 else {
258 Attach(fd);
259 return TRUE;
260 }
c801d85f
KB
261}
262
263// close
61b02744 264bool wxFile::Close()
c801d85f 265{
49d5d881
VZ
266 if ( IsOpened() ) {
267 if ( close(m_fd) == -1 ) {
268 wxLogSysError(_("can't close file descriptor %d"), m_fd);
269 m_fd = fd_invalid;
270 return FALSE;
271 }
272 else
273 m_fd = fd_invalid;
61b02744 274 }
61b02744 275
49d5d881 276 return TRUE;
c801d85f
KB
277}
278
279// ----------------------------------------------------------------------------
280// read/write
281// ----------------------------------------------------------------------------
282
283// read
284off_t wxFile::Read(void *pBuf, off_t nCount)
285{
49d5d881 286 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 287
469e1e5c 288#ifdef __MWERKS__
49d5d881 289 int iRc = ::read(m_fd, (char*) pBuf, nCount);
469e1e5c 290#else
49d5d881 291 int iRc = ::read(m_fd, pBuf, nCount);
469e1e5c 292#endif
49d5d881
VZ
293 if ( iRc == -1 ) {
294 wxLogSysError(_("can't read from file descriptor %d"), m_fd);
295 return wxInvalidOffset;
296 }
297 else
298 return (size_t)iRc;
c801d85f
KB
299}
300
301// write
c86f1403 302size_t wxFile::Write(const void *pBuf, size_t nCount)
c801d85f 303{
49d5d881 304 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 305
469e1e5c 306#ifdef __MWERKS__
49d5d881 307 int iRc = ::write(m_fd, (const char*) pBuf, nCount);
469e1e5c 308#else
49d5d881 309 int iRc = ::write(m_fd, pBuf, nCount);
469e1e5c 310#endif
49d5d881
VZ
311 if ( iRc == -1 ) {
312 wxLogSysError(_("can't write to file descriptor %d"), m_fd);
313 m_error = TRUE;
314 return 0;
315 }
316 else
317 return iRc;
c801d85f
KB
318}
319
320// flush
321bool wxFile::Flush()
322{
49d5d881
VZ
323 if ( IsOpened() ) {
324#if defined(__VISUALC__) || wxHAVE_FSYNC
09914df7
VZ
325 if ( fsync(m_fd) == -1 )
326 {
327 wxLogSysError(_("can't flush file descriptor %d"), m_fd);
328 return FALSE;
329 }
49d5d881 330#else // no fsync
09914df7 331 // just do nothing
49d5d881
VZ
332#endif // fsync
333 }
c801d85f 334
49d5d881 335 return TRUE;
c801d85f
KB
336}
337
338// ----------------------------------------------------------------------------
339// seek
340// ----------------------------------------------------------------------------
341
342// seek
79c3e0e1 343off_t wxFile::Seek(off_t ofs, wxSeekMode mode)
c801d85f 344{
49d5d881
VZ
345 wxASSERT( IsOpened() );
346
a1b82138 347 int origin;
49d5d881 348 switch ( mode ) {
a1b82138
VZ
349 default:
350 wxFAIL_MSG(_("unknown seek origin"));
351
49d5d881 352 case wxFromStart:
a1b82138 353 origin = SEEK_SET;
49d5d881
VZ
354 break;
355
356 case wxFromCurrent:
a1b82138 357 origin = SEEK_CUR;
49d5d881
VZ
358 break;
359
360 case wxFromEnd:
a1b82138 361 origin = SEEK_END;
49d5d881 362 break;
49d5d881
VZ
363 }
364
a1b82138 365 int iRc = lseek(m_fd, ofs, origin);
49d5d881
VZ
366 if ( iRc == -1 ) {
367 wxLogSysError(_("can't seek on file descriptor %d"), m_fd);
368 return wxInvalidOffset;
369 }
370 else
371 return (off_t)iRc;
c801d85f
KB
372}
373
374// get current off_t
375off_t wxFile::Tell() const
376{
49d5d881
VZ
377 wxASSERT( IsOpened() );
378
379 int iRc = tell(m_fd);
380 if ( iRc == -1 ) {
381 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd);
382 return wxInvalidOffset;
383 }
384 else
385 return (off_t)iRc;
c801d85f
KB
386}
387
388// get current file length
389off_t wxFile::Length() const
390{
49d5d881 391 wxASSERT( IsOpened() );
c801d85f 392
49d5d881 393#ifdef __VISUALC__
c801d85f 394 int iRc = _filelength(m_fd);
49d5d881 395#else // !VC++
c801d85f
KB
396 int iRc = tell(m_fd);
397 if ( iRc != -1 ) {
49d5d881
VZ
398 // @ have to use const_cast :-(
399 int iLen = ((wxFile *)this)->SeekEnd();
400 if ( iLen != -1 ) {
401 // restore old position
402 if ( ((wxFile *)this)->Seek(iRc) == -1 ) {
403 // error
404 iLen = -1;
405 }
c801d85f 406 }
c801d85f 407
49d5d881
VZ
408 iRc = iLen;
409 }
410#endif // VC++
411
412 if ( iRc == -1 ) {
413 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd);
414 return wxInvalidOffset;
c801d85f 415 }
49d5d881
VZ
416 else
417 return (off_t)iRc;
c801d85f
KB
418}
419
420// is end of file reached?
421bool wxFile::Eof() const
422{
49d5d881 423 wxASSERT( IsOpened() );
c801d85f 424
49d5d881 425 int iRc;
61b02744 426
49d5d881 427#if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
61b02744
VZ
428 // @@ this doesn't work, of course, on unseekable file descriptors
429 off_t ofsCur = Tell(),
49d5d881 430 ofsMax = Length();
1678ad78 431 if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset )
49d5d881 432 iRc = -1;
61b02744 433 else
49d5d881
VZ
434 iRc = ofsCur == ofsMax;
435#else // Windows and "native" compiler
61b02744 436 iRc = eof(m_fd);
49d5d881 437#endif // Windows/Unix
c801d85f 438
49d5d881
VZ
439 switch ( iRc ) {
440 case 1:
441 break;
c801d85f 442
49d5d881
VZ
443 case 0:
444 return FALSE;
c801d85f 445
49d5d881
VZ
446 case -1:
447 wxLogSysError(_("can't determine if the end of file is reached on \
448 descriptor %d"), m_fd);
449 break;
c801d85f 450
49d5d881
VZ
451 default:
452 wxFAIL_MSG(_("invalid eof() return value."));
453 }
c801d85f 454
49d5d881 455 return TRUE;
c801d85f
KB
456}
457
458// ============================================================================
459// implementation of wxTempFile
460// ============================================================================
461
462// ----------------------------------------------------------------------------
463// construction
464// ----------------------------------------------------------------------------
465wxTempFile::wxTempFile(const wxString& strName)
466{
49d5d881 467 Open(strName);
c801d85f
KB
468}
469
470bool wxTempFile::Open(const wxString& strName)
471{
49d5d881 472 m_strName = strName;
246037e2 473
49d5d881
VZ
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__ )
50920146 479 static const wxChar *szMktempSuffix = _T("XXXXXX");
b59650be 480 m_strTemp << strName << szMktempSuffix;
50920146 481 mktemp(MBSTRINGCAST m_strTemp.mb_str()); // will do because length doesn't change
49d5d881 482#else // Windows
30a5be97
VZ
483 wxString strPath;
484 wxSplitPath(strName, &strPath, NULL, NULL);
485 if ( strPath.IsEmpty() )
50920146 486 strPath = _T('.'); // GetTempFileName will fail if we give it empty string
81d66cf3 487#ifdef __WIN32__
50920146 488 if ( !GetTempFileName(strPath, _T("wx_"),0, m_strTemp.GetWriteBuf(MAX_PATH)) )
81d66cf3 489#else
49d5d881 490 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
50920146 491 if ( !GetTempFileName((BYTE) (const wxChar*) strPath, _T("wx_"),0, m_strTemp.GetWriteBuf(MAX_PATH)) )
81d66cf3 492#endif
50920146 493 wxLogLastError(_T("GetTempFileName"));
30a5be97 494 m_strTemp.UngetWriteBuf();
49d5d881
VZ
495#endif // Windows/Unix
496
497 int access = wxS_DEFAULT;
498#ifdef __UNIX__
499 // create the file with the same mode as the original one under Unix
f46f1bc6 500 mode_t umaskOld = 0; // just to suppress compiler warning
fe99e285
VZ
501 bool changedUmask;
502
49d5d881 503 struct stat st;
50920146 504 if ( stat(strName.fn_str(), &st) == 0 )
49d5d881
VZ
505 {
506 // this assumes that only lower bits of st_mode contain the access
507 // rights, but it's true for at least all Unices which have S_IXXXX()
508 // macros, so should not be less portable than using (not POSIX)
509 // S_IFREG &c
510 access = st.st_mode & 0777;
fe99e285
VZ
511
512 // we want to create the file with exactly the same access rights as
513 // the original one, so disable the user's umask for the moment
514 umaskOld = umask(0);
515 changedUmask = TRUE;
49d5d881
VZ
516 }
517 else
518 {
fe99e285
VZ
519 // file probably didn't exist, just create with default mode _using_
520 // user's umask (new files creation should respet umask)
cedda7e6 521 changedUmask = FALSE;
49d5d881 522 }
49d5d881
VZ
523#endif // Unix
524
cedda7e6 525 bool ok = m_file.Open(m_strTemp, wxFile::write, access);
49d5d881
VZ
526
527#ifdef __UNIX__
fe99e285
VZ
528 if ( changedUmask )
529 {
530 // restore umask now that the file is created
531 (void)umask(umaskOld);
532 }
49d5d881 533#endif // Unix
246037e2 534
49d5d881 535 return ok;
c801d85f
KB
536}
537
538// ----------------------------------------------------------------------------
539// destruction
540// ----------------------------------------------------------------------------
541
542wxTempFile::~wxTempFile()
543{
49d5d881
VZ
544 if ( IsOpened() )
545 Discard();
c801d85f
KB
546}
547
548bool wxTempFile::Commit()
549{
49d5d881 550 m_file.Close();
c801d85f 551
50920146 552 if ( wxFile::Exists(m_strName) && remove(m_strName.fn_str()) != 0 ) {
49d5d881
VZ
553 wxLogSysError(_("can't remove file '%s'"), m_strName.c_str());
554 return FALSE;
555 }
c801d85f 556
50920146 557 if ( rename(m_strTemp.fn_str(), m_strName.fn_str()) != 0 ) {
49d5d881
VZ
558 wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str());
559 return FALSE;
560 }
c801d85f 561
49d5d881 562 return TRUE;
c801d85f
KB
563}
564
565void wxTempFile::Discard()
566{
49d5d881 567 m_file.Close();
50920146 568 if ( remove(m_strTemp.fn_str()) != 0 )
49d5d881 569 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str());
c801d85f 570}
ce4169a4 571
cc985fac
PA
572#endif
573