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