First public contribution.
2 * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
5 * Silicon Graphics Computer Systems, Inc.
10 * This material is provided "as is", with absolutely no warranty expressed
11 * or implied. Any use is at your own risk.
13 * Permission to use or copy this software for any purpose is hereby granted
14 * without fee, provided the above notices are retained on all copies.
15 * Permission to modify the code and to distribute modified code is granted,
16 * provided the above notices are retained, and a notice that the code was
17 * modified is included with the above copyright notice.
22 #include "stlport_prefix.h"
24 #if defined (__SUNPPRO_CC) && !defined (_STLP_NO_NEW_C_HEADERS)
26 // For sunpro, it chokes if time.h is included through stat.h
32 # define __int64 long long
35 #if defined (_STLP_USE_UNIX_IO)
37 // open/close/read/write
38 # include <sys/stat.h> // For stat
39 # if !defined (_CRAY) && ! defined (__EMX__)
40 # include <sys/mman.h> // For mmap
43 // on HP-UX 11, this one contradicts with pthread.h on pthread_atfork, unless we unset this
44 # if defined (__hpux) && defined (__GNUC__)
45 # undef _INCLUDE_POSIX1C_SOURCE
52 # include <sys/sysctl.h>
54 #elif defined (_STLP_USE_WIN32_IO)
55 # define WIN32_LEAN_AND_MEAN
59 # include <cfcntl.h> // For _O_RDONLY, etc
60 # include <sys/stat.h> // For _fstat
61 # elif !defined(_STLP_WCE)
62 # include <io.h> // For _get_osfhandle
63 # include <fcntl.h> // For _O_RDONLY, etc
64 # include <sys/stat.h> // For _fstat
66 # define _TEXTBUF_SIZE 0x1000
67 #elif defined (_STLP_USE_UNIX_EMULATION_IO)
68 # if defined( __MSL__ )
74 # include <sys/stat.h>
75 #elif defined (_STLP_USE_STDIO_IO)
77 # if !(defined(__MRC__) || defined(__SC__) || defined(__ISCPP__) )
79 # include <sys/stat.h>
82 # if defined( __MSL__ )
85 # if defined(__ISCPP__)
86 # include <c_locale_is/filestat.h>
88 # if (defined(__BEOS__) && defined(__INTEL__)) || defined (__SYMBIAN32__)
90 # include <sys/stat.h> // For _fstat
91 # define _S_IREAD S_IREAD
92 # define _S_IWRITE S_IWRITE
93 # define _S_IFREG S_IFREG
96 # error "Configure i/o !"
99 #if defined (_STLP_USE_WIN32_IO)
100 const _STLP_fd INVALID_STLP_FD = INVALID_HANDLE_VALUE;
101 # if !defined (INVALID_SET_FILE_POINTER)
102 # define INVALID_SET_FILE_POINTER 0xffffffff
104 #elif defined (_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO) || defined (_STLP_USE_UNIX_IO)
105 const _STLP_fd INVALID_STLP_FD = -1;
107 # error "Configure i/o !"
110 // map permission masks
111 #if defined (_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO)
113 # define S_IRUSR _S_IREAD
114 # define S_IWUSR _S_IWRITE
115 # define S_IRGRP _S_IREAD
116 # define S_IWGRP _S_IWRITE
117 # define S_IROTH _S_IREAD
118 # define S_IWOTH _S_IWRITE
121 # define O_RDONLY _O_RDONLY
122 # define O_WRONLY _O_WRONLY
123 # define O_RDWR _O_RDWR
124 # define O_APPEND _O_APPEND
125 # define O_CREAT _O_CREAT
126 # define O_TRUNC _O_TRUNC
127 # define O_TEXT _O_TEXT
128 # define O_BINARY _O_BINARY
133 # if !defined( O_TEXT )
134 # define O_TEXT _O_TEXT
136 # define _S_IFREG S_IFREG
137 # define S_IREAD S_IRUSR
138 # define S_IWRITE S_IWUSR
139 # define S_IEXEC S_IXUSR
140 # define _S_IWRITE S_IWRITE
141 # define _S_IREAD S_IREAD
143 # define _lseek lseek
144 # define _close close
146 # define _write write
151 # define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
154 #include "fstream_impl.h"
156 #ifdef _STLP_LONG_LONG
157 # define ULL(x) ((unsigned _STLP_LONG_LONG)x)
158 #elif defined(__MRC__) || defined(__SC__) //*TY 02/25/2000 - added support for MPW compilers
160 # define ULL(x) (U64SetU(x))
161 #elif defined(__ISCPP__)
163 #elif defined(__SYMBIAN32__)
164 # include <sys/types.h>
166 # error "there should be some long long type on the system!"
170 #if defined(__SYMBIAN32__WSD__)
171 # include "libstdcppwsd.h"
173 _STLP_DECLSPEC size_t& get_fstream_Filebuf_Base_GetPageSize()
175 return get_libcpp_wsd().fstream_Filebuf_base_M_page_size;
178 void filebuf_page_size_init()
180 get_fstream_Filebuf_Base_GetPageSize() = 4096;
184 _STLP_BEGIN_NAMESPACE
185 // Compare with streamoff definition in stl/char_traits.h!
187 #ifdef _STLP_USE_DEFAULT_FILE_OFFSET
196 #elif defined(_LARGEFILE_SOURCE) || defined(_LARGEFILE64_SOURCE) /* || defined(__USE_FILE_OFFSET64) */ \
197 /* || (defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64)) */ /* || defined(__sgi) */
198 # define FOPEN fopen64
199 # define FSEEK fseeko64
200 # define FSTAT fstat64
202 # define FTELL ftello64
203 # define LSEEK lseek64
216 #ifdef _STLP_USE_UNIX_IO
217 # ifndef MAP_FAILED /* MMAP failure return code */
218 # define MAP_FAILED -1
220 #elif defined (_STLP_USE_UNIX_EMULATION_IO)
221 # define LSEEK _lseek
224 #if !defined(__MSL__) && !defined(__MRC__) && !defined(__SC__) && !defined(_STLP_WCE) //*TY 04/15/2000 - exclude mpw compilers also
225 static ios_base::openmode flag_to_openmode(int mode) {
226 ios_base::openmode ret = ios_base::__default_mode;
228 switch(mode & O_ACCMODE) {
230 ret = ios_base::in; break;
232 ret = ios_base::out; break;
234 ret = ios_base::in | ios_base::out; break;
238 ret |= ios_base::app;
240 # if defined (_STLP_USE_WIN32_IO)
242 ret |= ios_base::binary;
249 _STLP_MOVE_TO_PRIV_NAMESPACE
251 // Helper functions for _Filebuf_base.
253 bool __is_regular_file(_STLP_fd fd) {
255 #if defined (_STLP_UNIX)
258 return FSTAT(fd, &buf) == 0 && S_ISREG(buf.st_mode);
260 #elif defined(__MRC__) || defined(__SC__) //*TY 02/25/2000 - added support for MPW compilers
263 return true; // each file is a regular file under mac os, isn't it? (we don't have fstat())
265 #elif defined(_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO)
268 return FSTAT(fd, &buf) == 0 && (buf.st_mode & _S_IFREG) != 0 ;
270 #elif defined (_STLP_USE_WIN32_IO) && !defined(_STLP_WCE)
272 return (GetFileType(fd) & ~FILE_TYPE_REMOTE) == FILE_TYPE_DISK;
275 (void)fd; // dwa 4/27/00 - suppress unused parameter warning
280 // Number of characters in the file.
281 streamoff __file_size(_STLP_fd fd) {
284 #if defined (_STLP_UNIX)
287 if (FSTAT(fd, &buf) == 0 && S_ISREG(buf.st_mode))
288 ret = buf.st_size > 0 ? buf.st_size : 0;
290 #elif defined(__MRC__) || defined(__SC__) //*TY 02/25/2000 - added support for MPW compilers
294 #elif defined(_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO)
297 if (FSTAT(fd, &buf) == 0 && (buf.st_mode & _S_IFREG) != 0)
298 ret = buf.st_size > 0 ? buf.st_size : 0;
300 #elif defined (_STLP_USE_WIN32_IO)
303 li.LowPart = GetFileSize(fd, (unsigned long*) &li.HighPart);
304 if (li.LowPart != INVALID_FILE_SIZE || GetLastError() == NO_ERROR)
308 (void)fd; // dwa 4/27/00 - suppress unused parameter warning
313 _STLP_MOVE_TO_STD_NAMESPACE
315 // Visual C++ and Intel use this, but not Metrowerks
316 // Also MinGW, msvcrt.dll (but not crtdll.dll) dependent version
317 #if (!defined (__MSL__) && !defined (_STLP_WCE) && defined (_STLP_MSVC_LIB) && defined (_WIN32)) || \
318 (defined (__MINGW32__) && defined (__MSVCRT__) && !(defined (__SYMBIAN32__) && defined(__GCCXML__)) )
320 // fcntl(fileno, F_GETFL) for Microsoft library
321 // 'semi-documented' defines:
322 # define IOINFO_L2E 5
323 # define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
324 # define _pioinfo(i) ( __pioinfo[(i) >> IOINFO_L2E] + \
325 ((i) & (IOINFO_ARRAY_ELTS - 1)) )
326 # define FAPPEND 0x20 // O_APPEND flag
327 # define FTEXT 0x80 // O_TEXT flag
328 // end of 'semi-documented' defines
330 // 'semi-documented' internal structure
333 long osfhnd; // the real os HANDLE
334 char osfile; // file handle flags
335 char pipech; // pipe buffer
337 // multi-threaded locking
339 CRITICAL_SECTION lock;
342 # if defined (__MINGW32__)
343 __MINGW_IMPORT ioinfo * __pioinfo[];
345 extern _CRTIMP ioinfo * __pioinfo[];
348 // end of 'semi-documented' declarations
350 static ios_base::openmode _get_osfflags(int fd, HANDLE oshandle) {
353 dosflags = _pioinfo(fd)->osfile;
355 //the file will be considered as open in binary mode with no append attribute
356 // end of 'semi-documented' stuff
359 if (dosflags & FAPPEND)
362 if (dosflags & FTEXT)
367 // For Read/Write access we have to guess
369 BOOL writeOk = WriteFile(oshandle, &dummy2, 0, &dummy, 0);
370 BOOL readOk = ReadFile(oshandle, &dummy2, 0, &dummy, NULL);
371 if (writeOk && readOk)
378 return flag_to_openmode(mode);
381 #elif defined (__DMC__)
383 # define FHND_APPEND 0x04
384 # define FHND_DEVICE 0x08
385 # define FHND_TEXT 0x10
387 extern "C" unsigned char __fhnd_info[_NFILE];
389 static ios_base::openmode _get_osfflags(int fd, HANDLE oshandle) {
392 if (__fhnd_info[fd] & FHND_APPEND)
395 if (__fhnd_info[fd] & FHND_TEXT == 0)
398 for (FILE *fp = &_iob[0]; fp < &_iob[_NFILE]; fp++) {
399 if ((fileno(fp) == fd) && (fp->_flag & (_IOREAD | _IOWRT | _IORW))) {
400 const int osflags = fp->_flag;
402 if ((osflags & _IOREAD) && !(osflags & _IOWRT) && !(osflags & _IORW))
404 else if ((osflags & _IOWRT) && !(osflags & _IOREAD) && !(osflags & _IORW))
412 return flag_to_openmode(mode);
416 #if !defined(__SYMBIAN32__WSD__)
417 size_t _Filebuf_base::_M_page_size = 4096;
418 #endif //__SYMBIAN32__WSD__
420 _STLP_DECLSPEC _Filebuf_base::_Filebuf_base()
421 : _M_file_id(INVALID_STLP_FD),
422 #if defined (_STLP_USE_WIN32_IO)
427 _M_should_close(false)
430 void _Filebuf_base::_S_initialize() {
431 #if defined (_STLP_UNIX) && !defined (__DJGPP) && !defined (_CRAY)
432 # if defined (__APPLE__)
434 size_t pagesize, len;
436 mib[1] = HW_PAGESIZE;
437 len = sizeof(pagesize);
438 sysctl(mib, 2, &pagesize, &len, NULL, 0);
439 _M_page_size = pagesize;
440 # elif defined (__DJGPP) && defined (_CRAY)
441 _M_page_size = BUFSIZ;
443 _M_page_size = sysconf(_SC_PAGESIZE);
445 #elif defined (_STLP_USE_WIN32_IO)
446 SYSTEM_INFO SystemInfo;
447 GetSystemInfo(&SystemInfo);
448 _M_page_size = SystemInfo.dwPageSize;
449 // might be .dwAllocationGranularity
453 // Return the size of the file. This is a wrapper for stat.
454 // Returns zero if the size cannot be determined or is ill-defined.
455 _STLP_DECLSPEC streamoff _Filebuf_base::_M_file_size() {
456 return _STLP_PRIV __file_size(_M_file_id);
459 _STLP_DECLSPEC bool _Filebuf_base::_M_open(const char* name, ios_base::openmode openmode,
466 #if defined (_STLP_USE_UNIX_IO) || defined (_STLP_USE_UNIX_EMULATION_IO)
469 // Unix makes no distinction between text and binary files.
470 switch(openmode & (~ios_base::ate & ~ios_base::binary)) {
472 case ios_base::out | ios_base::trunc:
473 flags = O_WRONLY | O_CREAT | O_TRUNC;
475 case ios_base::out | ios_base::app:
476 flags = O_WRONLY | O_CREAT | O_APPEND;
480 permission = 0; // Irrelevant unless we're writing.
482 case ios_base::in | ios_base::out:
485 case ios_base::in | ios_base::out | ios_base::trunc:
486 flags = O_RDWR | O_CREAT | O_TRUNC;
488 default: // The above are the only combinations of
489 return false; // flags allowed by the C++ standard.
492 # if defined (_STLP_USE_UNIX_EMULATION_IO)
493 if (openmode & ios_base::binary)
498 file_no = _open(name, flags, permission);
500 file_no = OPEN(name, flags, permission);
501 # endif /* _STLP_USE_UNIX_EMULATION_IO */
508 if (openmode & ios_base::ate)
509 if (LSEEK(file_no, 0, SEEK_END) == -1)
512 #elif defined (_STLP_USE_STDIO_IO)
513 // use FILE-based i/o
516 switch(openmode & (~ios_base::ate)) {
518 case ios_base::out | ios_base::trunc:
522 case ios_base::out | ios_base::binary:
523 case ios_base::out | ios_base::trunc | ios_base::binary:
527 case ios_base::out | ios_base::app:
531 case ios_base::out | ios_base::app | ios_base::binary:
539 case ios_base::in | ios_base::binary:
543 case ios_base::in | ios_base::out:
547 case ios_base::in | ios_base::out | ios_base::binary:
552 case ios_base::in | ios_base::out | ios_base::trunc:
556 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
560 default: // The above are the only combinations of
561 return false; // flags allowed by the C++ standard.
564 // fbp : : set permissions !
565 (void)permission; // currently unused //*TY 02/26/2000 - added to suppress warning message
566 _M_file = FOPEN(name, flags);
569 file_no = fileno(_M_file);
574 // unset buffering immediately
579 if (openmode & ios_base::ate) {
580 if (FSEEK(_M_file, 0, SEEK_END) == -1)
584 #elif defined (_STLP_USE_WIN32_IO)
585 DWORD dwDesiredAccess, dwCreationDisposition;
586 bool doTruncate = false;
588 switch (openmode & (~ios_base::ate & ~ios_base::binary)) {
590 case ios_base::out | ios_base::trunc:
591 dwDesiredAccess = GENERIC_WRITE;
592 dwCreationDisposition = OPEN_ALWAYS;
593 // boris : even though it is very non-intuitive, standard
594 // requires them both to behave same.
597 case ios_base::out | ios_base::app:
598 dwDesiredAccess = GENERIC_WRITE;
599 dwCreationDisposition = OPEN_ALWAYS;
602 dwDesiredAccess = GENERIC_READ;
603 dwCreationDisposition = OPEN_EXISTING;
604 permission = 0; // Irrelevant unless we're writing.
606 case ios_base::in | ios_base::out:
607 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
608 dwCreationDisposition = OPEN_EXISTING;
610 case ios_base::in | ios_base::out | ios_base::trunc:
611 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
612 dwCreationDisposition = OPEN_ALWAYS;
615 default: // The above are the only combinations of
616 return false; // flags allowed by the C++ standard.
619 DWORD dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
621 # if defined(_STLP_USE_WIDE_INTERFACE)
622 file_no = CreateFile (_STLP_PRIV __ASCIIToWide(name).c_str(),
624 file_no = CreateFileA(name,
626 dwDesiredAccess, dwShareMode, 0,
627 dwCreationDisposition, permission, 0);
629 if (file_no == INVALID_STLP_FD)
632 if ((doTruncate && SetEndOfFile(file_no) == 0) ||
633 (((openmode & ios_base::ate) != 0) &&
634 (SetFilePointer(file_no, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER))) {
635 CloseHandle(file_no);
645 _M_file_id = file_no;
646 _M_should_close = _M_is_open;
647 _M_openmode = openmode;
650 _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
652 return (_M_is_open != 0);
656 _STLP_DECLSPEC bool _Filebuf_base::_M_open(const char* name, ios_base::openmode openmode) {
657 // This doesn't really grant everyone in the world read/write
658 // access. On Unix, file-creation system calls always clear
659 // bits that are set in the umask from the permissions flag.
660 #ifdef _STLP_USE_WIN32_IO
661 return this->_M_open(name, openmode, FILE_ATTRIBUTE_NORMAL);
662 #elif defined(__MRC__) || defined(__SC__) //*TY 02/26/2000 - added support for MPW compilers
663 return this->_M_open(name, openmode, 0);
665 return this->_M_open(name, openmode, S_IRUSR | S_IWUSR | S_IRGRP |
666 S_IWGRP | S_IROTH | S_IWOTH);
671 #if defined (_STLP_USE_WIN32_IO)
672 bool _Filebuf_base::_M_open(_STLP_fd __id, ios_base::openmode init_mode) {
673 # if (defined (_STLP_MSVC_LIB) && !defined (_STLP_WCE)) || \
674 (defined (__MINGW32__) && defined (__MSVCRT__)) || defined (__DMC__)
676 if (_M_is_open || __id == INVALID_STLP_FD)
679 if (init_mode != ios_base::__default_mode)
680 _M_openmode = init_mode;
682 _M_openmode = _get_osfflags(-1, __id);
686 _M_should_close = false;
687 _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
692 (void)init_mode; // dwa 4/27/00 - suppress unused parameter warning
694 // not available for the API
699 #endif /* _STLP_USE_WIN32_IO */
701 // Associated the filebuf with a file descriptor pointing to an already-
702 // open file. Mode is set to be consistent with the way that the file
704 _STLP_DECLSPEC bool _Filebuf_base::_M_open(int file_no, ios_base::openmode init_mode) {
705 if (_M_is_open || file_no < 0)
708 #if defined (_STLP_UNIX)
709 (void)init_mode; // dwa 4/27/00 - suppress unused parameter warning
711 mode = fcntl(file_no, F_GETFL);
716 _M_openmode = flag_to_openmode(mode);
717 _M_file_id = file_no;
718 #elif defined(__MRC__) || defined(__SC__) //*TY 02/26/2000 - added support for MPW compilers
719 (void)init_mode; // dwa 4/27/00 - suppress unused parameter warning
720 switch (_iob[file_no]._flag & (_IOREAD|_IOWRT|_IORW) )
723 _M_openmode = ios_base::in; break;
725 _M_openmode = ios_base::out; break;
727 _M_openmode = ios_base::in | ios_base::out; break;
731 _M_file_id = file_no;
732 #elif defined (_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO)
733 (void)init_mode; // dwa 4/27/00 - suppress unused parameter warning
736 if (FSTAT(file_no, &buf) != 0)
740 switch(mode & (_S_IWRITE | _S_IREAD) ) {
742 _M_openmode = ios_base::in; break;
744 _M_openmode = ios_base::out; break;
745 case (_S_IWRITE | _S_IREAD):
746 _M_openmode = ios_base::in | ios_base::out; break;
750 _M_file_id = file_no;
751 #elif (defined (_STLP_USE_WIN32_IO) && defined (_STLP_MSVC_LIB) && !defined (_STLP_WCE) ) || \
752 (defined (__MINGW32__) && defined (__MSVCRT__)) || \
755 HANDLE oshandle = (HANDLE)_get_osfhandle(file_no);
756 if (oshandle == INVALID_STLP_FD)
759 if (init_mode != ios_base::__default_mode)
760 _M_openmode = init_mode;
762 _M_openmode = _get_osfflags(file_no, oshandle);
764 _M_file_id = oshandle;
766 (void)init_mode; // dwa 4/27/00 - suppress unused parameter warning
767 // not available for the API
768 # define _STLP_NO_IMPLEMENTATION
771 #if !defined (_STLP_NO_IMPLEMENTATION)
773 _M_should_close = false;
774 _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
777 # undef _STLP_NO_IMPLEMENTATION
782 _STLP_DECLSPEC bool _Filebuf_base::_M_close() {
788 if (!_M_should_close)
792 #if defined (_STLP_USE_UNIX_IO)
794 ok = (close(_M_file_id) == 0);
796 #elif defined (_STLP_USE_UNIX_EMULATION_IO)
798 ok = (_close(_M_file_id) == 0);
800 #elif defined (_STLP_USE_STDIO_IO)
802 ok = (fclose(_M_file) == 0);
804 #elif defined (_STLP_USE_WIN32_IO)
806 if (_M_file_id != INVALID_STLP_FD) {
807 ok = (CloseHandle(_M_file_id) != 0);
817 #endif /* _STLP_USE_UNIX_IO */
820 _M_is_open = _M_should_close = false;
828 #define _STLP_CTRLZ 26
830 // Read up to n characters into a buffer. Return value is number of
832 _STLP_DECLSPEC ptrdiff_t _Filebuf_base::_M_read(char* buf, ptrdiff_t n) {
833 #if defined (_STLP_USE_UNIX_IO)
835 return read(_M_file_id, buf, n);
837 #elif defined (_STLP_USE_UNIX_EMULATION_IO)
839 return _read(_M_file_id, buf, n);
841 #elif defined (_STLP_USE_WIN32_IO)
842 ptrdiff_t readen = 0;
843 //Here cast to size_t is safe as n cannot be negative.
844 size_t chunkSize = (min)(size_t(0xffffffff), __STATIC_CAST(size_t, n));
845 // The following, while validating that we are still able to extract chunkSize
846 // charaters to the buffer, avoids extraction of too small chunk of datas
847 // which would be counter performant.
848 while (__STATIC_CAST(size_t, (n - readen)) >= chunkSize) {
849 DWORD NumberOfBytesRead;
850 ReadFile(_M_file_id, buf + readen, __STATIC_CAST(DWORD, chunkSize), &NumberOfBytesRead, 0);
852 if (NumberOfBytesRead == 0)
855 if (!(_M_openmode & ios_base::binary)) {
856 // translate CR-LFs to LFs in the buffer
857 char *to = buf + readen;
859 char *last = from + NumberOfBytesRead - 1;
860 for (; from <= last && *from != _STLP_CTRLZ; ++from) {
861 if (*from != _STLP_CR)
864 if (from < last) { // not at buffer end
865 if (*(from + 1) != _STLP_LF)
868 else { // last char is CR, peek for LF
870 DWORD NumberOfBytesPeeked;
871 ReadFile(_M_file_id, (LPVOID)&peek, 1, &NumberOfBytesPeeked, 0);
872 if (NumberOfBytesPeeked != 0) {
873 if (peek != _STLP_LF) { //not a <CR><LF> combination
875 if ((to < buf + n) && (peek != _STLP_CR))
876 //We have enough place to store peek and it is no a special
877 //_STLP_CR character, we can store it.
880 SetFilePointer(_M_file_id, (LONG)-1, 0, SEEK_CUR);
883 // A <CR><LF> combination, we keep the <LF>:
888 /* This case is tedious, we could
889 * - put peek back in the file but this would then generate an infinite loop
890 * - report an error as we don't know if in a future call to ReadFile we won't then
891 * get a <LF>. Doing so would make all files with a <CR> last an invalid file
892 * for STLport, a hard solution for STLport clients.
893 * - store the <CR> in the returned buffer, the chosen solution, even if in this
894 * case we could miss a <CR><LF> combination.
901 // seek back to TEXT end of file if hit CTRL-Z
902 if (from <= last) // terminated due to CTRLZ
903 SetFilePointer(_M_file_id, (LONG)((last+1) - from), 0, SEEK_CUR);
904 readen += to - (buf + readen);
907 readen += NumberOfBytesRead;
911 #elif defined (_STLP_USE_STDIO_IO)
913 return fread(buf, 1, n, _M_file);
920 // Write n characters from a buffer. Return value: true if we managed
921 // to write the entire buffer, false if we didn't.
922 _STLP_DECLSPEC bool _Filebuf_base::_M_write(char* buf, ptrdiff_t n) {
926 #if defined (_STLP_USE_UNIX_IO)
928 written = write(_M_file_id, buf, n);
930 #elif defined (_STLP_USE_UNIX_EMULATION_IO)
932 written = _write(_M_file_id, buf, n);
934 #elif defined (_STLP_USE_WIN32_IO)
936 //In the following implementation we are going to cast most of the ptrdiff_t
937 //values in size_t to work with coherent unsigned values. Doing so make code
938 //more simple especially in the min function call.
940 // In append mode, every write does an implicit seek to the end
942 if (_M_openmode & ios_base::app)
943 _M_seek(0, ios_base::end);
945 if (_M_openmode & ios_base::binary) {
947 size_t bytes_to_write = (size_t)n;
948 DWORD NumberOfBytesWritten;
950 for (; bytes_to_write != 0;) {
951 WriteFile(_M_file_id, buf + written,
952 __STATIC_CAST(DWORD, (min)(size_t(0xffffffff), bytes_to_write)),
953 &NumberOfBytesWritten, 0);
954 if (NumberOfBytesWritten == 0)
956 bytes_to_write -= NumberOfBytesWritten;
957 written += NumberOfBytesWritten;
961 char textbuf[_TEXTBUF_SIZE + 1]; // extra 1 in case LF at end
962 char * nextblock = buf, * ptrtextbuf = textbuf;
963 char * endtextbuf = textbuf + _TEXTBUF_SIZE;
964 char * endblock = buf + n;
965 ptrdiff_t nextblocksize = (min) (n, (ptrdiff_t)_TEXTBUF_SIZE);
968 while ( (nextblocksize > 0) &&
969 (nextlf = (char *)memchr(nextblock, _STLP_LF, nextblocksize)) != 0) {
970 ptrdiff_t linelength = nextlf - nextblock;
971 memcpy(ptrtextbuf, nextblock, linelength);
972 ptrtextbuf += linelength;
973 nextblock += (linelength + 1);
974 * ptrtextbuf ++ = _STLP_CR;
975 * ptrtextbuf ++ = _STLP_LF;
976 nextblocksize = (min) (ptrdiff_t(endblock - nextblock),
977 (max) (ptrdiff_t(0), ptrdiff_t(endtextbuf - ptrtextbuf)));
979 // write out what's left, > condition is here since for LF at the end ,
980 // endtextbuf may get < ptrtextbuf ...
981 if (nextblocksize > 0) {
982 memcpy(ptrtextbuf, nextblock, nextblocksize);
983 ptrtextbuf += nextblocksize;
984 nextblock += nextblocksize;
986 // now write out the translated buffer
987 char * writetextbuf = textbuf;
988 for (size_t NumberOfBytesToWrite = (size_t)(ptrtextbuf - textbuf);
989 NumberOfBytesToWrite;) {
990 DWORD NumberOfBytesWritten;
991 WriteFile((HANDLE)_M_file_id, writetextbuf,
992 __STATIC_CAST(DWORD, (min)(size_t(0xffffffff), NumberOfBytesToWrite)),
993 &NumberOfBytesWritten, 0);
994 if (!NumberOfBytesWritten) // write shortfall
996 writetextbuf += NumberOfBytesWritten;
997 NumberOfBytesToWrite -= NumberOfBytesWritten;
999 // count non-translated characters
1000 written = (nextblock - buf);
1003 #elif defined (_STLP_USE_STDIO_IO)
1005 written = fwrite(buf, 1, n, _M_file);
1013 else if (written > 0 && written < n) {
1023 #ifdef _STLP_USE_WIN32_IO
1024 # define STL_SEEK_SET FILE_BEGIN
1025 # define STL_SEEK_CUR FILE_CURRENT
1026 # define STL_SEEK_END FILE_END
1028 # define STL_SEEK_SET SEEK_SET
1029 # define STL_SEEK_CUR SEEK_CUR
1030 # define STL_SEEK_END SEEK_END
1033 // Wrapper for lseek or the like.
1034 _STLP_DECLSPEC streamoff _Filebuf_base::_M_seek(streamoff offset, ios_base::seekdir dir) {
1035 streamoff result = -1;
1040 if (offset < 0 /* || offset > _M_file_size() */ )
1041 return streamoff(-1);
1042 whence = STL_SEEK_SET;
1045 whence = STL_SEEK_CUR;
1048 if (/* offset > 0 || */ -offset > _M_file_size() )
1049 return streamoff(-1);
1050 whence = STL_SEEK_END;
1053 return streamoff(-1);
1056 #if defined (_STLP_USE_UNIX_IO) || defined (_STLP_USE_UNIX_EMULATION_IO)
1058 result = LSEEK(_M_file_id, offset, whence);
1060 #elif defined (_STLP_USE_STDIO_IO)
1062 #if defined (__SYMBIAN32__)
1063 /* This function is a wrapper for lseek and expects to return the offset in bytes from the beginning of the file.
1064 It is used by tellg as well as tellp (for setting/getting the file pointer).
1065 We don't want to use fseek for that. */
1066 if (FSEEK(_M_file, offset, whence) != 0)
1067 return streamoff(-1);
1068 result = FTELL(_M_file);
1070 result = FSEEK(_M_file, offset, whence);
1073 #elif defined (_STLP_USE_WIN32_IO)
1076 li.QuadPart = offset;
1077 li.LowPart = SetFilePointer(_M_file_id, li.LowPart, &li.HighPart, whence);
1078 if (li.LowPart != INVALID_SET_FILE_POINTER || GetLastError() == NO_ERROR)
1079 result = li.QuadPart;
1089 // Attempts to memory-map len bytes of the current file, starting
1090 // at position offset. Precondition: offset is a multiple of the
1091 // page size. Postcondition: return value is a null pointer if the
1092 // memory mapping failed. Otherwise the return value is a pointer to
1093 // the memory-mapped file and the file position is set to offset.
1094 _STLP_DECLSPEC void* _Filebuf_base::_M_mmap(streamoff offset, streamoff len) {
1096 #if defined (_STLP_UNIX) && !defined(__DJGPP) && !defined(_CRAY)
1097 base = MMAP(0, len, PROT_READ, MAP_PRIVATE, _M_file_id, offset);
1098 if (base != (void*)MAP_FAILED) {
1099 if (LSEEK(_M_file_id, offset + len, SEEK_SET) < 0) {
1100 this->_M_unmap(base, len);
1106 #elif defined (_STLP_USE_WIN32_IO)
1107 _M_view_id = CreateFileMapping(_M_file_id, (PSECURITY_ATTRIBUTES)0 ,
1108 PAGE_READONLY, 0 /* len >> 32 */ ,
1109 0 /* len & 0xFFFFFFFF */ , // low-order DWORD of size
1115 printf("view %x created from file %x, error = %d, size = %d, map_offset = %d map_len = %d\n",
1116 _M_view_id, _M_file_id, GetLastError(),
1117 (int)cur_filesize, ULL(offset) & 0xffffffff, len);
1120 base = MapViewOfFile(_M_view_id, FILE_MAP_READ, __STATIC_CAST(DWORD, ULL(offset) >> 32),
1121 __STATIC_CAST(DWORD, ULL(offset) & 0xffffffff),
1122 # if !defined (__DMC__)
1123 __STATIC_CAST(SIZE_T, len));
1125 __STATIC_CAST(DWORD, len));
1127 // check if mapping succeded and is usable
1128 if (base == 0 || _M_seek(offset + len, ios_base::beg) < 0) {
1129 this->_M_unmap(base, len);
1135 (void)len; //*TY 02/26/2000 - unused variables
1136 (void)offset; //*TY 02/26/2000 -
1142 _STLP_DECLSPEC void _Filebuf_base::_M_unmap(void* base, streamoff len) {
1143 // precondition : there is a valid mapping at the moment
1144 #if defined (_STLP_UNIX) && !defined(__DJGPP) && !defined(_CRAY)
1145 munmap((char*)base, len);
1146 #elif defined (_STLP_USE_WIN32_IO)
1148 UnmapViewOfFile(base);
1149 // destroy view handle as well
1150 if (_M_view_id != NULL)
1151 CloseHandle(_M_view_id);
1153 (void)len; //unused variable
1155 (void)len; //*TY 02/26/2000 - unused variables
1156 (void)base; //*TY 02/26/2000 -
1160 #if defined (__SYMBIAN32__) && defined(__EPOC32__)
1161 _STLP_DECLSPEC size_t _Filebuf_base::__page_size()
1163 return _Filebuf_base::_M_page_size;
1166 // fbp : let us map 1 MB maximum, just be sure not to trash VM
1167 #define MMAP_CHUNK 0x100000L
1169 _STLP_DECLSPEC int _STLP_CALL
1170 _Underflow<char, char_traits<char> >::_M_doit (basic_filebuf<char, char_traits<char> >* __this) {
1171 if (!__this->_M_in_input_mode) {
1172 if (!__this->_M_switch_to_input_mode())
1173 return traits_type::eof();
1175 else if (__this->_M_in_putback_mode) {
1176 __this->_M_exit_putback_mode();
1177 if (__this->gptr() != __this->egptr()) {
1178 int_type __c = traits_type::to_int_type(*__this->gptr());
1183 // If it's a disk file, and if the internal and external character
1184 // sequences are guaranteed to be identical, then try to use memory
1185 // mapped I/O. Otherwise, revert to ordinary read.
1186 if (__this->_M_base.__regular_file()
1187 && __this->_M_always_noconv
1188 && __this->_M_base._M_in_binary_mode()) {
1189 // If we've mmapped part of the file already, then unmap it.
1190 if (__this->_M_mmap_base)
1191 __this->_M_base._M_unmap(__this->_M_mmap_base, __this->_M_mmap_len);
1192 __this->_M_mmap_base = 0;
1193 __this->_M_mmap_len = 0;
1195 // Determine the position where we start mapping. It has to be
1196 // a multiple of the page size.
1197 streamoff __cur = __this->_M_base._M_seek(0, ios_base::cur);
1198 streamoff __size = __this->_M_base._M_file_size();
1199 if (__size > 0 && __cur >= 0 && __cur < __size) {
1200 streamoff __offset = (__cur / __this->_M_base.__page_size()) * __this->_M_base.__page_size();
1201 streamoff __remainder = __cur - __offset;
1203 __this->_M_mmap_len = __size - __offset;
1205 if (__this->_M_mmap_len > MMAP_CHUNK)
1206 __this->_M_mmap_len = MMAP_CHUNK;
1208 if ((__this->_M_mmap_base =
1209 __this->_M_base._M_mmap(__offset, __this->_M_mmap_len)) != 0) {
1210 __this->setg((char*) __this->_M_mmap_base,
1211 (char*) __this->_M_mmap_base + __STATIC_CAST(ptrdiff_t, __remainder),
1212 (char*) __this->_M_mmap_base + __STATIC_CAST(ptrdiff_t, __this->_M_mmap_len));
1213 return traits_type::to_int_type(*__this->gptr());
1215 } else /* size > 0 ... */ {
1216 // There is nothing to map. We unmapped the file above, now zap pointers.
1217 __this->_M_mmap_base = 0;
1218 __this->_M_mmap_len = 0;
1222 return __this->_M_underflow_aux();
1226 //----------------------------------------------------------------------
1227 // Force instantiation of filebuf and fstream classes.
1228 #if !defined(_STLP_NO_FORCE_INSTANTIATE)
1230 template class basic_filebuf<char, char_traits<char> >;
1231 template class basic_ifstream<char, char_traits<char> >;
1232 template class basic_ofstream<char, char_traits<char> >;
1233 template class basic_fstream<char, char_traits<char> >;
1235 # if !defined (_STLP_NO_WCHAR_T)
1236 template class _Underflow<wchar_t, char_traits<wchar_t> >;
1237 template class basic_filebuf<wchar_t, char_traits<wchar_t> >;
1238 template class basic_ifstream<wchar_t, char_traits<wchar_t> >;
1239 template class basic_ofstream<wchar_t, char_traits<wchar_t> >;
1240 template class basic_fstream<wchar_t, char_traits<wchar_t> >;
1241 # endif /* _STLP_NO_WCHAR_T */