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