]> git.saurik.com Git - wxWidgets.git/blob - src/common/file.cpp
Compile and build fixes for wxGTK and wxMotif
[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_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
178
179 if ( fd == -1 ) {
180 wxLogSysError(_("can't create file '%s'"), szFileName);
181 return FALSE;
182 }
183 else {
184 Attach(fd);
185 return TRUE;
186 }
187 }
188
189 // open the file
190 bool wxFile::Open(const char *szFileName, OpenMode mode, int access)
191 {
192 int flags = O_BINARY;
193
194 switch ( mode ) {
195 case read:
196 flags |= O_RDONLY;
197 break;
198
199 case write:
200 flags |= O_WRONLY | O_CREAT | O_TRUNC;
201 break;
202
203 case write_append:
204 flags |= O_WRONLY | O_APPEND;
205 break;
206
207 case read_write:
208 flags |= O_RDWR;
209 break;
210 }
211
212 int fd = open(szFileName, flags, access);
213
214 if ( fd == -1 ) {
215 wxLogSysError(_("can't open file '%s'"), szFileName);
216 return FALSE;
217 }
218 else {
219 Attach(fd);
220 return TRUE;
221 }
222 }
223
224 // close
225 bool wxFile::Close()
226 {
227 if ( IsOpened() ) {
228 if ( close(m_fd) == -1 ) {
229 wxLogSysError(_("can't close file descriptor %d"), m_fd);
230 m_fd = fd_invalid;
231 return FALSE;
232 }
233 else
234 m_fd = fd_invalid;
235 }
236
237 return TRUE;
238 }
239
240 // ----------------------------------------------------------------------------
241 // read/write
242 // ----------------------------------------------------------------------------
243
244 // read
245 off_t wxFile::Read(void *pBuf, off_t nCount)
246 {
247 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
248
249 int iRc = ::read(m_fd, pBuf, nCount);
250 if ( iRc == -1 ) {
251 wxLogSysError(_("can't read from file descriptor %d"), m_fd);
252 return wxInvalidOffset;
253 }
254 else
255 return (size_t)iRc;
256 }
257
258 // write
259 size_t wxFile::Write(const void *pBuf, size_t nCount)
260 {
261 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
262
263 int iRc = ::write(m_fd, pBuf, nCount);
264 if ( iRc == -1 ) {
265 wxLogSysError(_("can't write to file descriptor %d"), m_fd);
266 m_error = TRUE;
267 return 0;
268 }
269 else
270 return iRc;
271 }
272
273 // flush
274 bool wxFile::Flush()
275 {
276 if ( IsOpened() ) {
277 // @@@ fsync() is not ANSI (BSDish)
278 // if ( fsync(m_fd) == -1 ) { // TODO
279 if (wxTrue) {
280 wxLogSysError(_("can't flush file descriptor %d"), m_fd);
281 return FALSE;
282 }
283 }
284
285 return TRUE;
286 }
287
288 // ----------------------------------------------------------------------------
289 // seek
290 // ----------------------------------------------------------------------------
291
292 // seek
293 off_t wxFile::Seek(off_t ofs, wxSeekMode mode)
294 {
295 wxASSERT( IsOpened() );
296
297 int flag = -1;
298 switch ( mode ) {
299 case wxFromStart:
300 flag = SEEK_SET;
301 break;
302
303 case wxFromCurrent:
304 flag = SEEK_CUR;
305 break;
306
307 case wxFromEnd:
308 flag = SEEK_END;
309 break;
310
311 default:
312 wxFAIL_MSG(_("unknown seek origin"));
313 }
314
315 int iRc = lseek(m_fd, ofs, flag);
316 if ( iRc == -1 ) {
317 wxLogSysError(_("can't seek on file descriptor %d"), m_fd);
318 return wxInvalidOffset;
319 }
320 else
321 return (off_t)iRc;
322 }
323
324 // get current off_t
325 off_t wxFile::Tell() const
326 {
327 wxASSERT( IsOpened() );
328
329 int iRc = tell(m_fd);
330 if ( iRc == -1 ) {
331 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd);
332 return wxInvalidOffset;
333 }
334 else
335 return (off_t)iRc;
336 }
337
338 // get current file length
339 off_t wxFile::Length() const
340 {
341 wxASSERT( IsOpened() );
342
343 #ifdef _MSC_VER
344 int iRc = _filelength(m_fd);
345 #else
346 int iRc = tell(m_fd);
347 if ( iRc != -1 ) {
348 // @ have to use const_cast :-(
349 int iLen = ((wxFile *)this)->SeekEnd();
350 if ( iLen != -1 ) {
351 // restore old position
352 if ( ((wxFile *)this)->Seek(iRc) == -1 ) {
353 // error
354 iLen = -1;
355 }
356 }
357
358 iRc = iLen;
359 }
360
361 #endif //_MSC_VER
362
363 if ( iRc == -1 ) {
364 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd);
365 return wxInvalidOffset;
366 }
367 else
368 return (off_t)iRc;
369 }
370
371 // is end of file reached?
372 bool wxFile::Eof() const
373 {
374 wxASSERT( IsOpened() );
375
376 int iRc;
377
378 #if defined(__UNIX__) || defined(__GNUWIN32__)
379 // @@ this doesn't work, of course, on unseekable file descriptors
380 off_t ofsCur = Tell(),
381 ofsMax = Length();
382 if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset )
383 iRc = -1;
384 else
385 iRc = ofsCur == ofsMax;
386 #else // Windows and "native" compiler
387 iRc = eof(m_fd);
388 #endif // Windows/Unix
389
390 switch ( iRc ) {
391 case 1:
392 break;
393
394 case 0:
395 return FALSE;
396
397 case -1:
398 wxLogSysError(_("can't determine if the end of file is reached on \
399 descriptor %d"), m_fd);
400 break;
401
402 default:
403 wxFAIL_MSG(_("invalid eof() return value."));
404 }
405
406 return TRUE;
407 }
408
409 // ============================================================================
410 // implementation of wxTempFile
411 // ============================================================================
412
413 // ----------------------------------------------------------------------------
414 // construction
415 // ----------------------------------------------------------------------------
416 wxTempFile::wxTempFile(const wxString& strName)
417 {
418 Open(strName);
419 }
420
421 bool wxTempFile::Open(const wxString& strName)
422 {
423 m_strName = strName;
424
425 // we want to create the file in the same directory as strName because
426 // otherwise rename() in Commit() might not work (if the files are on
427 // different partitions for example). Unfortunately, the only standard
428 // (POSIX) temp file creation function tmpnam() can't do it.
429 #if defined(__UNIX__) || defined(__WXSTUBS__)
430 static const char *szMktempSuffix = "XXXXXX";
431 m_strTemp << strName << szMktempSuffix;
432 mktemp((char *)m_strTemp.c_str()); // will do because length doesn't change
433 #else // Windows
434 wxString strPath;
435 wxSplitPath(strName, &strPath, NULL, NULL);
436 if ( strPath.IsEmpty() )
437 strPath = '.'; // GetTempFileName will fail if we give it empty string
438 #ifdef __WIN32__
439 if ( !GetTempFileName(strPath, "wx_",0, m_strTemp.GetWriteBuf(MAX_PATH)) )
440 #else
441 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
442 if ( !GetTempFileName((BYTE) (const char*) strPath, "wx_",0, m_strTemp.GetWriteBuf(MAX_PATH)) )
443 #endif
444 wxLogLastError("GetTempFileName");
445 m_strTemp.UngetWriteBuf();
446 #endif // Windows/Unix
447
448 return m_file.Open(m_strTemp, wxFile::write);
449 }
450
451 // ----------------------------------------------------------------------------
452 // destruction
453 // ----------------------------------------------------------------------------
454
455 wxTempFile::~wxTempFile()
456 {
457 if ( IsOpened() )
458 Discard();
459 }
460
461 bool wxTempFile::Commit()
462 {
463 m_file.Close();
464
465 if ( wxFile::Exists(m_strName) && remove(m_strName) != 0 ) {
466 wxLogSysError(_("can't remove file '%s'"), m_strName.c_str());
467 return FALSE;
468 }
469
470 if ( rename(m_strTemp, m_strName) != 0 ) {
471 wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str());
472 return FALSE;
473 }
474
475 return TRUE;
476 }
477
478 void wxTempFile::Discard()
479 {
480 m_file.Close();
481 if ( remove(m_strTemp) != 0 )
482 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str());
483 }