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