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