]> git.saurik.com Git - wxWidgets.git/blame - src/common/file.cpp
don't use wxApp::GetStdIcon
[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
fac3b423 31#if defined(__WXMSW__) && !defined(__GNUWIN32__) && !defined(__WXWINE__) && !defined(__WXMICROWIN__)
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
c801d85f 57#elif (defined(__UNIX__) || defined(__GNUWIN32__))
3f4a0c5b
VZ
58 #include <unistd.h>
59 #ifdef __GNUWIN32__
60 #include <windows.h>
61 #endif
d3b4d710
VS
62#elif defined(__DOS__)
63 #if defined(__WATCOMC__)
64 #include <io.h>
65 #elif defined(__DJGPP__)
66 #include <io.h>
67 #include <unistd.h>
68 #include <stdio.h>
69 #else
70 #error "Please specify the header with file functions declarations."
71 #endif
c2ff79b1
DW
72#elif (defined(__WXPM__))
73 #include <io.h>
c2ff79b1
DW
74 #define W_OK 2
75 #define R_OK 4
34138703 76#elif (defined(__WXSTUBS__))
3f4a0c5b
VZ
77 // Have to ifdef this for different environments
78 #include <io.h>
17dff81c 79#elif (defined(__WXMAC__))
5b781a67 80#if __MSL__ < 0x6000
3f4a0c5b 81 int access( const char *path, int mode ) { return 0 ; }
5b781a67
SC
82#else
83 int _access( const char *path, int mode ) { return 0 ; }
84#endif
3f4a0c5b 85 char* mktemp( char * path ) { return path ;}
5b781a67 86 #include <stat.h>
3f4a0c5b
VZ
87 #define W_OK 2
88 #define R_OK 4
5b781a67 89 #include <unistd.h>
c801d85f 90#else
3f4a0c5b 91 #error "Please specify the header with file functions declarations."
c801d85f
KB
92#endif //Win/UNIX
93
94#include <stdio.h> // SEEK_xxx constants
95#include <fcntl.h> // O_RDONLY &c
a3ef5bf5 96
469e1e5c 97#ifndef __MWERKS__
3f4a0c5b
VZ
98 #include <sys/types.h> // needed for stat
99 #include <sys/stat.h> // stat
de85a884
VZ
100#elif ( defined(__MWERKS__) && defined(__WXMSW__) )
101 #include <sys/types.h> // needed for stat
102 #include <sys/stat.h> // stat
469e1e5c 103#endif
c801d85f 104
3f4a0c5b 105#if defined(__BORLANDC__) || defined(_MSC_VER)
49d5d881
VZ
106 #define W_OK 2
107 #define R_OK 4
34138703
JS
108#endif
109
b12915c1
VZ
110// there is no distinction between text and binary files under Unix, so define
111// O_BINARY as 0 if the system headers don't do it already
112#if defined(__UNIX__) && !defined(O_BINARY)
49d5d881 113 #define O_BINARY (0)
246037e2 114#endif //__UNIX__
c801d85f 115
a3ef5bf5 116#ifdef __SALFORDC__
3f4a0c5b 117 #include <unix.h>
a3ef5bf5
JS
118#endif
119
81d66cf3 120#ifndef MAX_PATH
3f4a0c5b 121 #define MAX_PATH 512
81d66cf3 122#endif
c801d85f 123
49d5d881
VZ
124// some broken compilers don't have 3rd argument in open() and creat()
125#ifdef __SALFORDC__
126 #define ACCESS(access)
127 #define stat _stat
128#else // normal compiler
129 #define ACCESS(access) , (access)
130#endif // Salford C
131
f6bcfd97 132// wxWindows
ade35f11
VZ
133#ifndef WX_PRECOMP
134 #include "wx/string.h"
135 #include "wx/intl.h"
ade35f11
VZ
136 #include "wx/log.h"
137#endif // !WX_PRECOMP
138
139#include "wx/filename.h"
44d568b6 140#include "wx/file.h"
f6bcfd97 141
28f5082b
VS
142#ifdef __WXMSW__
143 #include "wx/msw/mslu.h"
144#endif
145
c801d85f
KB
146// ============================================================================
147// implementation of wxFile
148// ============================================================================
149
150// ----------------------------------------------------------------------------
151// static functions
152// ----------------------------------------------------------------------------
50920146 153bool wxFile::Exists(const wxChar *name)
246037e2 154{
f6bcfd97 155 wxStructStat st;
50920146 156#if wxUSE_UNICODE && wxMBFILES
dcf924a3 157 wxCharBuffer fname = wxConvFile.cWC2MB(name);
a3ef5bf5 158
f6bcfd97
BP
159 return !wxAccess(fname, 0) &&
160 !wxStat(wxMBSTRINGCAST fname, &st) &&
50920146 161 (st.st_mode & S_IFREG);
bedaf53e 162
50920146 163#else
f6bcfd97
BP
164 return !wxAccess(name, 0) &&
165 !wxStat(name, &st) &&
49d5d881 166 (st.st_mode & S_IFREG);
50920146 167#endif
d1427b70
VZ
168}
169
50920146 170bool wxFile::Access(const wxChar *name, OpenMode mode)
d1427b70 171{
49d5d881 172 int how = 0;
d1427b70 173
49d5d881
VZ
174 switch ( mode ) {
175 case read:
176 how = R_OK;
177 break;
d1427b70 178
49d5d881
VZ
179 case write:
180 how = W_OK;
181 break;
d1427b70 182
49d5d881 183 default:
223d09f6 184 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
49d5d881 185 }
d1427b70 186
f6bcfd97 187 return wxAccess(wxFNCONV(name), how) == 0;
c801d85f
KB
188}
189
190// ----------------------------------------------------------------------------
191// opening/closing
192// ----------------------------------------------------------------------------
193
194// ctors
50920146 195wxFile::wxFile(const wxChar *szFileName, OpenMode mode)
c801d85f 196{
49d5d881
VZ
197 m_fd = fd_invalid;
198 m_error = FALSE;
c801d85f 199
49d5d881 200 Open(szFileName, mode);
c801d85f
KB
201}
202
c801d85f 203// create the file, fail if it already exists and bOverwrite
50920146 204bool wxFile::Create(const wxChar *szFileName, bool bOverwrite, int accessMode)
c801d85f 205{
49d5d881
VZ
206 // if bOverwrite we create a new file or truncate the existing one,
207 // otherwise we only create the new file and fail if it already exists
5fde6fcc 208#if defined(__WXMAC__) && !defined(__UNIX__)
5b781a67
SC
209 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
210 // int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
bedaf53e 211 int fd = creat( szFileName , accessMode);
7c74e7fe 212#else
f6bcfd97
BP
213 int fd = wxOpen(wxFNCONV(szFileName),
214 O_BINARY | O_WRONLY | O_CREAT |
215 (bOverwrite ? O_TRUNC : O_EXCL)
216 ACCESS(accessMode));
7c74e7fe 217#endif
49d5d881
VZ
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
f6bcfd97
BP
238 case write_append:
239 if ( wxFile::Exists(szFileName) )
240 {
241 flags |= O_WRONLY | O_APPEND;
242 break;
243 }
244 //else: fall through as write_append is the same as write if the
245 // file doesn't exist
246
49d5d881
VZ
247 case write:
248 flags |= O_WRONLY | O_CREAT | O_TRUNC;
249 break;
61b02744 250
68164137
RL
251 case write_excl:
252 flags |= O_WRONLY | O_CREAT | O_EXCL;
253 break;
254
49d5d881
VZ
255 case read_write:
256 flags |= O_RDWR;
257 break;
258 }
c801d85f 259
f6bcfd97 260 int fd = wxOpen(wxFNCONV(szFileName), flags ACCESS(accessMode));
49d5d881
VZ
261 if ( fd == -1 ) {
262 wxLogSysError(_("can't open file '%s'"), szFileName);
263 return FALSE;
264 }
265 else {
266 Attach(fd);
267 return TRUE;
268 }
c801d85f
KB
269}
270
271// close
61b02744 272bool wxFile::Close()
c801d85f 273{
49d5d881
VZ
274 if ( IsOpened() ) {
275 if ( close(m_fd) == -1 ) {
276 wxLogSysError(_("can't close file descriptor %d"), m_fd);
277 m_fd = fd_invalid;
278 return FALSE;
279 }
280 else
281 m_fd = fd_invalid;
61b02744 282 }
61b02744 283
49d5d881 284 return TRUE;
c801d85f
KB
285}
286
287// ----------------------------------------------------------------------------
288// read/write
289// ----------------------------------------------------------------------------
290
291// read
292off_t wxFile::Read(void *pBuf, off_t nCount)
293{
49d5d881 294 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 295
469e1e5c 296#ifdef __MWERKS__
49d5d881 297 int iRc = ::read(m_fd, (char*) pBuf, nCount);
469e1e5c 298#else
49d5d881 299 int iRc = ::read(m_fd, pBuf, nCount);
469e1e5c 300#endif
49d5d881
VZ
301 if ( iRc == -1 ) {
302 wxLogSysError(_("can't read from file descriptor %d"), m_fd);
303 return wxInvalidOffset;
304 }
305 else
306 return (size_t)iRc;
c801d85f
KB
307}
308
309// write
c86f1403 310size_t wxFile::Write(const void *pBuf, size_t nCount)
c801d85f 311{
49d5d881 312 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 313
469e1e5c 314#ifdef __MWERKS__
5b781a67
SC
315#if __MSL__ >= 0x6000
316 int iRc = ::write(m_fd, (void*) pBuf, nCount);
317#else
49d5d881 318 int iRc = ::write(m_fd, (const char*) pBuf, nCount);
5b781a67 319#endif
469e1e5c 320#else
49d5d881 321 int iRc = ::write(m_fd, pBuf, nCount);
469e1e5c 322#endif
49d5d881
VZ
323 if ( iRc == -1 ) {
324 wxLogSysError(_("can't write to file descriptor %d"), m_fd);
325 m_error = TRUE;
326 return 0;
327 }
328 else
329 return iRc;
c801d85f
KB
330}
331
332// flush
333bool wxFile::Flush()
334{
49d5d881
VZ
335 if ( IsOpened() ) {
336#if defined(__VISUALC__) || wxHAVE_FSYNC
f6bcfd97 337 if ( wxFsync(m_fd) == -1 )
09914df7
VZ
338 {
339 wxLogSysError(_("can't flush file descriptor %d"), m_fd);
340 return FALSE;
341 }
49d5d881 342#else // no fsync
09914df7 343 // just do nothing
49d5d881
VZ
344#endif // fsync
345 }
c801d85f 346
49d5d881 347 return TRUE;
c801d85f
KB
348}
349
350// ----------------------------------------------------------------------------
351// seek
352// ----------------------------------------------------------------------------
353
354// seek
79c3e0e1 355off_t wxFile::Seek(off_t ofs, wxSeekMode mode)
c801d85f 356{
49d5d881
VZ
357 wxASSERT( IsOpened() );
358
a1b82138 359 int origin;
49d5d881 360 switch ( mode ) {
a1b82138
VZ
361 default:
362 wxFAIL_MSG(_("unknown seek origin"));
363
49d5d881 364 case wxFromStart:
a1b82138 365 origin = SEEK_SET;
49d5d881
VZ
366 break;
367
368 case wxFromCurrent:
a1b82138 369 origin = SEEK_CUR;
49d5d881
VZ
370 break;
371
372 case wxFromEnd:
a1b82138 373 origin = SEEK_END;
49d5d881 374 break;
49d5d881
VZ
375 }
376
a1b82138 377 int iRc = lseek(m_fd, ofs, origin);
49d5d881
VZ
378 if ( iRc == -1 ) {
379 wxLogSysError(_("can't seek on file descriptor %d"), m_fd);
380 return wxInvalidOffset;
381 }
382 else
383 return (off_t)iRc;
c801d85f
KB
384}
385
386// get current off_t
387off_t wxFile::Tell() const
388{
49d5d881
VZ
389 wxASSERT( IsOpened() );
390
f6bcfd97 391 int iRc = wxTell(m_fd);
49d5d881
VZ
392 if ( iRc == -1 ) {
393 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd);
394 return wxInvalidOffset;
395 }
396 else
397 return (off_t)iRc;
c801d85f
KB
398}
399
400// get current file length
401off_t wxFile::Length() const
402{
49d5d881 403 wxASSERT( IsOpened() );
c801d85f 404
49d5d881 405#ifdef __VISUALC__
c801d85f 406 int iRc = _filelength(m_fd);
49d5d881 407#else // !VC++
f6bcfd97 408 int iRc = wxTell(m_fd);
c801d85f 409 if ( iRc != -1 ) {
49d5d881
VZ
410 // @ have to use const_cast :-(
411 int iLen = ((wxFile *)this)->SeekEnd();
412 if ( iLen != -1 ) {
413 // restore old position
414 if ( ((wxFile *)this)->Seek(iRc) == -1 ) {
415 // error
416 iLen = -1;
417 }
c801d85f 418 }
c801d85f 419
49d5d881
VZ
420 iRc = iLen;
421 }
422#endif // VC++
423
424 if ( iRc == -1 ) {
425 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd);
426 return wxInvalidOffset;
c801d85f 427 }
49d5d881
VZ
428 else
429 return (off_t)iRc;
c801d85f
KB
430}
431
432// is end of file reached?
433bool wxFile::Eof() const
434{
49d5d881 435 wxASSERT( IsOpened() );
c801d85f 436
49d5d881 437 int iRc;
61b02744 438
d3b4d710 439#if defined(__DOS__) || defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
61b02744
VZ
440 // @@ this doesn't work, of course, on unseekable file descriptors
441 off_t ofsCur = Tell(),
49d5d881 442 ofsMax = Length();
1678ad78 443 if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset )
49d5d881 444 iRc = -1;
61b02744 445 else
49d5d881
VZ
446 iRc = ofsCur == ofsMax;
447#else // Windows and "native" compiler
61b02744 448 iRc = eof(m_fd);
49d5d881 449#endif // Windows/Unix
c801d85f 450
49d5d881
VZ
451 switch ( iRc ) {
452 case 1:
453 break;
c801d85f 454
49d5d881
VZ
455 case 0:
456 return FALSE;
c801d85f 457
49d5d881 458 case -1:
8e3f1261 459 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd);
49d5d881 460 break;
c801d85f 461
49d5d881
VZ
462 default:
463 wxFAIL_MSG(_("invalid eof() return value."));
464 }
c801d85f 465
49d5d881 466 return TRUE;
c801d85f
KB
467}
468
469// ============================================================================
470// implementation of wxTempFile
471// ============================================================================
472
473// ----------------------------------------------------------------------------
474// construction
475// ----------------------------------------------------------------------------
44b62d54 476
c801d85f
KB
477wxTempFile::wxTempFile(const wxString& strName)
478{
49d5d881 479 Open(strName);
c801d85f
KB
480}
481
482bool wxTempFile::Open(const wxString& strName)
483{
44b62d54
VZ
484 // we must have an absolute filename because otherwise CreateTempFileName()
485 // would create the temp file in $TMP (i.e. the system standard location
486 // for the temp files) which might be on another volume/drive/mount and
487 // wxRename()ing it later to m_strName from Commit() would then fail
488 //
489 // with the absolute filename, the temp file is created in the same
490 // directory as this one which ensures that wxRename() may work later
491 wxFileName fn(strName);
492 if ( !fn.IsAbsolute() )
493 {
494 fn.Normalize(wxPATH_NORM_ABSOLUTE);
495 }
496
497 m_strName = fn.GetFullPath();
246037e2 498
44b62d54 499 m_strTemp = wxFileName::CreateTempFileName(m_strName, &m_file);
ade35f11
VZ
500
501 if ( m_strTemp.empty() )
502 {
503 // CreateTempFileName() failed
504 return FALSE;
505 }
49d5d881 506
49d5d881 507#ifdef __UNIX__
ade35f11
VZ
508 // the temp file should have the same permissions as the original one
509 mode_t mode;
fe99e285 510
f6bcfd97 511 wxStructStat st;
44b62d54 512 if ( stat(m_strName.fn_str(), &st) == 0 )
49d5d881 513 {
ade35f11 514 mode = st.st_mode;
49d5d881
VZ
515 }
516 else
517 {
ade35f11 518 // file probably didn't exist, just give it the default mode _using_
68164137 519 // user's umask (new files creation should respect umask)
ade35f11
VZ
520 mode_t mask = umask(0777);
521 mode = 0666 & ~mask;
522 umask(mask);
49d5d881 523 }
49d5d881 524
ade35f11 525 if ( chmod(m_strTemp.mb_str(), mode) == -1 )
fe99e285 526 {
ade35f11 527 wxLogSysError(_("Failed to set temporary file permissions"));
fe99e285 528 }
49d5d881 529#endif // Unix
246037e2 530
ade35f11 531 return TRUE;
c801d85f
KB
532}
533
534// ----------------------------------------------------------------------------
535// destruction
536// ----------------------------------------------------------------------------
537
538wxTempFile::~wxTempFile()
539{
49d5d881
VZ
540 if ( IsOpened() )
541 Discard();
c801d85f
KB
542}
543
544bool wxTempFile::Commit()
545{
49d5d881 546 m_file.Close();
c801d85f 547
f6bcfd97 548 if ( wxFile::Exists(m_strName) && wxRemove(m_strName) != 0 ) {
49d5d881
VZ
549 wxLogSysError(_("can't remove file '%s'"), m_strName.c_str());
550 return FALSE;
551 }
c801d85f 552
f6bcfd97 553 if ( wxRename(m_strTemp, m_strName) != 0 ) {
49d5d881
VZ
554 wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str());
555 return FALSE;
556 }
c801d85f 557
49d5d881 558 return TRUE;
c801d85f
KB
559}
560
561void wxTempFile::Discard()
562{
49d5d881 563 m_file.Close();
f6bcfd97 564 if ( wxRemove(m_strTemp) != 0 )
49d5d881 565 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str());
c801d85f 566}
ce4169a4 567
ade35f11 568#endif // wxUSE_FILE
cc985fac 569