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