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