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