]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
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> | |
55d99c7a | 10 | // Licence: wxWindows licence |
c801d85f KB |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
13 | // ---------------------------------------------------------------------------- | |
14 | // headers | |
15 | // ---------------------------------------------------------------------------- | |
16 | ||
17 | #ifdef __GNUG__ | |
3f4a0c5b | 18 | #pragma implementation "file.h" |
c801d85f KB |
19 | #endif |
20 | ||
21 | // For compilers that support precompilation, includes "wx.h". | |
22 | #include "wx/wxprec.h" | |
c801d85f KB |
23 | |
24 | #ifdef __BORLANDC__ | |
ce4169a4 | 25 | #pragma hdrstop |
c801d85f KB |
26 | #endif |
27 | ||
ce4169a4 RR |
28 | #if wxUSE_FILE |
29 | ||
c801d85f | 30 | // standard |
1c193821 | 31 | #if defined(__WXMSW__) && !defined(__GNUWIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__) |
c801d85f | 32 | #include <io.h> |
30a5be97 | 33 | |
a3ef5bf5 | 34 | #ifndef __SALFORDC__ |
49d5d881 VZ |
35 | #define WIN32_LEAN_AND_MEAN |
36 | #define NOSERVICE | |
37 | #define NOIME | |
38 | #define NOATOM | |
39 | #define NOGDI | |
40 | #define NOGDICAPMASKS | |
41 | #define NOMETAFILE | |
42 | #define NOMINMAX | |
43 | #define NOMSG | |
44 | #define NOOPENFILE | |
45 | #define NORASTEROPS | |
46 | #define NOSCROLL | |
47 | #define NOSOUND | |
48 | #define NOSYSMETRICS | |
49 | #define NOTEXTMETRIC | |
50 | #define NOWH | |
51 | #define NOCOMM | |
52 | #define NOKANJI | |
53 | #define NOCRYPT | |
54 | #define NOMCX | |
a3ef5bf5 JS |
55 | #endif |
56 | ||
1c193821 JS |
57 | #elif defined(__WXMSW__) && defined(__WXWINCE__) |
58 | // TODO: what to include? | |
c801d85f | 59 | #elif (defined(__UNIX__) || defined(__GNUWIN32__)) |
3f4a0c5b | 60 | #include <unistd.h> |
732b8386 | 61 | #include <sys/stat.h> |
3f4a0c5b | 62 | #ifdef __GNUWIN32__ |
9ed0d735 | 63 | #include "wx/msw/wrapwin.h" |
3f4a0c5b | 64 | #endif |
d3b4d710 VS |
65 | #elif defined(__DOS__) |
66 | #if defined(__WATCOMC__) | |
67 | #include <io.h> | |
68 | #elif defined(__DJGPP__) | |
69 | #include <io.h> | |
70 | #include <unistd.h> | |
71 | #include <stdio.h> | |
72 | #else | |
73 | #error "Please specify the header with file functions declarations." | |
74 | #endif | |
c2ff79b1 DW |
75 | #elif (defined(__WXPM__)) |
76 | #include <io.h> | |
34138703 | 77 | #elif (defined(__WXSTUBS__)) |
3f4a0c5b VZ |
78 | // Have to ifdef this for different environments |
79 | #include <io.h> | |
17dff81c | 80 | #elif (defined(__WXMAC__)) |
5b781a67 | 81 | #if __MSL__ < 0x6000 |
3f4a0c5b | 82 | int access( const char *path, int mode ) { return 0 ; } |
5b781a67 SC |
83 | #else |
84 | int _access( const char *path, int mode ) { return 0 ; } | |
85 | #endif | |
3f4a0c5b | 86 | char* mktemp( char * path ) { return path ;} |
5b781a67 | 87 | #include <stat.h> |
5b781a67 | 88 | #include <unistd.h> |
c801d85f | 89 | #else |
3f4a0c5b | 90 | #error "Please specify the header with file functions declarations." |
c801d85f KB |
91 | #endif //Win/UNIX |
92 | ||
93 | #include <stdio.h> // SEEK_xxx constants | |
1c193821 JS |
94 | |
95 | #ifndef __WXWINCE__ | |
c801d85f | 96 | #include <fcntl.h> // O_RDONLY &c |
1c193821 | 97 | #endif |
a3ef5bf5 | 98 | |
1c193821 JS |
99 | #ifdef __WXWINCE__ |
100 | // Nothing | |
101 | #elif !defined(__MWERKS__) | |
31907d03 SC |
102 | #include <sys/types.h> // needed for stat |
103 | #include <sys/stat.h> // stat | |
104 | #elif defined(__MWERKS__) && ( defined(__WXMSW__) || defined(__MACH__) ) | |
de85a884 VZ |
105 | #include <sys/types.h> // needed for stat |
106 | #include <sys/stat.h> // stat | |
469e1e5c | 107 | #endif |
c801d85f | 108 | |
4ea2c29f VZ |
109 | // Windows compilers don't have these constants |
110 | #ifndef W_OK | |
111 | enum | |
112 | { | |
113 | F_OK = 0, // test for existence | |
114 | X_OK = 1, // execute permission | |
115 | W_OK = 2, // write | |
116 | R_OK = 4 // read | |
117 | }; | |
118 | #endif // W_OK | |
34138703 | 119 | |
b12915c1 VZ |
120 | // there is no distinction between text and binary files under Unix, so define |
121 | // O_BINARY as 0 if the system headers don't do it already | |
122 | #if defined(__UNIX__) && !defined(O_BINARY) | |
49d5d881 | 123 | #define O_BINARY (0) |
246037e2 | 124 | #endif //__UNIX__ |
c801d85f | 125 | |
a3ef5bf5 | 126 | #ifdef __SALFORDC__ |
3f4a0c5b | 127 | #include <unix.h> |
a3ef5bf5 JS |
128 | #endif |
129 | ||
81d66cf3 | 130 | #ifndef MAX_PATH |
3f4a0c5b | 131 | #define MAX_PATH 512 |
81d66cf3 | 132 | #endif |
c801d85f | 133 | |
49d5d881 VZ |
134 | // some broken compilers don't have 3rd argument in open() and creat() |
135 | #ifdef __SALFORDC__ | |
136 | #define ACCESS(access) | |
137 | #define stat _stat | |
138 | #else // normal compiler | |
139 | #define ACCESS(access) , (access) | |
140 | #endif // Salford C | |
141 | ||
f6bcfd97 | 142 | // wxWindows |
ade35f11 VZ |
143 | #ifndef WX_PRECOMP |
144 | #include "wx/string.h" | |
145 | #include "wx/intl.h" | |
ade35f11 VZ |
146 | #include "wx/log.h" |
147 | #endif // !WX_PRECOMP | |
148 | ||
149 | #include "wx/filename.h" | |
44d568b6 | 150 | #include "wx/file.h" |
4ea2c29f | 151 | #include "wx/filefn.h" |
f6bcfd97 | 152 | |
28f5082b VS |
153 | #ifdef __WXMSW__ |
154 | #include "wx/msw/mslu.h" | |
155 | #endif | |
156 | ||
1c193821 JS |
157 | #ifdef __WXWINCE__ |
158 | #include "wx/msw/private.h" | |
159 | #endif | |
160 | ||
c801d85f KB |
161 | // ============================================================================ |
162 | // implementation of wxFile | |
163 | // ============================================================================ | |
164 | ||
165 | // ---------------------------------------------------------------------------- | |
166 | // static functions | |
167 | // ---------------------------------------------------------------------------- | |
4ea2c29f | 168 | |
50920146 | 169 | bool wxFile::Exists(const wxChar *name) |
246037e2 | 170 | { |
4ea2c29f | 171 | return wxFileExists(name); |
d1427b70 VZ |
172 | } |
173 | ||
50920146 | 174 | bool wxFile::Access(const wxChar *name, OpenMode mode) |
d1427b70 | 175 | { |
4ea2c29f VZ |
176 | int how; |
177 | ||
178 | switch ( mode ) | |
179 | { | |
180 | default: | |
181 | wxFAIL_MSG(wxT("bad wxFile::Access mode parameter.")); | |
182 | // fall through | |
d1427b70 | 183 | |
49d5d881 VZ |
184 | case read: |
185 | how = R_OK; | |
186 | break; | |
d1427b70 | 187 | |
49d5d881 VZ |
188 | case write: |
189 | how = W_OK; | |
190 | break; | |
d1427b70 | 191 | |
4ea2c29f VZ |
192 | case read_write: |
193 | how = R_OK | W_OK; | |
194 | break; | |
49d5d881 | 195 | } |
d1427b70 | 196 | |
1c193821 JS |
197 | #ifdef __WXWINCE__ |
198 | // FIXME: use CreateFile with 0 access to query the file | |
199 | return TRUE; | |
200 | #else | |
4ea2c29f | 201 | return wxAccess(name, how) == 0; |
1c193821 | 202 | #endif |
c801d85f KB |
203 | } |
204 | ||
205 | // ---------------------------------------------------------------------------- | |
206 | // opening/closing | |
207 | // ---------------------------------------------------------------------------- | |
208 | ||
209 | // ctors | |
50920146 | 210 | wxFile::wxFile(const wxChar *szFileName, OpenMode mode) |
c801d85f | 211 | { |
49d5d881 VZ |
212 | m_fd = fd_invalid; |
213 | m_error = FALSE; | |
c801d85f | 214 | |
49d5d881 | 215 | Open(szFileName, mode); |
c801d85f KB |
216 | } |
217 | ||
c801d85f | 218 | // create the file, fail if it already exists and bOverwrite |
50920146 | 219 | bool wxFile::Create(const wxChar *szFileName, bool bOverwrite, int accessMode) |
c801d85f | 220 | { |
49d5d881 VZ |
221 | // if bOverwrite we create a new file or truncate the existing one, |
222 | // otherwise we only create the new file and fail if it already exists | |
c4e41ce3 | 223 | #if defined(__WXMAC__) && !defined(__UNIX__) && !wxUSE_UNICODE |
92980e90 RR |
224 | // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace |
225 | // int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access); | |
226 | int fd = creat( szFileName , accessMode); | |
1c193821 JS |
227 | #else |
228 | #ifdef __WXWINCE__ | |
229 | HANDLE fileHandle = ::CreateFile(szFileName, GENERIC_WRITE, 0, NULL, | |
230 | bOverwrite ? CREATE_ALWAYS : CREATE_NEW, FILE_ATTRIBUTE_NORMAL, | |
231 | 0); | |
232 | int fd = 0; | |
233 | if (fileHandle == INVALID_HANDLE_VALUE) | |
234 | fd = (int) fileHandle; | |
235 | else | |
236 | fd = -1; | |
7c74e7fe | 237 | #else |
92980e90 RR |
238 | int fd = wxOpen( szFileName, |
239 | O_BINARY | O_WRONLY | O_CREAT | | |
240 | (bOverwrite ? O_TRUNC : O_EXCL) | |
241 | ACCESS(accessMode) ); | |
1c193821 | 242 | #endif |
7c74e7fe | 243 | #endif |
92980e90 RR |
244 | if ( fd == -1 ) |
245 | { | |
49d5d881 VZ |
246 | wxLogSysError(_("can't create file '%s'"), szFileName); |
247 | return FALSE; | |
248 | } | |
92980e90 RR |
249 | else |
250 | { | |
49d5d881 VZ |
251 | Attach(fd); |
252 | return TRUE; | |
253 | } | |
c801d85f KB |
254 | } |
255 | ||
256 | // open the file | |
50920146 | 257 | bool wxFile::Open(const wxChar *szFileName, OpenMode mode, int accessMode) |
c801d85f | 258 | { |
1c193821 JS |
259 | #ifdef __WXWINCE__ |
260 | DWORD access = 0; | |
261 | DWORD shareMode = 0; | |
262 | DWORD disposition = 0; | |
263 | ||
264 | int flags = O_BINARY; | |
265 | ||
266 | switch ( mode ) | |
267 | { | |
268 | case read: | |
269 | access = GENERIC_READ; | |
270 | shareMode = FILE_SHARE_READ|FILE_SHARE_WRITE; | |
271 | disposition = OPEN_EXISTING; | |
272 | break; | |
273 | ||
274 | case write_append: | |
275 | if ( wxFile::Exists(szFileName) ) | |
276 | { | |
277 | access = GENERIC_READ|GENERIC_WRITE; | |
278 | shareMode = FILE_SHARE_READ; | |
279 | disposition = 0; | |
280 | break; | |
281 | } | |
282 | //else: fall through as write_append is the same as write if the | |
283 | // file doesn't exist | |
284 | ||
285 | case write: | |
286 | access = GENERIC_WRITE; | |
287 | shareMode = 0; | |
288 | disposition = TRUNCATE_EXISTING; | |
289 | break; | |
290 | ||
291 | case write_excl: | |
292 | access = GENERIC_WRITE; | |
293 | shareMode = 0; | |
294 | disposition = TRUNCATE_EXISTING; | |
295 | break; | |
296 | ||
297 | case read_write: | |
298 | access = GENERIC_READ|GENERIC_WRITE; | |
299 | shareMode = 0; | |
300 | disposition = 0; | |
301 | break; | |
302 | } | |
303 | ||
304 | int fd = 0; | |
305 | HANDLE fileHandle = ::CreateFile(szFileName, access, shareMode, NULL, | |
306 | disposition, FILE_ATTRIBUTE_NORMAL, 0); | |
307 | if (fileHandle == INVALID_HANDLE_VALUE) | |
308 | fd = -1; | |
309 | else | |
310 | fd = (int) fileHandle; | |
311 | #else | |
49d5d881 | 312 | int flags = O_BINARY; |
c801d85f | 313 | |
92980e90 RR |
314 | switch ( mode ) |
315 | { | |
49d5d881 VZ |
316 | case read: |
317 | flags |= O_RDONLY; | |
318 | break; | |
c801d85f | 319 | |
f6bcfd97 BP |
320 | case write_append: |
321 | if ( wxFile::Exists(szFileName) ) | |
322 | { | |
323 | flags |= O_WRONLY | O_APPEND; | |
324 | break; | |
325 | } | |
326 | //else: fall through as write_append is the same as write if the | |
327 | // file doesn't exist | |
328 | ||
49d5d881 VZ |
329 | case write: |
330 | flags |= O_WRONLY | O_CREAT | O_TRUNC; | |
331 | break; | |
61b02744 | 332 | |
68164137 RL |
333 | case write_excl: |
334 | flags |= O_WRONLY | O_CREAT | O_EXCL; | |
335 | break; | |
336 | ||
49d5d881 VZ |
337 | case read_write: |
338 | flags |= O_RDWR; | |
339 | break; | |
340 | } | |
c801d85f | 341 | |
92980e90 | 342 | int fd = wxOpen( szFileName, flags ACCESS(accessMode)); |
1c193821 | 343 | #endif |
92980e90 RR |
344 | if ( fd == -1 ) |
345 | { | |
49d5d881 VZ |
346 | wxLogSysError(_("can't open file '%s'"), szFileName); |
347 | return FALSE; | |
348 | } | |
349 | else { | |
350 | Attach(fd); | |
351 | return TRUE; | |
352 | } | |
c801d85f KB |
353 | } |
354 | ||
355 | // close | |
61b02744 | 356 | bool wxFile::Close() |
c801d85f | 357 | { |
49d5d881 | 358 | if ( IsOpened() ) { |
1c193821 JS |
359 | #ifdef __WXWINCE__ |
360 | if (!CloseHandle((HANDLE) m_fd)) | |
361 | #else | |
362 | if ( close(m_fd) == -1 ) | |
363 | #endif | |
364 | { | |
49d5d881 VZ |
365 | wxLogSysError(_("can't close file descriptor %d"), m_fd); |
366 | m_fd = fd_invalid; | |
367 | return FALSE; | |
368 | } | |
369 | else | |
370 | m_fd = fd_invalid; | |
61b02744 | 371 | } |
61b02744 | 372 | |
49d5d881 | 373 | return TRUE; |
c801d85f KB |
374 | } |
375 | ||
376 | // ---------------------------------------------------------------------------- | |
377 | // read/write | |
378 | // ---------------------------------------------------------------------------- | |
379 | ||
380 | // read | |
381 | off_t wxFile::Read(void *pBuf, off_t nCount) | |
382 | { | |
49d5d881 | 383 | wxCHECK( (pBuf != NULL) && IsOpened(), 0 ); |
c801d85f | 384 | |
1c193821 JS |
385 | #ifdef __WXWINCE__ |
386 | DWORD bytesRead = 0; | |
387 | int iRc = 0; | |
388 | if (ReadFile((HANDLE) m_fd, pBuf, (DWORD) nCount, & bytesRead, NULL)) | |
389 | iRc = bytesRead; | |
390 | else | |
391 | iRc = -1; | |
392 | #elif defined(__MWERKS__) | |
49d5d881 | 393 | int iRc = ::read(m_fd, (char*) pBuf, nCount); |
469e1e5c | 394 | #else |
49d5d881 | 395 | int iRc = ::read(m_fd, pBuf, nCount); |
469e1e5c | 396 | #endif |
49d5d881 VZ |
397 | if ( iRc == -1 ) { |
398 | wxLogSysError(_("can't read from file descriptor %d"), m_fd); | |
399 | return wxInvalidOffset; | |
400 | } | |
401 | else | |
402 | return (size_t)iRc; | |
c801d85f KB |
403 | } |
404 | ||
405 | // write | |
c86f1403 | 406 | size_t wxFile::Write(const void *pBuf, size_t nCount) |
c801d85f | 407 | { |
49d5d881 | 408 | wxCHECK( (pBuf != NULL) && IsOpened(), 0 ); |
c801d85f | 409 | |
1c193821 JS |
410 | #ifdef __WXWINCE__ |
411 | DWORD bytesRead = 0; | |
412 | int iRc = 0; | |
413 | if (WriteFile((HANDLE) m_fd, pBuf, (DWORD) nCount, & bytesRead, NULL)) | |
414 | iRc = bytesRead; | |
415 | else | |
416 | iRc = -1; | |
417 | #elif defined(__MWERKS__) | |
5b781a67 SC |
418 | #if __MSL__ >= 0x6000 |
419 | int iRc = ::write(m_fd, (void*) pBuf, nCount); | |
420 | #else | |
49d5d881 | 421 | int iRc = ::write(m_fd, (const char*) pBuf, nCount); |
5b781a67 | 422 | #endif |
469e1e5c | 423 | #else |
49d5d881 | 424 | int iRc = ::write(m_fd, pBuf, nCount); |
469e1e5c | 425 | #endif |
49d5d881 VZ |
426 | if ( iRc == -1 ) { |
427 | wxLogSysError(_("can't write to file descriptor %d"), m_fd); | |
428 | m_error = TRUE; | |
429 | return 0; | |
430 | } | |
431 | else | |
432 | return iRc; | |
c801d85f KB |
433 | } |
434 | ||
435 | // flush | |
436 | bool wxFile::Flush() | |
437 | { | |
49d5d881 | 438 | if ( IsOpened() ) { |
1c193821 JS |
439 | #ifdef __WXWINCE__ |
440 | // Do nothing | |
441 | #elif defined(__VISUALC__) || wxHAVE_FSYNC | |
f6bcfd97 | 442 | if ( wxFsync(m_fd) == -1 ) |
09914df7 VZ |
443 | { |
444 | wxLogSysError(_("can't flush file descriptor %d"), m_fd); | |
445 | return FALSE; | |
446 | } | |
49d5d881 | 447 | #else // no fsync |
09914df7 | 448 | // just do nothing |
49d5d881 VZ |
449 | #endif // fsync |
450 | } | |
c801d85f | 451 | |
49d5d881 | 452 | return TRUE; |
c801d85f KB |
453 | } |
454 | ||
455 | // ---------------------------------------------------------------------------- | |
456 | // seek | |
457 | // ---------------------------------------------------------------------------- | |
458 | ||
459 | // seek | |
79c3e0e1 | 460 | off_t wxFile::Seek(off_t ofs, wxSeekMode mode) |
c801d85f | 461 | { |
49d5d881 VZ |
462 | wxASSERT( IsOpened() ); |
463 | ||
1c193821 JS |
464 | #ifdef __WXWINCE__ |
465 | int origin; | |
466 | switch ( mode ) { | |
467 | default: | |
468 | wxFAIL_MSG(_("unknown seek origin")); | |
469 | ||
470 | case wxFromStart: | |
471 | origin = FILE_BEGIN; | |
472 | break; | |
473 | ||
474 | case wxFromCurrent: | |
475 | origin = FILE_CURRENT; | |
476 | break; | |
477 | ||
478 | case wxFromEnd: | |
479 | origin = FILE_END; | |
480 | break; | |
481 | } | |
482 | ||
483 | DWORD res = SetFilePointer((HANDLE) m_fd, ofs, 0, origin) ; | |
484 | if (res == 0xFFFFFFFF && GetLastError() != NO_ERROR) | |
485 | { | |
486 | wxLogSysError(_("can't seek on file descriptor %d"), m_fd); | |
487 | return wxInvalidOffset; | |
488 | } | |
489 | else | |
490 | return (off_t)res; | |
491 | #else | |
a1b82138 | 492 | int origin; |
49d5d881 | 493 | switch ( mode ) { |
a1b82138 VZ |
494 | default: |
495 | wxFAIL_MSG(_("unknown seek origin")); | |
496 | ||
49d5d881 | 497 | case wxFromStart: |
a1b82138 | 498 | origin = SEEK_SET; |
49d5d881 VZ |
499 | break; |
500 | ||
501 | case wxFromCurrent: | |
a1b82138 | 502 | origin = SEEK_CUR; |
49d5d881 VZ |
503 | break; |
504 | ||
505 | case wxFromEnd: | |
a1b82138 | 506 | origin = SEEK_END; |
49d5d881 | 507 | break; |
49d5d881 VZ |
508 | } |
509 | ||
a1b82138 | 510 | int iRc = lseek(m_fd, ofs, origin); |
49d5d881 VZ |
511 | if ( iRc == -1 ) { |
512 | wxLogSysError(_("can't seek on file descriptor %d"), m_fd); | |
513 | return wxInvalidOffset; | |
514 | } | |
515 | else | |
516 | return (off_t)iRc; | |
1c193821 | 517 | #endif |
c801d85f KB |
518 | } |
519 | ||
520 | // get current off_t | |
521 | off_t wxFile::Tell() const | |
522 | { | |
49d5d881 VZ |
523 | wxASSERT( IsOpened() ); |
524 | ||
1c193821 JS |
525 | #ifdef __WXWINCE__ |
526 | DWORD res = SetFilePointer((HANDLE) m_fd, 0, 0, FILE_CURRENT) ; | |
527 | if (res == 0xFFFFFFFF && GetLastError() != NO_ERROR) | |
528 | { | |
529 | wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd); | |
530 | return wxInvalidOffset; | |
531 | } | |
532 | else | |
533 | return (off_t)res; | |
534 | #else | |
f6bcfd97 | 535 | int iRc = wxTell(m_fd); |
49d5d881 VZ |
536 | if ( iRc == -1 ) { |
537 | wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd); | |
538 | return wxInvalidOffset; | |
539 | } | |
540 | else | |
541 | return (off_t)iRc; | |
1c193821 | 542 | #endif |
c801d85f KB |
543 | } |
544 | ||
545 | // get current file length | |
546 | off_t wxFile::Length() const | |
547 | { | |
49d5d881 | 548 | wxASSERT( IsOpened() ); |
c801d85f | 549 | |
1c193821 JS |
550 | #ifdef __WXWINCE__ |
551 | DWORD off0 = SetFilePointer((HANDLE) m_fd, 0, 0, FILE_CURRENT); | |
552 | DWORD off1 = SetFilePointer((HANDLE) m_fd, 0, 0, FILE_END); | |
553 | off_t len = off1; | |
554 | ||
555 | // Restore position | |
556 | SetFilePointer((HANDLE) m_fd, off0, 0, FILE_BEGIN); | |
557 | return len; | |
558 | #else | |
49d5d881 | 559 | #ifdef __VISUALC__ |
c801d85f | 560 | int iRc = _filelength(m_fd); |
49d5d881 | 561 | #else // !VC++ |
f6bcfd97 | 562 | int iRc = wxTell(m_fd); |
c801d85f | 563 | if ( iRc != -1 ) { |
49d5d881 VZ |
564 | // @ have to use const_cast :-( |
565 | int iLen = ((wxFile *)this)->SeekEnd(); | |
566 | if ( iLen != -1 ) { | |
567 | // restore old position | |
568 | if ( ((wxFile *)this)->Seek(iRc) == -1 ) { | |
569 | // error | |
570 | iLen = -1; | |
571 | } | |
c801d85f | 572 | } |
c801d85f | 573 | |
49d5d881 VZ |
574 | iRc = iLen; |
575 | } | |
576 | #endif // VC++ | |
577 | ||
578 | if ( iRc == -1 ) { | |
579 | wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd); | |
580 | return wxInvalidOffset; | |
c801d85f | 581 | } |
49d5d881 VZ |
582 | else |
583 | return (off_t)iRc; | |
1c193821 | 584 | #endif |
c801d85f KB |
585 | } |
586 | ||
587 | // is end of file reached? | |
588 | bool wxFile::Eof() const | |
589 | { | |
49d5d881 | 590 | wxASSERT( IsOpened() ); |
c801d85f | 591 | |
1c193821 JS |
592 | #ifdef __WXWINCE__ |
593 | DWORD off0 = SetFilePointer((HANDLE) m_fd, 0, 0, FILE_CURRENT); | |
594 | DWORD off1 = SetFilePointer((HANDLE) m_fd, 0, 0, FILE_END); | |
595 | if (off0 == off1) | |
596 | return TRUE; | |
597 | else | |
598 | { | |
599 | SetFilePointer((HANDLE) m_fd, off0, 0, FILE_BEGIN); | |
600 | return FALSE; | |
601 | } | |
602 | #else | |
49d5d881 | 603 | int iRc; |
61b02744 | 604 | |
d3b4d710 | 605 | #if defined(__DOS__) || defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__) |
61b02744 VZ |
606 | // @@ this doesn't work, of course, on unseekable file descriptors |
607 | off_t ofsCur = Tell(), | |
49d5d881 | 608 | ofsMax = Length(); |
1678ad78 | 609 | if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset ) |
49d5d881 | 610 | iRc = -1; |
61b02744 | 611 | else |
49d5d881 VZ |
612 | iRc = ofsCur == ofsMax; |
613 | #else // Windows and "native" compiler | |
61b02744 | 614 | iRc = eof(m_fd); |
49d5d881 | 615 | #endif // Windows/Unix |
c801d85f | 616 | |
49d5d881 VZ |
617 | switch ( iRc ) { |
618 | case 1: | |
619 | break; | |
c801d85f | 620 | |
49d5d881 VZ |
621 | case 0: |
622 | return FALSE; | |
c801d85f | 623 | |
49d5d881 | 624 | case -1: |
8e3f1261 | 625 | wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd); |
49d5d881 | 626 | break; |
c801d85f | 627 | |
49d5d881 VZ |
628 | default: |
629 | wxFAIL_MSG(_("invalid eof() return value.")); | |
630 | } | |
c801d85f | 631 | |
49d5d881 | 632 | return TRUE; |
1c193821 | 633 | #endif |
c801d85f KB |
634 | } |
635 | ||
636 | // ============================================================================ | |
637 | // implementation of wxTempFile | |
638 | // ============================================================================ | |
639 | ||
640 | // ---------------------------------------------------------------------------- | |
641 | // construction | |
642 | // ---------------------------------------------------------------------------- | |
44b62d54 | 643 | |
c801d85f KB |
644 | wxTempFile::wxTempFile(const wxString& strName) |
645 | { | |
49d5d881 | 646 | Open(strName); |
c801d85f KB |
647 | } |
648 | ||
649 | bool wxTempFile::Open(const wxString& strName) | |
650 | { | |
44b62d54 VZ |
651 | // we must have an absolute filename because otherwise CreateTempFileName() |
652 | // would create the temp file in $TMP (i.e. the system standard location | |
653 | // for the temp files) which might be on another volume/drive/mount and | |
654 | // wxRename()ing it later to m_strName from Commit() would then fail | |
655 | // | |
656 | // with the absolute filename, the temp file is created in the same | |
657 | // directory as this one which ensures that wxRename() may work later | |
658 | wxFileName fn(strName); | |
659 | if ( !fn.IsAbsolute() ) | |
660 | { | |
661 | fn.Normalize(wxPATH_NORM_ABSOLUTE); | |
662 | } | |
663 | ||
664 | m_strName = fn.GetFullPath(); | |
246037e2 | 665 | |
44b62d54 | 666 | m_strTemp = wxFileName::CreateTempFileName(m_strName, &m_file); |
ade35f11 VZ |
667 | |
668 | if ( m_strTemp.empty() ) | |
669 | { | |
670 | // CreateTempFileName() failed | |
671 | return FALSE; | |
672 | } | |
49d5d881 | 673 | |
49d5d881 | 674 | #ifdef __UNIX__ |
ade35f11 VZ |
675 | // the temp file should have the same permissions as the original one |
676 | mode_t mode; | |
ca11abde | 677 | |
f6bcfd97 | 678 | wxStructStat st; |
ca11abde | 679 | if ( stat( (const char*) m_strName.fn_str(), &st) == 0 ) |
49d5d881 | 680 | { |
ade35f11 | 681 | mode = st.st_mode; |
49d5d881 VZ |
682 | } |
683 | else | |
684 | { | |
ade35f11 | 685 | // file probably didn't exist, just give it the default mode _using_ |
68164137 | 686 | // user's umask (new files creation should respect umask) |
ade35f11 VZ |
687 | mode_t mask = umask(0777); |
688 | mode = 0666 & ~mask; | |
689 | umask(mask); | |
49d5d881 | 690 | } |
49d5d881 | 691 | |
ca11abde | 692 | if ( chmod( (const char*) m_strTemp.fn_str(), mode) == -1 ) |
fe99e285 | 693 | { |
ade35f11 | 694 | wxLogSysError(_("Failed to set temporary file permissions")); |
fe99e285 | 695 | } |
49d5d881 | 696 | #endif // Unix |
246037e2 | 697 | |
ade35f11 | 698 | return TRUE; |
c801d85f KB |
699 | } |
700 | ||
701 | // ---------------------------------------------------------------------------- | |
702 | // destruction | |
703 | // ---------------------------------------------------------------------------- | |
704 | ||
705 | wxTempFile::~wxTempFile() | |
706 | { | |
49d5d881 VZ |
707 | if ( IsOpened() ) |
708 | Discard(); | |
c801d85f KB |
709 | } |
710 | ||
711 | bool wxTempFile::Commit() | |
712 | { | |
49d5d881 | 713 | m_file.Close(); |
c801d85f | 714 | |
f6bcfd97 | 715 | if ( wxFile::Exists(m_strName) && wxRemove(m_strName) != 0 ) { |
49d5d881 VZ |
716 | wxLogSysError(_("can't remove file '%s'"), m_strName.c_str()); |
717 | return FALSE; | |
718 | } | |
c801d85f | 719 | |
f35746ce | 720 | if ( !wxRenameFile(m_strTemp, m_strName) ) { |
49d5d881 VZ |
721 | wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str()); |
722 | return FALSE; | |
723 | } | |
c801d85f | 724 | |
49d5d881 | 725 | return TRUE; |
c801d85f KB |
726 | } |
727 | ||
728 | void wxTempFile::Discard() | |
729 | { | |
49d5d881 | 730 | m_file.Close(); |
f6bcfd97 | 731 | if ( wxRemove(m_strTemp) != 0 ) |
49d5d881 | 732 | wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str()); |
c801d85f | 733 | } |
ce4169a4 | 734 | |
ade35f11 | 735 | #endif // wxUSE_FILE |
cc985fac | 736 |