]> git.saurik.com Git - wxWidgets.git/blame - src/common/file.cpp
1. added wxTreeCtrl::DeleteChildren()
[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
5fc5e442
VZ
177 int fd = open(szFileName, O_WRONLY | O_CREAT |
178 (bOverwrite ? O_TRUNC : O_EXCL), access);
c801d85f
KB
179
180 if ( fd == -1 ) {
1a5a8367 181 wxLogSysError(_("can't create file '%s'"), szFileName);
c801d85f
KB
182 return FALSE;
183 }
184 else {
185 Attach(fd);
186 return TRUE;
187 }
188}
189
190// open the file
246037e2 191bool wxFile::Open(const char *szFileName, OpenMode mode, int access)
c801d85f
KB
192{
193 int flags = O_BINARY;
194
195 switch ( mode ) {
246037e2
VZ
196 case read:
197 flags |= O_RDONLY;
c801d85f
KB
198 break;
199
246037e2
VZ
200 case write:
201 flags |= O_WRONLY | O_CREAT | O_TRUNC;
61b02744
VZ
202 break;
203
204 case write_append:
205 flags |= O_WRONLY | O_APPEND;
c801d85f
KB
206 break;
207
246037e2 208 case read_write:
c801d85f
KB
209 flags |= O_RDWR;
210 break;
211 }
212
246037e2 213 int fd = open(szFileName, flags, access);
c801d85f
KB
214
215 if ( fd == -1 ) {
1a5a8367 216 wxLogSysError(_("can't open file '%s'"), szFileName);
c801d85f
KB
217 return FALSE;
218 }
219 else {
220 Attach(fd);
221 return TRUE;
222 }
223}
224
225// close
61b02744 226bool wxFile::Close()
c801d85f
KB
227{
228 if ( IsOpened() ) {
61b02744 229 if ( close(m_fd) == -1 ) {
1a5a8367 230 wxLogSysError(_("can't close file descriptor %d"), m_fd);
61b02744
VZ
231 m_fd = fd_invalid;
232 return FALSE;
233 }
234 else
235 m_fd = fd_invalid;
c801d85f 236 }
61b02744
VZ
237
238 return TRUE;
c801d85f
KB
239}
240
241// ----------------------------------------------------------------------------
242// read/write
243// ----------------------------------------------------------------------------
244
245// read
246off_t wxFile::Read(void *pBuf, off_t nCount)
247{
1311c7a9 248 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f
KB
249
250 int iRc = ::read(m_fd, pBuf, nCount);
251 if ( iRc == -1 ) {
1a5a8367 252 wxLogSysError(_("can't read from file descriptor %d"), m_fd);
1678ad78 253 return wxInvalidOffset;
c801d85f
KB
254 }
255 else
c86f1403 256 return (size_t)iRc;
c801d85f
KB
257}
258
259// write
c86f1403 260size_t wxFile::Write(const void *pBuf, size_t nCount)
c801d85f 261{
1311c7a9 262 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f
KB
263
264 int iRc = ::write(m_fd, pBuf, nCount);
265 if ( iRc == -1 ) {
1a5a8367 266 wxLogSysError(_("can't write to file descriptor %d"), m_fd);
79c3e0e1
GL
267 m_error = TRUE;
268 return 0;
c801d85f
KB
269 }
270 else
1678ad78 271 return iRc;
c801d85f
KB
272}
273
274// flush
275bool wxFile::Flush()
276{
277 if ( IsOpened() ) {
1678ad78 278 // @@@ fsync() is not ANSI (BSDish)
c801d85f 279// if ( fsync(m_fd) == -1 ) { // TODO
341287bf 280 if (wxTrue) {
1a5a8367 281 wxLogSysError(_("can't flush file descriptor %d"), m_fd);
c801d85f
KB
282 return FALSE;
283 }
284 }
285
286 return TRUE;
287}
288
289// ----------------------------------------------------------------------------
290// seek
291// ----------------------------------------------------------------------------
292
293// seek
79c3e0e1 294off_t wxFile::Seek(off_t ofs, wxSeekMode mode)
c801d85f
KB
295{
296 wxASSERT( IsOpened() );
297
298 int flag = -1;
299 switch ( mode ) {
79c3e0e1 300 case wxFromStart:
c801d85f
KB
301 flag = SEEK_SET;
302 break;
303
79c3e0e1 304 case wxFromCurrent:
c801d85f
KB
305 flag = SEEK_CUR;
306 break;
307
79c3e0e1 308 case wxFromEnd:
c801d85f
KB
309 flag = SEEK_END;
310 break;
311
312 default:
1a5a8367 313 wxFAIL_MSG(_("unknown seek origin"));
c801d85f
KB
314 }
315
316 int iRc = lseek(m_fd, ofs, flag);
317 if ( iRc == -1 ) {
1a5a8367 318 wxLogSysError(_("can't seek on file descriptor %d"), m_fd);
1678ad78 319 return wxInvalidOffset;
c801d85f
KB
320 }
321 else
322 return (off_t)iRc;
323}
324
325// get current off_t
326off_t wxFile::Tell() const
327{
328 wxASSERT( IsOpened() );
329
330 int iRc = tell(m_fd);
331 if ( iRc == -1 ) {
1a5a8367 332 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd);
1678ad78 333 return wxInvalidOffset;
c801d85f
KB
334 }
335 else
336 return (off_t)iRc;
337}
338
339// get current file length
340off_t wxFile::Length() const
341{
342 wxASSERT( IsOpened() );
343
344 #ifdef _MSC_VER
345 int iRc = _filelength(m_fd);
346 #else
347 int iRc = tell(m_fd);
348 if ( iRc != -1 ) {
246037e2 349 // @ have to use const_cast :-(
c801d85f
KB
350 int iLen = ((wxFile *)this)->SeekEnd();
351 if ( iLen != -1 ) {
352 // restore old position
353 if ( ((wxFile *)this)->Seek(iRc) == -1 ) {
354 // error
355 iLen = -1;
356 }
357 }
358
359 iRc = iLen;
360 }
361
362 #endif //_MSC_VER
363
364 if ( iRc == -1 ) {
1a5a8367 365 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd);
1678ad78 366 return wxInvalidOffset;
c801d85f
KB
367 }
368 else
369 return (off_t)iRc;
370}
371
372// is end of file reached?
373bool wxFile::Eof() const
374{
375 wxASSERT( IsOpened() );
376
61b02744
VZ
377 int iRc;
378
379 #if defined(__UNIX__) || defined(__GNUWIN32__)
380 // @@ this doesn't work, of course, on unseekable file descriptors
381 off_t ofsCur = Tell(),
382 ofsMax = Length();
1678ad78 383 if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset )
61b02744
VZ
384 iRc = -1;
385 else
386 iRc = ofsCur == ofsMax;
387 #else // Windows and "native" compiler
388 iRc = eof(m_fd);
389 #endif // Windows/Unix
c801d85f
KB
390
391 switch ( iRc ) {
392 case 1:
393 break;
394
395 case 0:
396 return FALSE;
397
398 case -1:
1a5a8367
DP
399 wxLogSysError(_("can't determine if the end of file is reached on \
400descriptor %d"), m_fd);
c801d85f
KB
401 break;
402
403 default:
1a5a8367 404 wxFAIL_MSG(_("invalid eof() return value."));
c801d85f
KB
405 }
406
407 return TRUE;
408}
409
410// ============================================================================
411// implementation of wxTempFile
412// ============================================================================
413
414// ----------------------------------------------------------------------------
415// construction
416// ----------------------------------------------------------------------------
417wxTempFile::wxTempFile(const wxString& strName)
418{
419 Open(strName);
420}
421
422bool wxTempFile::Open(const wxString& strName)
423{
424 m_strName = strName;
246037e2 425
b59650be
VZ
426 // we want to create the file in the same directory as strName because
427 // otherwise rename() in Commit() might not work (if the files are on
428 // different partitions for example). Unfortunately, the only standard
429 // (POSIX) temp file creation function tmpnam() can't do it.
34138703 430 #if defined(__UNIX__) || defined(__WXSTUBS__)
b59650be
VZ
431 static const char *szMktempSuffix = "XXXXXX";
432 m_strTemp << strName << szMktempSuffix;
30a5be97 433 mktemp((char *)m_strTemp.c_str()); // will do because length doesn't change
b59650be 434 #else // Windows
30a5be97
VZ
435 wxString strPath;
436 wxSplitPath(strName, &strPath, NULL, NULL);
437 if ( strPath.IsEmpty() )
438 strPath = '.'; // GetTempFileName will fail if we give it empty string
81d66cf3 439#ifdef __WIN32__
30a5be97 440 if ( !GetTempFileName(strPath, "wx_",0, m_strTemp.GetWriteBuf(MAX_PATH)) )
81d66cf3
JS
441#else
442 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
443 if ( !GetTempFileName((BYTE) (const char*) strPath, "wx_",0, m_strTemp.GetWriteBuf(MAX_PATH)) )
444#endif
30a5be97
VZ
445 wxLogLastError("GetTempFileName");
446 m_strTemp.UngetWriteBuf();
b59650be 447 #endif // Windows/Unix
246037e2 448
c801d85f
KB
449 return m_file.Open(m_strTemp, wxFile::write);
450}
451
452// ----------------------------------------------------------------------------
453// destruction
454// ----------------------------------------------------------------------------
455
456wxTempFile::~wxTempFile()
457{
458 if ( IsOpened() )
459 Discard();
460}
461
462bool wxTempFile::Commit()
463{
464 m_file.Close();
465
466 if ( wxFile::Exists(m_strName) && remove(m_strName) != 0 ) {
1a5a8367 467 wxLogSysError(_("can't remove file '%s'"), m_strName.c_str());
c801d85f
KB
468 return FALSE;
469 }
470
471 if ( rename(m_strTemp, m_strName) != 0 ) {
1a5a8367 472 wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str());
c801d85f
KB
473 return FALSE;
474 }
475
476 return TRUE;
477}
478
479void wxTempFile::Discard()
480{
481 m_file.Close();
482 if ( remove(m_strTemp) != 0 )
1a5a8367 483 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str());
c801d85f 484}