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