]> git.saurik.com Git - wxWidgets.git/blame - src/common/file.cpp
Cured some small doc typos; some WIN16 fixes; transferred DLL WinMain to
[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"
23#include "wx/defs.h"
24
25#ifdef __BORLANDC__
3f4a0c5b 26 #pragma hdrstop
c801d85f
KB
27#endif
28
29// standard
246037e2 30#if defined(__WXMSW__) && !defined(__GNUWIN32__)
c801d85f 31 #include <io.h>
30a5be97 32
a3ef5bf5 33#ifndef __SALFORDC__
30a5be97
VZ
34 #define WIN32_LEAN_AND_MEAN
35 #define NOSERVICE
36 #define NOIME
37 #define NOATOM
38 #define NOGDI
39 #define NOGDICAPMASKS
40 #define NOMETAFILE
41 #define NOMINMAX
42 #define NOMSG
43 #define NOOPENFILE
44 #define NORASTEROPS
45 #define NOSCROLL
46 #define NOSOUND
47 #define NOSYSMETRICS
48 #define NOTEXTMETRIC
49 #define NOWH
50 #define NOCOMM
51 #define NOKANJI
52 #define NOCRYPT
53 #define NOMCX
a3ef5bf5
JS
54#endif
55
30a5be97 56 #include <windows.h> // for GetTempFileName
c801d85f 57#elif (defined(__UNIX__) || defined(__GNUWIN32__))
3f4a0c5b
VZ
58 #include <unistd.h>
59 #ifdef __GNUWIN32__
60 #include <windows.h>
61 #endif
34138703 62#elif (defined(__WXSTUBS__))
3f4a0c5b
VZ
63 // Have to ifdef this for different environments
64 #include <io.h>
17dff81c 65#elif (defined(__WXMAC__))
3f4a0c5b
VZ
66 int access( const char *path, int mode ) { return 0 ; }
67 char* mktemp( char * path ) { return path ;}
68 #include <unistd.h>
69 #include <unix.h>
70 #define W_OK 2
71 #define R_OK 4
c801d85f 72#else
3f4a0c5b 73 #error "Please specify the header with file functions declarations."
c801d85f
KB
74#endif //Win/UNIX
75
76#include <stdio.h> // SEEK_xxx constants
77#include <fcntl.h> // O_RDONLY &c
a3ef5bf5 78
469e1e5c 79#ifndef __MWERKS__
3f4a0c5b
VZ
80 #include <sys/types.h> // needed for stat
81 #include <sys/stat.h> // stat
469e1e5c 82#endif
c801d85f
KB
83
84// Microsoft compiler loves underscores, feed them to it
3f4a0c5b 85#ifdef __VISUALC__
c801d85f
KB
86 // functions
87 #define open _open
88 #define close _close
89 #define read _read
90 #define write _write
91 #define lseek _lseek
92 #define fsync _commit
93 #define access _access
94 #define eof _eof
95
96 // types
97 #define stat _stat
98
99 // constants
3f4a0c5b 100
c801d85f
KB
101 #define O_RDONLY _O_RDONLY
102 #define O_WRONLY _O_WRONLY
103 #define O_RDWR _O_RDWR
104 #define O_EXCL _O_EXCL
105 #define O_CREAT _O_CREAT
106 #define O_BINARY _O_BINARY
107
108 #define S_IFDIR _S_IFDIR
109 #define S_IFREG _S_IFREG
c801d85f
KB
110#else
111 #define tell(fd) lseek(fd, 0, SEEK_CUR)
3f4a0c5b 112#endif // VC++
c801d85f 113
3f4a0c5b 114#if defined(__BORLANDC__) || defined(_MSC_VER)
34138703
JS
115 #define W_OK 2
116 #define R_OK 4
117#endif
118
c801d85f 119// there is no distinction between text and binary files under Unix
246037e2
VZ
120#ifdef __UNIX__
121 #define O_BINARY (0)
122#endif //__UNIX__
c801d85f 123
a3ef5bf5 124#ifdef __SALFORDC__
3f4a0c5b 125 #include <unix.h>
a3ef5bf5
JS
126#endif
127
c801d85f
KB
128// wxWindows
129#include <wx/string.h>
130#include <wx/intl.h>
131#include <wx/file.h>
132#include <wx/log.h>
133
81d66cf3 134#ifndef MAX_PATH
3f4a0c5b 135 #define MAX_PATH 512
81d66cf3 136#endif
c801d85f 137
169935ad 138#ifdef __WXMAC__
3f4a0c5b
VZ
139 char gwxMacFileName[ MAX_PATH ] ;
140 char gwxMacFileName2[ MAX_PATH ] ;
141 char gwxMacFileName3[ MAX_PATH ] ;
169935ad
SC
142#endif
143
c801d85f
KB
144// ============================================================================
145// implementation of wxFile
146// ============================================================================
147
148// ----------------------------------------------------------------------------
149// static functions
150// ----------------------------------------------------------------------------
d1427b70 151bool wxFile::Exists(const char *name)
246037e2 152{
a3ef5bf5
JS
153#ifdef __SALFORDC__
154 struct _stat st;
155#else
246037e2 156 struct stat st;
a3ef5bf5
JS
157#endif
158
159 return !access(name, 0) && !stat((char*) name, &st) && (st.st_mode & S_IFREG);
d1427b70
VZ
160}
161
162bool wxFile::Access(const char *name, OpenMode mode)
163{
8fdca65c 164 int how = 0;
d1427b70
VZ
165
166 switch ( mode ) {
167 case read:
168 how = R_OK;
169 break;
170
171 case write:
172 how = W_OK;
173 break;
174
175 default:
176 wxFAIL_MSG("bad wxFile::Access mode parameter.");
177 }
178
179 return access(name, how) == 0;
c801d85f
KB
180}
181
182// ----------------------------------------------------------------------------
183// opening/closing
184// ----------------------------------------------------------------------------
185
186// ctors
187wxFile::wxFile(const char *szFileName, OpenMode mode)
188{
189 m_fd = fd_invalid;
79c3e0e1 190 m_error = FALSE;
c801d85f
KB
191
192 Open(szFileName, mode);
193}
194
195// dtor
196wxFile::~wxFile()
197{
198 Close();
199}
200
201// create the file, fail if it already exists and bOverwrite
a3ef5bf5 202bool wxFile::Create(const char *szFileName, bool bOverwrite, int accessMode)
c801d85f
KB
203{
204 // if bOverwrite we create a new file or truncate the existing one,
205 // otherwise we only create the new file and fail if it already exists
a3ef5bf5 206#ifdef __SALFORDC__
3f4a0c5b 207 int fd = open(szFileName, O_WRONLY | O_CREAT |
a3ef5bf5
JS
208 (bOverwrite ? O_TRUNC : O_EXCL));
209#else
210 int fd = open(szFileName, O_WRONLY | O_CREAT |
211 (bOverwrite ? O_TRUNC : O_EXCL), accessMode);
212#endif
c801d85f
KB
213
214 if ( fd == -1 ) {
1a5a8367 215 wxLogSysError(_("can't create file '%s'"), szFileName);
c801d85f
KB
216 return FALSE;
217 }
218 else {
219 Attach(fd);
220 return TRUE;
221 }
222}
223
224// open the file
a3ef5bf5 225bool wxFile::Open(const char *szFileName, OpenMode mode, int accessMode)
c801d85f
KB
226{
227 int flags = O_BINARY;
228
229 switch ( mode ) {
246037e2
VZ
230 case read:
231 flags |= O_RDONLY;
c801d85f
KB
232 break;
233
246037e2
VZ
234 case write:
235 flags |= O_WRONLY | O_CREAT | O_TRUNC;
61b02744
VZ
236 break;
237
238 case write_append:
239 flags |= O_WRONLY | O_APPEND;
c801d85f
KB
240 break;
241
246037e2 242 case read_write:
c801d85f
KB
243 flags |= O_RDWR;
244 break;
245 }
246
a3ef5bf5
JS
247#ifdef __SALFORDC__
248 int fd = open(szFileName, flags);
249#else
250 int fd = open(szFileName, flags, accessMode);
251#endif
c801d85f
KB
252
253 if ( fd == -1 ) {
1a5a8367 254 wxLogSysError(_("can't open file '%s'"), szFileName);
c801d85f
KB
255 return FALSE;
256 }
257 else {
258 Attach(fd);
259 return TRUE;
260 }
261}
262
263// close
61b02744 264bool wxFile::Close()
c801d85f
KB
265{
266 if ( IsOpened() ) {
61b02744 267 if ( close(m_fd) == -1 ) {
1a5a8367 268 wxLogSysError(_("can't close file descriptor %d"), m_fd);
61b02744
VZ
269 m_fd = fd_invalid;
270 return FALSE;
271 }
272 else
273 m_fd = fd_invalid;
c801d85f 274 }
61b02744
VZ
275
276 return TRUE;
c801d85f
KB
277}
278
279// ----------------------------------------------------------------------------
280// read/write
281// ----------------------------------------------------------------------------
282
283// read
284off_t wxFile::Read(void *pBuf, off_t nCount)
285{
1311c7a9 286 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 287
469e1e5c
SC
288#ifdef __MWERKS__
289 int iRc = ::read(m_fd, (char*) pBuf, nCount);
290#else
c801d85f 291 int iRc = ::read(m_fd, pBuf, nCount);
469e1e5c 292#endif
c801d85f 293 if ( iRc == -1 ) {
1a5a8367 294 wxLogSysError(_("can't read from file descriptor %d"), m_fd);
1678ad78 295 return wxInvalidOffset;
c801d85f
KB
296 }
297 else
c86f1403 298 return (size_t)iRc;
c801d85f
KB
299}
300
301// write
c86f1403 302size_t wxFile::Write(const void *pBuf, size_t nCount)
c801d85f 303{
1311c7a9 304 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 305
469e1e5c
SC
306#ifdef __MWERKS__
307 int iRc = ::write(m_fd, (const char*) pBuf, nCount);
308#else
c801d85f 309 int iRc = ::write(m_fd, pBuf, nCount);
469e1e5c 310#endif
c801d85f 311 if ( iRc == -1 ) {
1a5a8367 312 wxLogSysError(_("can't write to file descriptor %d"), m_fd);
79c3e0e1
GL
313 m_error = TRUE;
314 return 0;
c801d85f
KB
315 }
316 else
1678ad78 317 return iRc;
c801d85f
KB
318}
319
320// flush
321bool wxFile::Flush()
322{
323 if ( IsOpened() ) {
3f4a0c5b 324 #if defined(__VISUALC__) || wxHAVE_FSYNC
09914df7
VZ
325 if ( fsync(m_fd) == -1 )
326 {
327 wxLogSysError(_("can't flush file descriptor %d"), m_fd);
328 return FALSE;
329 }
330 #else // no fsync
331 // just do nothing
332 #endif // fsync
c801d85f
KB
333 }
334
335 return TRUE;
336}
337
338// ----------------------------------------------------------------------------
339// seek
340// ----------------------------------------------------------------------------
341
342// seek
79c3e0e1 343off_t wxFile::Seek(off_t ofs, wxSeekMode mode)
c801d85f
KB
344{
345 wxASSERT( IsOpened() );
346
347 int flag = -1;
348 switch ( mode ) {
79c3e0e1 349 case wxFromStart:
c801d85f
KB
350 flag = SEEK_SET;
351 break;
352
79c3e0e1 353 case wxFromCurrent:
c801d85f
KB
354 flag = SEEK_CUR;
355 break;
356
79c3e0e1 357 case wxFromEnd:
c801d85f
KB
358 flag = SEEK_END;
359 break;
360
361 default:
1a5a8367 362 wxFAIL_MSG(_("unknown seek origin"));
c801d85f
KB
363 }
364
365 int iRc = lseek(m_fd, ofs, flag);
366 if ( iRc == -1 ) {
1a5a8367 367 wxLogSysError(_("can't seek on file descriptor %d"), m_fd);
1678ad78 368 return wxInvalidOffset;
c801d85f
KB
369 }
370 else
371 return (off_t)iRc;
372}
373
374// get current off_t
375off_t wxFile::Tell() const
376{
377 wxASSERT( IsOpened() );
378
379 int iRc = tell(m_fd);
380 if ( iRc == -1 ) {
1a5a8367 381 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd);
1678ad78 382 return wxInvalidOffset;
c801d85f
KB
383 }
384 else
385 return (off_t)iRc;
386}
387
388// get current file length
389off_t wxFile::Length() const
390{
391 wxASSERT( IsOpened() );
392
3f4a0c5b 393 #ifdef __VISUALC__
c801d85f 394 int iRc = _filelength(m_fd);
3f4a0c5b 395 #else // !VC++
c801d85f
KB
396 int iRc = tell(m_fd);
397 if ( iRc != -1 ) {
246037e2 398 // @ have to use const_cast :-(
c801d85f
KB
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 }
406 }
407
408 iRc = iLen;
409 }
3f4a0c5b 410 #endif // VC++
c801d85f
KB
411
412 if ( iRc == -1 ) {
1a5a8367 413 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd);
1678ad78 414 return wxInvalidOffset;
c801d85f
KB
415 }
416 else
417 return (off_t)iRc;
418}
419
420// is end of file reached?
421bool wxFile::Eof() const
422{
423 wxASSERT( IsOpened() );
424
61b02744
VZ
425 int iRc;
426
a3ef5bf5 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(),
430 ofsMax = Length();
1678ad78 431 if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset )
61b02744
VZ
432 iRc = -1;
433 else
434 iRc = ofsCur == ofsMax;
435 #else // Windows and "native" compiler
436 iRc = eof(m_fd);
437 #endif // Windows/Unix
c801d85f
KB
438
439 switch ( iRc ) {
440 case 1:
441 break;
442
443 case 0:
444 return FALSE;
445
446 case -1:
1a5a8367
DP
447 wxLogSysError(_("can't determine if the end of file is reached on \
448descriptor %d"), m_fd);
c801d85f
KB
449 break;
450
451 default:
1a5a8367 452 wxFAIL_MSG(_("invalid eof() return value."));
c801d85f
KB
453 }
454
455 return TRUE;
456}
457
458// ============================================================================
459// implementation of wxTempFile
460// ============================================================================
461
462// ----------------------------------------------------------------------------
463// construction
464// ----------------------------------------------------------------------------
465wxTempFile::wxTempFile(const wxString& strName)
466{
467 Open(strName);
468}
469
470bool wxTempFile::Open(const wxString& strName)
471{
472 m_strName = strName;
246037e2 473
b59650be
VZ
474 // we want to create the file in the same directory as strName because
475 // otherwise rename() in Commit() might not work (if the files are on
476 // different partitions for example). Unfortunately, the only standard
477 // (POSIX) temp file creation function tmpnam() can't do it.
17dff81c 478 #if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ )
b59650be
VZ
479 static const char *szMktempSuffix = "XXXXXX";
480 m_strTemp << strName << szMktempSuffix;
30a5be97 481 mktemp((char *)m_strTemp.c_str()); // will do because length doesn't change
b59650be 482 #else // Windows
30a5be97
VZ
483 wxString strPath;
484 wxSplitPath(strName, &strPath, NULL, NULL);
485 if ( strPath.IsEmpty() )
486 strPath = '.'; // GetTempFileName will fail if we give it empty string
81d66cf3 487#ifdef __WIN32__
30a5be97 488 if ( !GetTempFileName(strPath, "wx_",0, m_strTemp.GetWriteBuf(MAX_PATH)) )
81d66cf3
JS
489#else
490 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
491 if ( !GetTempFileName((BYTE) (const char*) strPath, "wx_",0, m_strTemp.GetWriteBuf(MAX_PATH)) )
492#endif
30a5be97
VZ
493 wxLogLastError("GetTempFileName");
494 m_strTemp.UngetWriteBuf();
b59650be 495 #endif // Windows/Unix
246037e2 496
c801d85f
KB
497 return m_file.Open(m_strTemp, wxFile::write);
498}
499
500// ----------------------------------------------------------------------------
501// destruction
502// ----------------------------------------------------------------------------
503
504wxTempFile::~wxTempFile()
505{
506 if ( IsOpened() )
507 Discard();
508}
509
510bool wxTempFile::Commit()
511{
512 m_file.Close();
513
514 if ( wxFile::Exists(m_strName) && remove(m_strName) != 0 ) {
1a5a8367 515 wxLogSysError(_("can't remove file '%s'"), m_strName.c_str());
c801d85f
KB
516 return FALSE;
517 }
518
519 if ( rename(m_strTemp, m_strName) != 0 ) {
1a5a8367 520 wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str());
c801d85f
KB
521 return FALSE;
522 }
523
524 return TRUE;
525}
526
527void wxTempFile::Discard()
528{
529 m_file.Close();
530 if ( remove(m_strTemp) != 0 )
1a5a8367 531 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str());
c801d85f 532}