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