]> git.saurik.com Git - wxWidgets.git/blame - src/common/file.cpp
DJGPP compilation
[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
b7f411dd
VS
62#elif defined(__DOS__) && defined(__WATCOMC__)
63 #include <io.h>
c2ff79b1
DW
64#elif (defined(__WXPM__))
65 #include <io.h>
c2ff79b1
DW
66 #define W_OK 2
67 #define R_OK 4
34138703 68#elif (defined(__WXSTUBS__))
3f4a0c5b
VZ
69 // Have to ifdef this for different environments
70 #include <io.h>
17dff81c 71#elif (defined(__WXMAC__))
5b781a67 72#if __MSL__ < 0x6000
3f4a0c5b 73 int access( const char *path, int mode ) { return 0 ; }
5b781a67
SC
74#else
75 int _access( const char *path, int mode ) { return 0 ; }
76#endif
3f4a0c5b 77 char* mktemp( char * path ) { return path ;}
5b781a67 78 #include <stat.h>
3f4a0c5b
VZ
79 #define W_OK 2
80 #define R_OK 4
5b781a67 81 #include <unistd.h>
c801d85f 82#else
3f4a0c5b 83 #error "Please specify the header with file functions declarations."
c801d85f
KB
84#endif //Win/UNIX
85
86#include <stdio.h> // SEEK_xxx constants
87#include <fcntl.h> // O_RDONLY &c
a3ef5bf5 88
469e1e5c 89#ifndef __MWERKS__
3f4a0c5b
VZ
90 #include <sys/types.h> // needed for stat
91 #include <sys/stat.h> // stat
de85a884
VZ
92#elif ( defined(__MWERKS__) && defined(__WXMSW__) )
93 #include <sys/types.h> // needed for stat
94 #include <sys/stat.h> // stat
469e1e5c 95#endif
c801d85f 96
3f4a0c5b 97#if defined(__BORLANDC__) || defined(_MSC_VER)
49d5d881
VZ
98 #define W_OK 2
99 #define R_OK 4
34138703
JS
100#endif
101
b12915c1
VZ
102// there is no distinction between text and binary files under Unix, so define
103// O_BINARY as 0 if the system headers don't do it already
104#if defined(__UNIX__) && !defined(O_BINARY)
49d5d881 105 #define O_BINARY (0)
246037e2 106#endif //__UNIX__
c801d85f 107
a3ef5bf5 108#ifdef __SALFORDC__
3f4a0c5b 109 #include <unix.h>
a3ef5bf5
JS
110#endif
111
81d66cf3 112#ifndef MAX_PATH
3f4a0c5b 113 #define MAX_PATH 512
81d66cf3 114#endif
c801d85f 115
49d5d881
VZ
116// some broken compilers don't have 3rd argument in open() and creat()
117#ifdef __SALFORDC__
118 #define ACCESS(access)
119 #define stat _stat
120#else // normal compiler
121 #define ACCESS(access) , (access)
122#endif // Salford C
123
f6bcfd97 124// wxWindows
ade35f11
VZ
125#ifndef WX_PRECOMP
126 #include "wx/string.h"
127 #include "wx/intl.h"
ade35f11
VZ
128 #include "wx/log.h"
129#endif // !WX_PRECOMP
130
131#include "wx/filename.h"
44d568b6 132#include "wx/file.h"
f6bcfd97 133
c801d85f
KB
134// ============================================================================
135// implementation of wxFile
136// ============================================================================
137
138// ----------------------------------------------------------------------------
139// static functions
140// ----------------------------------------------------------------------------
50920146 141bool wxFile::Exists(const wxChar *name)
246037e2 142{
f6bcfd97 143 wxStructStat st;
50920146 144#if wxUSE_UNICODE && wxMBFILES
dcf924a3 145 wxCharBuffer fname = wxConvFile.cWC2MB(name);
a3ef5bf5 146
f6bcfd97
BP
147 return !wxAccess(fname, 0) &&
148 !wxStat(wxMBSTRINGCAST fname, &st) &&
50920146 149 (st.st_mode & S_IFREG);
bedaf53e 150
50920146 151#else
f6bcfd97
BP
152 return !wxAccess(name, 0) &&
153 !wxStat(name, &st) &&
49d5d881 154 (st.st_mode & S_IFREG);
50920146 155#endif
d1427b70
VZ
156}
157
50920146 158bool wxFile::Access(const wxChar *name, OpenMode mode)
d1427b70 159{
49d5d881 160 int how = 0;
d1427b70 161
49d5d881
VZ
162 switch ( mode ) {
163 case read:
164 how = R_OK;
165 break;
d1427b70 166
49d5d881
VZ
167 case write:
168 how = W_OK;
169 break;
d1427b70 170
49d5d881 171 default:
223d09f6 172 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
49d5d881 173 }
d1427b70 174
f6bcfd97 175 return wxAccess(wxFNCONV(name), how) == 0;
c801d85f
KB
176}
177
178// ----------------------------------------------------------------------------
179// opening/closing
180// ----------------------------------------------------------------------------
181
182// ctors
50920146 183wxFile::wxFile(const wxChar *szFileName, OpenMode mode)
c801d85f 184{
49d5d881
VZ
185 m_fd = fd_invalid;
186 m_error = FALSE;
c801d85f 187
49d5d881 188 Open(szFileName, mode);
c801d85f
KB
189}
190
c801d85f 191// create the file, fail if it already exists and bOverwrite
50920146 192bool wxFile::Create(const wxChar *szFileName, bool bOverwrite, int accessMode)
c801d85f 193{
49d5d881
VZ
194 // if bOverwrite we create a new file or truncate the existing one,
195 // otherwise we only create the new file and fail if it already exists
5fde6fcc 196#if defined(__WXMAC__) && !defined(__UNIX__)
5b781a67
SC
197 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
198 // int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
bedaf53e 199 int fd = creat( szFileName , accessMode);
7c74e7fe 200#else
f6bcfd97
BP
201 int fd = wxOpen(wxFNCONV(szFileName),
202 O_BINARY | O_WRONLY | O_CREAT |
203 (bOverwrite ? O_TRUNC : O_EXCL)
204 ACCESS(accessMode));
7c74e7fe 205#endif
49d5d881
VZ
206 if ( fd == -1 ) {
207 wxLogSysError(_("can't create file '%s'"), szFileName);
208 return FALSE;
209 }
210 else {
211 Attach(fd);
212 return TRUE;
213 }
c801d85f
KB
214}
215
216// open the file
50920146 217bool wxFile::Open(const wxChar *szFileName, OpenMode mode, int accessMode)
c801d85f 218{
49d5d881 219 int flags = O_BINARY;
c801d85f 220
49d5d881
VZ
221 switch ( mode ) {
222 case read:
223 flags |= O_RDONLY;
224 break;
c801d85f 225
f6bcfd97
BP
226 case write_append:
227 if ( wxFile::Exists(szFileName) )
228 {
229 flags |= O_WRONLY | O_APPEND;
230 break;
231 }
232 //else: fall through as write_append is the same as write if the
233 // file doesn't exist
234
49d5d881
VZ
235 case write:
236 flags |= O_WRONLY | O_CREAT | O_TRUNC;
237 break;
61b02744 238
68164137
RL
239 case write_excl:
240 flags |= O_WRONLY | O_CREAT | O_EXCL;
241 break;
242
49d5d881
VZ
243 case read_write:
244 flags |= O_RDWR;
245 break;
246 }
c801d85f 247
f6bcfd97 248 int fd = wxOpen(wxFNCONV(szFileName), flags ACCESS(accessMode));
49d5d881
VZ
249 if ( fd == -1 ) {
250 wxLogSysError(_("can't open file '%s'"), szFileName);
251 return FALSE;
252 }
253 else {
254 Attach(fd);
255 return TRUE;
256 }
c801d85f
KB
257}
258
259// close
61b02744 260bool wxFile::Close()
c801d85f 261{
49d5d881
VZ
262 if ( IsOpened() ) {
263 if ( close(m_fd) == -1 ) {
264 wxLogSysError(_("can't close file descriptor %d"), m_fd);
265 m_fd = fd_invalid;
266 return FALSE;
267 }
268 else
269 m_fd = fd_invalid;
61b02744 270 }
61b02744 271
49d5d881 272 return TRUE;
c801d85f
KB
273}
274
275// ----------------------------------------------------------------------------
276// read/write
277// ----------------------------------------------------------------------------
278
279// read
280off_t wxFile::Read(void *pBuf, off_t nCount)
281{
49d5d881 282 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 283
469e1e5c 284#ifdef __MWERKS__
49d5d881 285 int iRc = ::read(m_fd, (char*) pBuf, nCount);
469e1e5c 286#else
49d5d881 287 int iRc = ::read(m_fd, pBuf, nCount);
469e1e5c 288#endif
49d5d881
VZ
289 if ( iRc == -1 ) {
290 wxLogSysError(_("can't read from file descriptor %d"), m_fd);
291 return wxInvalidOffset;
292 }
293 else
294 return (size_t)iRc;
c801d85f
KB
295}
296
297// write
c86f1403 298size_t wxFile::Write(const void *pBuf, size_t nCount)
c801d85f 299{
49d5d881 300 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 301
469e1e5c 302#ifdef __MWERKS__
5b781a67
SC
303#if __MSL__ >= 0x6000
304 int iRc = ::write(m_fd, (void*) pBuf, nCount);
305#else
49d5d881 306 int iRc = ::write(m_fd, (const char*) pBuf, nCount);
5b781a67 307#endif
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
f6bcfd97 325 if ( wxFsync(m_fd) == -1 )
09914df7
VZ
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
f6bcfd97 379 int iRc = wxTell(m_fd);
49d5d881
VZ
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++
f6bcfd97 396 int iRc = wxTell(m_fd);
c801d85f 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 446 case -1:
8e3f1261 447 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd);
49d5d881 448 break;
c801d85f 449
49d5d881
VZ
450 default:
451 wxFAIL_MSG(_("invalid eof() return value."));
452 }
c801d85f 453
49d5d881 454 return TRUE;
c801d85f
KB
455}
456
457// ============================================================================
458// implementation of wxTempFile
459// ============================================================================
460
461// ----------------------------------------------------------------------------
462// construction
463// ----------------------------------------------------------------------------
464wxTempFile::wxTempFile(const wxString& strName)
465{
49d5d881 466 Open(strName);
c801d85f
KB
467}
468
469bool wxTempFile::Open(const wxString& strName)
470{
49d5d881 471 m_strName = strName;
246037e2 472
ade35f11
VZ
473 m_strTemp = wxFileName::CreateTempFileName(strName);
474
475 if ( m_strTemp.empty() )
476 {
477 // CreateTempFileName() failed
478 return FALSE;
479 }
49d5d881 480
3f247475
VZ
481 // actually open the file now (it must already exist)
482 if ( !m_file.Open(m_strTemp, wxFile::write) )
483 {
484 // opening existing file failed?
485 return FALSE;
486 }
487
49d5d881 488#ifdef __UNIX__
ade35f11
VZ
489 // the temp file should have the same permissions as the original one
490 mode_t mode;
fe99e285 491
f6bcfd97 492 wxStructStat st;
50920146 493 if ( stat(strName.fn_str(), &st) == 0 )
49d5d881 494 {
ade35f11 495 mode = st.st_mode;
49d5d881
VZ
496 }
497 else
498 {
ade35f11 499 // file probably didn't exist, just give it the default mode _using_
68164137 500 // user's umask (new files creation should respect umask)
ade35f11
VZ
501 mode_t mask = umask(0777);
502 mode = 0666 & ~mask;
503 umask(mask);
49d5d881 504 }
49d5d881 505
ade35f11 506 if ( chmod(m_strTemp.mb_str(), mode) == -1 )
fe99e285 507 {
ade35f11 508 wxLogSysError(_("Failed to set temporary file permissions"));
fe99e285 509 }
49d5d881 510#endif // Unix
246037e2 511
ade35f11 512 return TRUE;
c801d85f
KB
513}
514
515// ----------------------------------------------------------------------------
516// destruction
517// ----------------------------------------------------------------------------
518
519wxTempFile::~wxTempFile()
520{
49d5d881
VZ
521 if ( IsOpened() )
522 Discard();
c801d85f
KB
523}
524
525bool wxTempFile::Commit()
526{
49d5d881 527 m_file.Close();
c801d85f 528
f6bcfd97 529 if ( wxFile::Exists(m_strName) && wxRemove(m_strName) != 0 ) {
49d5d881
VZ
530 wxLogSysError(_("can't remove file '%s'"), m_strName.c_str());
531 return FALSE;
532 }
c801d85f 533
f6bcfd97 534 if ( wxRename(m_strTemp, m_strName) != 0 ) {
49d5d881
VZ
535 wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str());
536 return FALSE;
537 }
c801d85f 538
49d5d881 539 return TRUE;
c801d85f
KB
540}
541
542void wxTempFile::Discard()
543{
49d5d881 544 m_file.Close();
f6bcfd97 545 if ( wxRemove(m_strTemp) != 0 )
49d5d881 546 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str());
c801d85f 547}
ce4169a4 548
ade35f11 549#endif // wxUSE_FILE
cc985fac 550