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