[STLPORT] - Add basic_fstream(FILE *) constructor to make up for the completely broken basic_fstream(int) one

[EXPLORER] - Fix accordingly. Also fix totally broken initialization order in t[io]fstream

svn path=/trunk/; revision=58227
This commit is contained in:
Thomas Faber 2013-01-26 18:21:39 +00:00
parent 6e0c97bcbe
commit 25c6b6c631
3 changed files with 72 additions and 38 deletions

View file

@ -516,25 +516,41 @@ extern XS_String DecodeXMLString(const std::string& str);
/// base class for XMLStorage::tifstream and XMLStorage::tofstream
struct FileHolder
{
FileHolder(LPCTSTR path, LPCTSTR mode)
protected:
FileHolder()
{
//@@ _MS_VER: temporarily needed for the ReactOS build environment
#if defined(__STDC_WANT_SECURE_LIB__) && defined(_MS_VER) // secure CRT functions using VS 2005
if (_tfopen_s(&_pfile, path, mode) != 0)
_pfile = NULL;
#else
_pfile = _tfopen(path, mode);
#endif
}
~FileHolder()
{
if (_pfile)
fclose(_pfile);
delete _buf;
}
protected:
FILE* _pfile;
FILE_FILEBUF* init_buf(LPCTSTR path, std::ios_base::openmode mode)
{
PCTSTR modestr = mode == std::ios::in ? TEXT("rb") : TEXT("wb");
//@@ _MS_VER: temporarily needed for the ReactOS build environment
#if defined(__STDC_WANT_SECURE_LIB__) && defined(_MS_VER) // secure CRT functions using VS 2005
if (_tfopen_s(&_pfile, path, modestr) != 0)
_pfile = NULL;
#else
_pfile = _tfopen(path, modestr);
#endif
#ifdef __GNUC__
_buf = new FILE_FILEBUF(_pfile, mode);
#else
_buf = new FILE_FILEBUF;
if (_pfile)
_buf->open(_pfile, mode);
#endif
return _buf;
}
FILE* _pfile;
FILE_FILEBUF* _buf;
};
/// input file stream with ANSI/UNICODE file names
@ -543,24 +559,11 @@ struct tifstream : public std::istream, FileHolder
typedef std::istream super;
tifstream(LPCTSTR path)
: super(&_buf),
FileHolder(path, TEXT("rb")), // binary mode is important for XMLReader::read_buffer() with MinGW libraries
#ifdef __GNUC__
_buf(_pfile, std::ios::in)
#else
_buf()
#endif
: super(init_buf(path, std::ios::in))
{
if (!_pfile)
setstate(badbit);
#ifdef _MSC_VER
else
_buf.open(fileno(_pfile));
#endif
}
protected:
FILE_FILEBUF _buf;
};
/// output file stream with ANSI/UNICODE file names
@ -569,29 +572,16 @@ struct tofstream : public std::ostream, FileHolder
typedef std::ostream super;
tofstream(LPCTSTR path)
: super(&_buf),
FileHolder(path, TEXT("wb")),
#ifdef __GNUC__
_buf(_pfile, std::ios::out)
#else
_buf()
#endif
: super(init_buf(path, std::ios::out))
{
if (!_pfile)
setstate(badbit);
#ifdef _MSC_VER
else
_buf.open(fileno(_pfile));
#endif
}
~tofstream()
{
flush();
}
protected:
FILE_FILEBUF _buf;
};
#else // FILE_FILEBUF

View file

@ -59,6 +59,9 @@ public: // Opening and closing files.
bool _M_open(const char*, ios_base::openmode, long __protection);
bool _M_open(const char*, ios_base::openmode);
bool _M_open(int __id, ios_base::openmode = ios_base::__default_mode);
#if defined (_STLP_USE_STDIO_IO)
bool _M_open(FILE *file, ios_base::openmode = ios_base::__default_mode);
#endif /* _STLP_USE_STDIO_IO */
#if defined (_STLP_USE_WIN32_IO)
bool _M_open(_STLP_fd __id, ios_base::openmode = ios_base::__default_mode);
#endif /* _STLP_USE_WIN32_IO */
@ -182,6 +185,12 @@ public: // Opening and closing files.
return this->_M_open(__id, _Init_mode);
}
# if defined (_STLP_USE_STDIO_IO)
_Self* open(FILE *file, ios_base::openmode _Init_mode = ios_base::__default_mode) {
return _M_base._M_open(file, _Init_mode) ? this : 0;
}
# endif /* _STLP_USE_STDIO_IO */
# if defined (_STLP_USE_WIN32_IO)
_Self* open(_STLP_fd __id, ios_base::openmode _Init_mode = ios_base::__default_mode) {
return _M_base._M_open(__id, _Init_mode) ? this : 0;

View file

@ -300,6 +300,41 @@ bool _Filebuf_base::_M_open( int file_no, ios_base::openmode )
return true;
}
bool _Filebuf_base::_M_open(FILE *file, ios_base::openmode openmode)
{
_STLP_fd file_no;
if (_M_is_open)
return false;
_M_file = file;
if (_M_file) {
file_no = fileno(_M_file);
} else {
return false;
}
// unset buffering immediately
setbuf(_M_file, 0);
_M_is_open = true;
if (openmode & ios_base::ate) {
if (FSEEK(_M_file, 0, SEEK_END) != 0)
_M_is_open = false;
}
_M_file_id = file_no;
_M_should_close = _M_is_open;
_M_openmode = openmode;
if (_M_is_open)
_M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
return (_M_is_open != 0);
}
bool _Filebuf_base::_M_close()
{
if (!_M_is_open)