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