📄 fstream.cpp
字号:
ptrdiff_t _Filebuf_base::_M_read(char* buf, ptrdiff_t n) {
#if defined (_STLP_USE_UNIX_IO)
return read(_M_file_id, buf, n);
#elif defined (_STLP_USE_UNIX_EMULATION_IO)
return _read(_M_file_id, buf, n);
#elif defined (_STLP_USE_WIN32_IO)
ptrdiff_t readen = 0;
//Here cast to size_t is safe as n cannot be negative.
size_t chunkSize = (min)(size_t(0xffffffff), __STATIC_CAST(size_t, n));
// The following, while validating that we are still able to extract chunkSize
// charaters to the buffer, avoids extraction of too small chunk of datas
// which would be counter performant.
while (__STATIC_CAST(size_t, (n - readen)) >= chunkSize) {
DWORD numberOfBytesRead;
ReadFile(_M_file_id, buf + readen, __STATIC_CAST(DWORD, chunkSize), &numberOfBytesRead, 0);
if (numberOfBytesRead == 0)
break;
if (!(_M_openmode & ios_base::binary)) {
// translate CR-LFs to LFs in the buffer
char *to = buf + readen;
char *from = to;
char *last = from + numberOfBytesRead - 1;
for (; from <= last && *from != _STLP_CTRLZ; ++from) {
if (*from != _STLP_CR)
*to++ = *from;
else { // found CR
if (from < last) { // not at buffer end
if (*(from + 1) != _STLP_LF)
*to++ = _STLP_CR;
}
else { // last char is CR, peek for LF
char peek = ' ';
DWORD NumberOfBytesPeeked;
ReadFile(_M_file_id, (LPVOID)&peek, 1, &NumberOfBytesPeeked, 0);
if (NumberOfBytesPeeked != 0) {
if (peek != _STLP_LF) { //not a <CR><LF> combination
*to++ = _STLP_CR;
if ((to < buf + n) && (peek != _STLP_CR))
//We have enough place to store peek and it is no a special
//_STLP_CR character, we can store it.
*to++ = peek;
else
SetFilePointer(_M_file_id, (LONG)-1, 0, SEEK_CUR);
}
else {
// A <CR><LF> combination, we keep the <LF>:
*to++ = _STLP_LF;
}
}
else {
/* This case is tedious, we could
* - put peek back in the file but this would then generate an infinite loop
* - report an error as we don't know if in a future call to ReadFile we won't then
* get a <LF>. Doing so would make all files with a <CR> last an invalid file
* for STLport, a hard solution for STLport clients.
* - store the <CR> in the returned buffer, the chosen solution, even if in this
* case we could miss a <CR><LF> combination.
*/
*to++ = _STLP_CR;
}
}
} // found CR
} // for
readen = to - buf;
// seek back to TEXT end of file if hit CTRL-Z
if (from <= last) { // terminated due to CTRLZ
SetFilePointer(_M_file_id, -(LONG)((last + 1) - from), 0, SEEK_CUR);
break;
}
}
else
readen += numberOfBytesRead;
}
return readen;
#elif defined (_STLP_USE_STDIO_IO)
return fread(buf, 1, n, _M_file);
#else
# error "Port!"
#endif /* __unix */
}
// Write n characters from a buffer. Return value: true if we managed
// to write the entire buffer, false if we didn't.
bool _Filebuf_base::_M_write(char* buf, ptrdiff_t n) {
for (;;) {
ptrdiff_t written;
#if defined (_STLP_USE_UNIX_IO)
written = write(_M_file_id, buf, n);
#elif defined (_STLP_USE_UNIX_EMULATION_IO)
written = _write(_M_file_id, buf, n);
#elif defined (_STLP_USE_WIN32_IO)
//In the following implementation we are going to cast most of the ptrdiff_t
//values in size_t to work with coherent unsigned values. Doing so make code
//more simple especially in the min function call.
// In append mode, every write does an implicit seek to the end
// of the file.
if (_M_openmode & ios_base::app)
_M_seek(0, ios_base::end);
if (_M_openmode & ios_base::binary) {
// binary mode
size_t bytes_to_write = (size_t)n;
DWORD NumberOfBytesWritten;
written = 0;
for (; bytes_to_write != 0;) {
WriteFile(_M_file_id, buf + written,
__STATIC_CAST(DWORD, (min)(size_t(0xffffffff), bytes_to_write)),
&NumberOfBytesWritten, 0);
if (NumberOfBytesWritten == 0)
return false;
bytes_to_write -= NumberOfBytesWritten;
written += NumberOfBytesWritten;
}
}
else {
char textbuf[_TEXTBUF_SIZE + 1]; // extra 1 in case LF at end
char * nextblock = buf, * ptrtextbuf = textbuf;
char * endtextbuf = textbuf + _TEXTBUF_SIZE;
char * endblock = buf + n;
ptrdiff_t nextblocksize = (min) (n, (ptrdiff_t)_TEXTBUF_SIZE);
char * nextlf;
while ( (nextblocksize > 0) &&
(nextlf = (char *)memchr(nextblock, _STLP_LF, nextblocksize)) != 0) {
ptrdiff_t linelength = nextlf - nextblock;
memcpy(ptrtextbuf, nextblock, linelength);
ptrtextbuf += linelength;
nextblock += (linelength + 1);
* ptrtextbuf ++ = _STLP_CR;
* ptrtextbuf ++ = _STLP_LF;
nextblocksize = (min) (ptrdiff_t(endblock - nextblock),
(max) (ptrdiff_t(0), ptrdiff_t(endtextbuf - ptrtextbuf)));
}
// write out what's left, > condition is here since for LF at the end ,
// endtextbuf may get < ptrtextbuf ...
if (nextblocksize > 0) {
memcpy(ptrtextbuf, nextblock, nextblocksize);
ptrtextbuf += nextblocksize;
nextblock += nextblocksize;
}
// now write out the translated buffer
char * writetextbuf = textbuf;
for (size_t NumberOfBytesToWrite = (size_t)(ptrtextbuf - textbuf);
NumberOfBytesToWrite;) {
DWORD NumberOfBytesWritten;
WriteFile((HANDLE)_M_file_id, writetextbuf,
__STATIC_CAST(DWORD, (min)(size_t(0xffffffff), NumberOfBytesToWrite)),
&NumberOfBytesWritten, 0);
if (!NumberOfBytesWritten) // write shortfall
return false;
writetextbuf += NumberOfBytesWritten;
NumberOfBytesToWrite -= NumberOfBytesWritten;
}
// count non-translated characters
written = (nextblock - buf);
}
#elif defined (_STLP_USE_STDIO_IO)
written = fwrite(buf, 1, n, _M_file);
#else
# error "Port!"
#endif /* __unix */
if (n == written)
return true;
else if (written > 0 && written < n) {
n -= written;
buf += written;
}
else
return false;
}
}
#ifdef _STLP_USE_WIN32_IO
# define STL_SEEK_SET FILE_BEGIN
# define STL_SEEK_CUR FILE_CURRENT
# define STL_SEEK_END FILE_END
#else
# define STL_SEEK_SET SEEK_SET
# define STL_SEEK_CUR SEEK_CUR
# define STL_SEEK_END SEEK_END
#endif
// Wrapper for lseek or the like.
streamoff _Filebuf_base::_M_seek(streamoff offset, ios_base::seekdir dir) {
streamoff result = -1;
int whence;
switch(dir) {
case ios_base::beg:
if (offset < 0 /* || offset > _M_file_size() */ )
return streamoff(-1);
whence = STL_SEEK_SET;
break;
case ios_base::cur:
whence = STL_SEEK_CUR;
break;
case ios_base::end:
if (/* offset > 0 || */ -offset > _M_file_size() )
return streamoff(-1);
whence = STL_SEEK_END;
break;
default:
return streamoff(-1);
}
#if defined (_STLP_USE_UNIX_IO) || defined (_STLP_USE_UNIX_EMULATION_IO)
result = LSEEK(_M_file_id, offset, whence);
#elif defined (_STLP_USE_STDIO_IO)
result = FSEEK(_M_file, offset, whence);
#elif defined (_STLP_USE_WIN32_IO)
LARGE_INTEGER li;
li.QuadPart = offset;
li.LowPart = SetFilePointer(_M_file_id, li.LowPart, &li.HighPart, whence);
if (li.LowPart != INVALID_SET_FILE_POINTER || GetLastError() == NO_ERROR)
result = li.QuadPart;
#else
# error "Port!"
#endif
return result;
}
// Attempts to memory-map len bytes of the current file, starting
// at position offset. Precondition: offset is a multiple of the
// page size. Postcondition: return value is a null pointer if the
// memory mapping failed. Otherwise the return value is a pointer to
// the memory-mapped file and the file position is set to offset.
void* _Filebuf_base::_M_mmap(streamoff offset, streamoff len) {
void* base;
#if defined (_STLP_UNIX) && !defined(__DJGPP) && !defined(_CRAY)
base = MMAP(0, len, PROT_READ, MAP_PRIVATE, _M_file_id, offset);
if (base != (void*)MAP_FAILED) {
if (LSEEK(_M_file_id, offset + len, SEEK_SET) < 0) {
this->_M_unmap(base, len);
base = 0;
}
} else
base =0;
#elif defined (_STLP_USE_WIN32_IO)
_M_view_id = CreateFileMapping(_M_file_id, (PSECURITY_ATTRIBUTES)0 ,
PAGE_READONLY, 0 /* len >> 32 */ ,
0 /* len & 0xFFFFFFFF */ , // low-order DWORD of size
0);
if (_M_view_id) {
# if 0
/*
printf("view %x created from file %x, error = %d, size = %d, map_offset = %d map_len = %d\n",
_M_view_id, _M_file_id, GetLastError(),
(int)cur_filesize, ULL(offset) & 0xffffffff, len);
*/
# endif
base = MapViewOfFile(_M_view_id, FILE_MAP_READ, __STATIC_CAST(DWORD, ULL(offset) >> 32),
__STATIC_CAST(DWORD, ULL(offset) & 0xffffffff),
# if !defined (__DMC__)
__STATIC_CAST(SIZE_T, len));
# else
__STATIC_CAST(DWORD, len));
# endif
// check if mapping succeded and is usable
if (base == 0 || _M_seek(offset + len, ios_base::beg) < 0) {
this->_M_unmap(base, len);
base = 0;
}
} else
base = 0;
#else
(void)len; //*TY 02/26/2000 - unused variables
(void)offset; //*TY 02/26/2000 -
base = 0;
#endif
return base;
}
void _Filebuf_base::_M_unmap(void* base, streamoff len) {
// precondition : there is a valid mapping at the moment
#if defined (_STLP_UNIX) && !defined(__DJGPP) && !defined(_CRAY)
munmap((char*)base, len);
#elif defined (_STLP_USE_WIN32_IO)
if (base != NULL)
UnmapViewOfFile(base);
// destroy view handle as well
if (_M_view_id != NULL)
CloseHandle(_M_view_id);
_M_view_id = NULL;
(void)len; //unused variable
#else
(void)len; //*TY 02/26/2000 - unused variables
(void)base; //*TY 02/26/2000 -
#endif
}
// fbp : let us map 1 MB maximum, just be sure not to trash VM
#define MMAP_CHUNK 0x100000L
int _STLP_CALL
_Underflow<char, char_traits<char> >::_M_doit (basic_filebuf<char, char_traits<char> >* __this) {
if (!__this->_M_in_input_mode) {
if (!__this->_M_switch_to_input_mode())
return traits_type::eof();
}
else if (__this->_M_in_putback_mode) {
__this->_M_exit_putback_mode();
if (__this->gptr() != __this->egptr()) {
int_type __c = traits_type::to_int_type(*__this->gptr());
return __c;
}
}
// If it's a disk file, and if the internal and external character
// sequences are guaranteed to be identical, then try to use memory
// mapped I/O. Otherwise, revert to ordinary read.
if (__this->_M_base.__regular_file()
&& __this->_M_always_noconv
&& __this->_M_base._M_in_binary_mode()) {
// If we've mmapped part of the file already, then unmap it.
if (__this->_M_mmap_base)
__this->_M_base._M_unmap(__this->_M_mmap_base, __this->_M_mmap_len);
// Determine the position where we start mapping. It has to be
// a multiple of the page size.
streamoff __cur = __this->_M_base._M_seek(0, ios_base::cur);
streamoff __size = __this->_M_base._M_file_size();
if (__size > 0 && __cur >= 0 && __cur < __size) {
streamoff __offset = (__cur / __this->_M_base.__page_size()) * __this->_M_base.__page_size();
streamoff __remainder = __cur - __offset;
__this->_M_mmap_len = __size - __offset;
if (__this->_M_mmap_len > MMAP_CHUNK)
__this->_M_mmap_len = MMAP_CHUNK;
if ((__this->_M_mmap_base =
__this->_M_base._M_mmap(__offset, __this->_M_mmap_len)) != 0) {
__this->setg((char*) __this->_M_mmap_base,
(char*) __this->_M_mmap_base + __STATIC_CAST(ptrdiff_t, __remainder),
(char*) __this->_M_mmap_base + __STATIC_CAST(ptrdiff_t, __this->_M_mmap_len));
return traits_type::to_int_type(*__this->gptr());
} else
__this->_M_mmap_len = 0;
}
else {
__this->_M_mmap_base = 0;
__this->_M_mmap_len = 0;
}
}
return __this->_M_underflow_aux();
}
//----------------------------------------------------------------------
// Force instantiation of filebuf and fstream classes.
#if !defined(_STLP_NO_FORCE_INSTANTIATE)
template class basic_filebuf<char, char_traits<char> >;
template class basic_ifstream<char, char_traits<char> >;
template class basic_ofstream<char, char_traits<char> >;
template class basic_fstream<char, char_traits<char> >;
# if !defined (_STLP_NO_WCHAR_T)
template class _Underflow<wchar_t, char_traits<wchar_t> >;
template class basic_filebuf<wchar_t, char_traits<wchar_t> >;
template class basic_ifstream<wchar_t, char_traits<wchar_t> >;
template class basic_ofstream<wchar_t, char_traits<wchar_t> >;
template class basic_fstream<wchar_t, char_traits<wchar_t> >;
# endif /* _STLP_NO_WCHAR_T */
#endif
_STLP_END_NAMESPACE
// Local Variables:
// mode:C++
// End:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -