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