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