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