]> git.saurik.com Git - wxWidgets.git/blame - src/common/file.cpp
corrected warnings
[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;
f6bcfd97
BP
156 return !wxAccess(name, 0) &&
157 !wxStat(name, &st) &&
49d5d881 158 (st.st_mode & S_IFREG);
d1427b70
VZ
159}
160
50920146 161bool wxFile::Access(const wxChar *name, OpenMode mode)
d1427b70 162{
49d5d881 163 int how = 0;
d1427b70 164
49d5d881
VZ
165 switch ( mode ) {
166 case read:
167 how = R_OK;
168 break;
d1427b70 169
49d5d881
VZ
170 case write:
171 how = W_OK;
172 break;
d1427b70 173
49d5d881 174 default:
223d09f6 175 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
49d5d881 176 }
d1427b70 177
92980e90 178 return wxAccess( name, how) == 0;
c801d85f
KB
179}
180
181// ----------------------------------------------------------------------------
182// opening/closing
183// ----------------------------------------------------------------------------
184
185// ctors
50920146 186wxFile::wxFile(const wxChar *szFileName, OpenMode mode)
c801d85f 187{
49d5d881
VZ
188 m_fd = fd_invalid;
189 m_error = FALSE;
c801d85f 190
49d5d881 191 Open(szFileName, mode);
c801d85f
KB
192}
193
c801d85f 194// create the file, fail if it already exists and bOverwrite
50920146 195bool wxFile::Create(const wxChar *szFileName, bool bOverwrite, int accessMode)
c801d85f 196{
49d5d881
VZ
197 // if bOverwrite we create a new file or truncate the existing one,
198 // otherwise we only create the new file and fail if it already exists
5fde6fcc 199#if defined(__WXMAC__) && !defined(__UNIX__)
92980e90
RR
200 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
201 // int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
202 int fd = creat( szFileName , accessMode);
7c74e7fe 203#else
92980e90
RR
204 int fd = wxOpen( szFileName,
205 O_BINARY | O_WRONLY | O_CREAT |
206 (bOverwrite ? O_TRUNC : O_EXCL)
207 ACCESS(accessMode) );
7c74e7fe 208#endif
92980e90
RR
209 if ( fd == -1 )
210 {
49d5d881
VZ
211 wxLogSysError(_("can't create file '%s'"), szFileName);
212 return FALSE;
213 }
92980e90
RR
214 else
215 {
49d5d881
VZ
216 Attach(fd);
217 return TRUE;
218 }
c801d85f
KB
219}
220
221// open the file
50920146 222bool wxFile::Open(const wxChar *szFileName, OpenMode mode, int accessMode)
c801d85f 223{
49d5d881 224 int flags = O_BINARY;
c801d85f 225
92980e90
RR
226 switch ( mode )
227 {
49d5d881
VZ
228 case read:
229 flags |= O_RDONLY;
230 break;
c801d85f 231
f6bcfd97
BP
232 case write_append:
233 if ( wxFile::Exists(szFileName) )
234 {
235 flags |= O_WRONLY | O_APPEND;
236 break;
237 }
238 //else: fall through as write_append is the same as write if the
239 // file doesn't exist
240
49d5d881
VZ
241 case write:
242 flags |= O_WRONLY | O_CREAT | O_TRUNC;
243 break;
61b02744 244
68164137
RL
245 case write_excl:
246 flags |= O_WRONLY | O_CREAT | O_EXCL;
247 break;
248
49d5d881
VZ
249 case read_write:
250 flags |= O_RDWR;
251 break;
252 }
c801d85f 253
92980e90
RR
254 int fd = wxOpen( szFileName, flags ACCESS(accessMode));
255 if ( fd == -1 )
256 {
49d5d881
VZ
257 wxLogSysError(_("can't open file '%s'"), szFileName);
258 return FALSE;
259 }
260 else {
261 Attach(fd);
262 return TRUE;
263 }
c801d85f
KB
264}
265
266// close
61b02744 267bool wxFile::Close()
c801d85f 268{
49d5d881
VZ
269 if ( IsOpened() ) {
270 if ( close(m_fd) == -1 ) {
271 wxLogSysError(_("can't close file descriptor %d"), m_fd);
272 m_fd = fd_invalid;
273 return FALSE;
274 }
275 else
276 m_fd = fd_invalid;
61b02744 277 }
61b02744 278
49d5d881 279 return TRUE;
c801d85f
KB
280}
281
282// ----------------------------------------------------------------------------
283// read/write
284// ----------------------------------------------------------------------------
285
286// read
287off_t wxFile::Read(void *pBuf, off_t nCount)
288{
49d5d881 289 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 290
469e1e5c 291#ifdef __MWERKS__
49d5d881 292 int iRc = ::read(m_fd, (char*) pBuf, nCount);
469e1e5c 293#else
49d5d881 294 int iRc = ::read(m_fd, pBuf, nCount);
469e1e5c 295#endif
49d5d881
VZ
296 if ( iRc == -1 ) {
297 wxLogSysError(_("can't read from file descriptor %d"), m_fd);
298 return wxInvalidOffset;
299 }
300 else
301 return (size_t)iRc;
c801d85f
KB
302}
303
304// write
c86f1403 305size_t wxFile::Write(const void *pBuf, size_t nCount)
c801d85f 306{
49d5d881 307 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 308
469e1e5c 309#ifdef __MWERKS__
5b781a67
SC
310#if __MSL__ >= 0x6000
311 int iRc = ::write(m_fd, (void*) pBuf, nCount);
312#else
49d5d881 313 int iRc = ::write(m_fd, (const char*) pBuf, nCount);
5b781a67 314#endif
469e1e5c 315#else
49d5d881 316 int iRc = ::write(m_fd, pBuf, nCount);
469e1e5c 317#endif
49d5d881
VZ
318 if ( iRc == -1 ) {
319 wxLogSysError(_("can't write to file descriptor %d"), m_fd);
320 m_error = TRUE;
321 return 0;
322 }
323 else
324 return iRc;
c801d85f
KB
325}
326
327// flush
328bool wxFile::Flush()
329{
49d5d881
VZ
330 if ( IsOpened() ) {
331#if defined(__VISUALC__) || wxHAVE_FSYNC
f6bcfd97 332 if ( wxFsync(m_fd) == -1 )
09914df7
VZ
333 {
334 wxLogSysError(_("can't flush file descriptor %d"), m_fd);
335 return FALSE;
336 }
49d5d881 337#else // no fsync
09914df7 338 // just do nothing
49d5d881
VZ
339#endif // fsync
340 }
c801d85f 341
49d5d881 342 return TRUE;
c801d85f
KB
343}
344
345// ----------------------------------------------------------------------------
346// seek
347// ----------------------------------------------------------------------------
348
349// seek
79c3e0e1 350off_t wxFile::Seek(off_t ofs, wxSeekMode mode)
c801d85f 351{
49d5d881
VZ
352 wxASSERT( IsOpened() );
353
a1b82138 354 int origin;
49d5d881 355 switch ( mode ) {
a1b82138
VZ
356 default:
357 wxFAIL_MSG(_("unknown seek origin"));
358
49d5d881 359 case wxFromStart:
a1b82138 360 origin = SEEK_SET;
49d5d881
VZ
361 break;
362
363 case wxFromCurrent:
a1b82138 364 origin = SEEK_CUR;
49d5d881
VZ
365 break;
366
367 case wxFromEnd:
a1b82138 368 origin = SEEK_END;
49d5d881 369 break;
49d5d881
VZ
370 }
371
a1b82138 372 int iRc = lseek(m_fd, ofs, origin);
49d5d881
VZ
373 if ( iRc == -1 ) {
374 wxLogSysError(_("can't seek on file descriptor %d"), m_fd);
375 return wxInvalidOffset;
376 }
377 else
378 return (off_t)iRc;
c801d85f
KB
379}
380
381// get current off_t
382off_t wxFile::Tell() const
383{
49d5d881
VZ
384 wxASSERT( IsOpened() );
385
f6bcfd97 386 int iRc = wxTell(m_fd);
49d5d881
VZ
387 if ( iRc == -1 ) {
388 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd);
389 return wxInvalidOffset;
390 }
391 else
392 return (off_t)iRc;
c801d85f
KB
393}
394
395// get current file length
396off_t wxFile::Length() const
397{
49d5d881 398 wxASSERT( IsOpened() );
c801d85f 399
49d5d881 400#ifdef __VISUALC__
c801d85f 401 int iRc = _filelength(m_fd);
49d5d881 402#else // !VC++
f6bcfd97 403 int iRc = wxTell(m_fd);
c801d85f 404 if ( iRc != -1 ) {
49d5d881
VZ
405 // @ have to use const_cast :-(
406 int iLen = ((wxFile *)this)->SeekEnd();
407 if ( iLen != -1 ) {
408 // restore old position
409 if ( ((wxFile *)this)->Seek(iRc) == -1 ) {
410 // error
411 iLen = -1;
412 }
c801d85f 413 }
c801d85f 414
49d5d881
VZ
415 iRc = iLen;
416 }
417#endif // VC++
418
419 if ( iRc == -1 ) {
420 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd);
421 return wxInvalidOffset;
c801d85f 422 }
49d5d881
VZ
423 else
424 return (off_t)iRc;
c801d85f
KB
425}
426
427// is end of file reached?
428bool wxFile::Eof() const
429{
49d5d881 430 wxASSERT( IsOpened() );
c801d85f 431
49d5d881 432 int iRc;
61b02744 433
d3b4d710 434#if defined(__DOS__) || defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
61b02744
VZ
435 // @@ this doesn't work, of course, on unseekable file descriptors
436 off_t ofsCur = Tell(),
49d5d881 437 ofsMax = Length();
1678ad78 438 if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset )
49d5d881 439 iRc = -1;
61b02744 440 else
49d5d881
VZ
441 iRc = ofsCur == ofsMax;
442#else // Windows and "native" compiler
61b02744 443 iRc = eof(m_fd);
49d5d881 444#endif // Windows/Unix
c801d85f 445
49d5d881
VZ
446 switch ( iRc ) {
447 case 1:
448 break;
c801d85f 449
49d5d881
VZ
450 case 0:
451 return FALSE;
c801d85f 452
49d5d881 453 case -1:
8e3f1261 454 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd);
49d5d881 455 break;
c801d85f 456
49d5d881
VZ
457 default:
458 wxFAIL_MSG(_("invalid eof() return value."));
459 }
c801d85f 460
49d5d881 461 return TRUE;
c801d85f
KB
462}
463
464// ============================================================================
465// implementation of wxTempFile
466// ============================================================================
467
468// ----------------------------------------------------------------------------
469// construction
470// ----------------------------------------------------------------------------
44b62d54 471
c801d85f
KB
472wxTempFile::wxTempFile(const wxString& strName)
473{
49d5d881 474 Open(strName);
c801d85f
KB
475}
476
477bool wxTempFile::Open(const wxString& strName)
478{
44b62d54
VZ
479 // we must have an absolute filename because otherwise CreateTempFileName()
480 // would create the temp file in $TMP (i.e. the system standard location
481 // for the temp files) which might be on another volume/drive/mount and
482 // wxRename()ing it later to m_strName from Commit() would then fail
483 //
484 // with the absolute filename, the temp file is created in the same
485 // directory as this one which ensures that wxRename() may work later
486 wxFileName fn(strName);
487 if ( !fn.IsAbsolute() )
488 {
489 fn.Normalize(wxPATH_NORM_ABSOLUTE);
490 }
491
492 m_strName = fn.GetFullPath();
246037e2 493
44b62d54 494 m_strTemp = wxFileName::CreateTempFileName(m_strName, &m_file);
ade35f11
VZ
495
496 if ( m_strTemp.empty() )
497 {
498 // CreateTempFileName() failed
499 return FALSE;
500 }
49d5d881 501
49d5d881 502#ifdef __UNIX__
ade35f11
VZ
503 // the temp file should have the same permissions as the original one
504 mode_t mode;
ca11abde 505
f6bcfd97 506 wxStructStat st;
ca11abde 507 if ( stat( (const char*) m_strName.fn_str(), &st) == 0 )
49d5d881 508 {
ade35f11 509 mode = st.st_mode;
49d5d881
VZ
510 }
511 else
512 {
ade35f11 513 // file probably didn't exist, just give it the default mode _using_
68164137 514 // user's umask (new files creation should respect umask)
ade35f11
VZ
515 mode_t mask = umask(0777);
516 mode = 0666 & ~mask;
517 umask(mask);
49d5d881 518 }
49d5d881 519
ca11abde 520 if ( chmod( (const char*) m_strTemp.fn_str(), mode) == -1 )
fe99e285 521 {
ade35f11 522 wxLogSysError(_("Failed to set temporary file permissions"));
fe99e285 523 }
49d5d881 524#endif // Unix
246037e2 525
ade35f11 526 return TRUE;
c801d85f
KB
527}
528
529// ----------------------------------------------------------------------------
530// destruction
531// ----------------------------------------------------------------------------
532
533wxTempFile::~wxTempFile()
534{
49d5d881
VZ
535 if ( IsOpened() )
536 Discard();
c801d85f
KB
537}
538
539bool wxTempFile::Commit()
540{
49d5d881 541 m_file.Close();
c801d85f 542
f6bcfd97 543 if ( wxFile::Exists(m_strName) && wxRemove(m_strName) != 0 ) {
49d5d881
VZ
544 wxLogSysError(_("can't remove file '%s'"), m_strName.c_str());
545 return FALSE;
546 }
c801d85f 547
f6bcfd97 548 if ( wxRename(m_strTemp, m_strName) != 0 ) {
49d5d881
VZ
549 wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str());
550 return FALSE;
551 }
c801d85f 552
49d5d881 553 return TRUE;
c801d85f
KB
554}
555
556void wxTempFile::Discard()
557{
49d5d881 558 m_file.Close();
f6bcfd97 559 if ( wxRemove(m_strTemp) != 0 )
49d5d881 560 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str());
c801d85f 561}
ce4169a4 562
ade35f11 563#endif // wxUSE_FILE
cc985fac 564