stdio_filebuf.h: New file.

2002-04-30  Benjamin Kosnik  <bkoz@redhat.com>

	* include/ext/stdio_filebuf.h: New file.
	* include/ext/enc_filebuf.h: New file.
	* config/io/basic_file_stdio.h (__basic_file::sys_open): Add fd ctor.
	* config/io/basic_file_stdio.cc: Same.
	* include/bits/fstream.tcc (filebuf::_M_allocate_internal_buffer):
	Remove _M_unbuf hacks.
	(filebuf::_M_destroy_internal_buffer): Same.
	(filebuf::filebuf(cfile, openmode, int_type): Remove definition.
	(filebuf::fd): Remove.
	* include/std/std_fstream.h (filebuf::_M_unbuf): Remove.
	(filebuf::filebuf(__c_file*, openmode, int_type)): Remove.
	(filebuf::fd): Remove.
	* src/ios.cc (ios_base::_S_ios_create): Change to use specialized
	filebufs.
	(ios_base::_S_ios_destroy): Same.
	* src/misc-inst.cc (file_filebuf<char>): Add instantiation.
	* include/Makefile.am (ext_headers): Add ext_filebuf.h,
	stdio_filebuf.h.  * include/Makefile.in: Regenerate.

From-SVN: r52963
This commit is contained in:
Benjamin Kosnik 2002-04-30 19:05:47 +00:00 committed by Benjamin Kosnik
parent ca4834b7c8
commit b7952e6c3e
11 changed files with 111 additions and 97 deletions

View File

@ -1,3 +1,24 @@
2002-04-30 Benjamin Kosnik <bkoz@redhat.com>
* include/ext/stdio_filebuf.h: New file.
* include/ext/enc_filebuf.h: New file.
* config/io/basic_file_stdio.h (__basic_file::sys_open): Add fd ctor.
* config/io/basic_file_stdio.cc: Same.
* include/bits/fstream.tcc (filebuf::_M_allocate_internal_buffer):
Remove _M_unbuf hacks.
(filebuf::_M_destroy_internal_buffer): Same.
(filebuf::filebuf(cfile, openmode, int_type): Remove definition.
(filebuf::fd): Remove.
* include/std/std_fstream.h (filebuf::_M_unbuf): Remove.
(filebuf::filebuf(__c_file*, openmode, int_type)): Remove.
(filebuf::fd): Remove.
* src/ios.cc (ios_base::_S_ios_create): Change to use specialized
filebufs.
(ios_base::_S_ios_destroy): Same.
* src/misc-inst.cc (file_filebuf<char>): Add instantiation.
* include/Makefile.am (ext_headers): Add ext_filebuf.h,
stdio_filebuf.h. * include/Makefile.in: Regenerate.
2002-04-30 Benjamin Kosnik <bkoz@redhat.com>
PR libstdc++/6511

View File

@ -74,8 +74,31 @@ namespace std
__basic_file* __ret = NULL;
if (!this->is_open() && __file)
{
_M_cfile = __file;
_M_cfile_created = false;
_M_cfile = __file;
_M_cfile_created = false;
__ret = this;
}
return __ret;
}
__basic_file<char>*
__basic_file<char>::sys_open(int __fd, ios_base::openmode __mode,
bool __del)
{
__basic_file* __ret = NULL;
int __p_mode = 0;
int __rw_mode = 0;
char __c_mode[4];
_M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
if (!this->is_open() && (_M_cfile = fdopen(__fd, __c_mode)))
{
// Iff __del is true, then close will fclose the fd.
_M_cfile_created = __del;
if (__fd == 0)
setvbuf(_M_cfile, reinterpret_cast<char*>(NULL), _IONBF, 0);
__ret = this;
}
return __ret;
@ -84,7 +107,7 @@ namespace std
int
__basic_file<char>::sys_getc()
{ return getc(_M_cfile); }
int
__basic_file<char>::sys_ungetc(int __c)
{ return ungetc(__c, _M_cfile); }
@ -112,10 +135,12 @@ namespace std
}
bool
__basic_file<char>::is_open() const { return _M_cfile != 0; }
__basic_file<char>::is_open() const
{ return _M_cfile != 0; }
int
__basic_file<char>::fd() { return fileno(_M_cfile) ; }
__basic_file<char>::fd()
{ return fileno(_M_cfile) ; }
__basic_file<char>*
__basic_file<char>::close()
@ -157,5 +182,6 @@ namespace std
}
int
__basic_file<char>::sync() { return fflush(_M_cfile); }
__basic_file<char>::sync()
{ return fflush(_M_cfile); }
} // namespace std

View File

@ -70,7 +70,10 @@ namespace std
open(const char* __name, ios_base::openmode __mode, int __prot = 0664);
__basic_file*
sys_open(__c_file* __file, ios_base::openmode __mode);
sys_open(__c_file* __file, ios_base::openmode);
__basic_file*
sys_open(int __fd, ios_base::openmode __mode, bool __del);
int
sys_getc();

View File

@ -143,6 +143,8 @@ ext_srcdir = ${glibcpp_srcdir}/include/ext
ext_builddir = ./ext
ext_headers = \
${ext_srcdir}/algorithm \
${ext_srcdir}/enc_filebuf.h \
${ext_srcdir}/stdio_filebuf.h \
${ext_srcdir}/functional \
${ext_srcdir}/hash_map \
${ext_srcdir}/hash_set \

View File

@ -272,6 +272,8 @@ ext_srcdir = ${glibcpp_srcdir}/include/ext
ext_builddir = ./ext
ext_headers = \
${ext_srcdir}/algorithm \
${ext_srcdir}/enc_filebuf.h \
${ext_srcdir}/stdio_filebuf.h \
${ext_srcdir}/functional \
${ext_srcdir}/hash_map \
${ext_srcdir}/hash_set \

View File

@ -48,19 +48,14 @@ namespace std
{
_M_buf_size = _M_buf_size_opt;
if (_M_buf_size != 1)
// Allocate internal buffer.
try { _M_buf = new char_type[_M_buf_size]; }
catch(...)
{
// Allocate internal buffer.
try { _M_buf = new char_type[_M_buf_size]; }
catch(...)
{
delete [] _M_buf;
__throw_exception_again;
}
_M_buf_allocated = true;
delete [] _M_buf;
__throw_exception_again;
}
else
_M_buf = _M_unbuf;
_M_buf_allocated = true;
}
}
@ -78,52 +73,15 @@ namespace std
this->setg(NULL, NULL, NULL);
this->setp(NULL, NULL);
}
else
{
if (_M_buf == _M_unbuf)
{
_M_buf = NULL;
this->setg(NULL, NULL, NULL);
this->setp(NULL, NULL);
}
}
}
template<typename _CharT, typename _Traits>
basic_filebuf<_CharT, _Traits>::
basic_filebuf()
: __streambuf_type(), _M_file(&_M_lock), _M_state_cur(__state_type()),
_M_state_beg(__state_type()), _M_buf_allocated(false),
_M_last_overflowed(false)
basic_filebuf() : __streambuf_type(), _M_file(&_M_lock),
_M_state_cur(__state_type()), _M_state_beg(__state_type()),
_M_buf_allocated(false), _M_last_overflowed(false)
{ _M_buf_unified = true; }
template<typename _CharT, typename _Traits>
basic_filebuf<_CharT, _Traits>::
basic_filebuf(__c_file* __f, ios_base::openmode __mode, int_type __s)
: __streambuf_type(), _M_file(&_M_lock), _M_state_cur(__state_type()),
_M_state_beg(__state_type()), _M_buf_allocated(false),
_M_last_overflowed(false)
{
_M_buf_unified = true;
_M_file.sys_open(__f, __mode);
if (this->is_open())
{
_M_mode = __mode;
if (__s)
{
_M_buf_size_opt = __s;
_M_allocate_internal_buffer();
_M_set_indeterminate();
}
}
}
template<typename _CharT, typename _Traits>
int
basic_filebuf<_CharT, _Traits>::
fd()
{ return _M_file.fd(); }
template<typename _CharT, typename _Traits>
typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
basic_filebuf<_CharT, _Traits>::

View File

@ -84,12 +84,9 @@ namespace std
__state_type _M_state_cur;
__state_type _M_state_beg;
// Set iff _M_buf is allocated memory from _M_allocate_internal_buffer..
// Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
bool _M_buf_allocated;
// Stack-based buffer for unbuffered input.
char_type _M_unbuf[4];
// XXX Needed?
bool _M_last_overflowed;
@ -101,14 +98,6 @@ namespace std
// Constructors/destructor:
basic_filebuf();
// Non-standard ctor:
basic_filebuf(__c_file* __f, ios_base::openmode __mode,
int_type __s = static_cast<int_type>(BUFSIZ));
// Non-standard member:
int
fd();
virtual
~basic_filebuf()
{

View File

@ -31,6 +31,7 @@
#include <istream>
#include <ostream>
#include <locale>
#include <ext/stdio_filebuf.h>
// On AIX, and perhaps other systems, library initialization order is
// not guaranteed. For example, the static initializers for the main
@ -176,8 +177,8 @@ namespace std
fake_ostream cerr;
fake_ostream clog;
typedef char fake_filebuf[sizeof(filebuf)]
__attribute__ ((aligned(__alignof__(filebuf))));
typedef char fake_filebuf[sizeof(__gnu_cxx::stdio_filebuf<char>)]
__attribute__ ((aligned(__alignof__(__gnu_cxx::stdio_filebuf<char>))));
fake_filebuf buf_cout;
fake_filebuf buf_cin;
fake_filebuf buf_cerr;
@ -192,8 +193,8 @@ namespace std
fake_wostream wcerr;
fake_wostream wclog;
typedef char fake_wfilebuf[sizeof(wfilebuf)]
__attribute__ ((aligned(__alignof__(wfilebuf))));
typedef char fake_wfilebuf[sizeof(__gnu_cxx::stdio_filebuf<wchar_t>)]
__attribute__ ((aligned(__alignof__(__gnu_cxx::stdio_filebuf<wchar_t>))));
fake_wfilebuf buf_wcout;
fake_wfilebuf buf_wcin;
fake_wfilebuf buf_wcerr;

View File

@ -36,8 +36,8 @@
#include <ostream>
#include <istream>
#include <fstream>
#include <bits/atomicity.h>
#include <ext/stdio_filebuf.h>
namespace std
{
@ -46,18 +46,21 @@ namespace std
extern ostream cout;
extern ostream cerr;
extern ostream clog;
extern filebuf buf_cout;
extern filebuf buf_cin;
extern filebuf buf_cerr;
using __gnu_cxx::stdio_filebuf;
extern stdio_filebuf<char> buf_cout;
extern stdio_filebuf<char> buf_cin;
extern stdio_filebuf<char> buf_cerr;
#ifdef _GLIBCPP_USE_WCHAR_T
extern wistream wcin;
extern wostream wcout;
extern wostream wcerr;
extern wostream wclog;
extern wfilebuf buf_wcout;
extern wfilebuf buf_wcin;
extern wfilebuf buf_wcerr;
extern stdio_filebuf<wchar_t> buf_wcout;
extern stdio_filebuf<wchar_t> buf_wcin;
extern stdio_filebuf<wchar_t> buf_wcerr;
#endif
// Definitions for static const data members of __ios_flags.
@ -147,15 +150,15 @@ namespace std
void
ios_base::Init::_S_ios_create(bool __sync)
{
int __out_bufsize = __sync ? 0 : static_cast<int>(BUFSIZ);
int __in_bufsize = __sync ? 1 : static_cast<int>(BUFSIZ);
int __out_size = __sync ? 0 : static_cast<int>(BUFSIZ);
int __in_size = __sync ? 1 : static_cast<int>(BUFSIZ);
// NB: The file globals.cc creates the four standard files
// with NULL buffers. At this point, we swap out the dummy NULL
// [io]stream objects and buffers with the real deal.
new (&buf_cout) filebuf(stdout, ios_base::out, __out_bufsize);
new (&buf_cin) filebuf(stdin, ios_base::in, __in_bufsize);
new (&buf_cerr) filebuf(stderr, ios_base::out, __out_bufsize);
new (&buf_cout) stdio_filebuf<char>(stdout, ios_base::out, __out_size);
new (&buf_cin) stdio_filebuf<char>(stdin, ios_base::in, __in_size);
new (&buf_cerr) stdio_filebuf<char>(stderr, ios_base::out, __out_size);
new (&cout) ostream(&buf_cout);
new (&cin) istream(&buf_cin);
new (&cerr) ostream(&buf_cerr);
@ -164,9 +167,9 @@ namespace std
cerr.flags(ios_base::unitbuf);
#ifdef _GLIBCPP_USE_WCHAR_T
new (&buf_wcout) wfilebuf(stdout, ios_base::out, __out_bufsize);
new (&buf_wcin) wfilebuf(stdin, ios_base::in, __in_bufsize);
new (&buf_wcerr) wfilebuf(stderr, ios_base::out, __out_bufsize);
new (&buf_wcout) stdio_filebuf<wchar_t>(stdout, ios_base::out, __out_size);
new (&buf_wcin) stdio_filebuf<wchar_t>(stdin, ios_base::in, __in_size);
new (&buf_wcerr) stdio_filebuf<wchar_t>(stderr, ios_base::out, __out_size);
new (&wcout) wostream(&buf_wcout);
new (&wcin) wistream(&buf_wcin);
new (&wcerr) wostream(&buf_wcerr);
@ -182,13 +185,14 @@ namespace std
// Explicitly call dtors to free any memory that is dynamically
// allocated by filebuf ctor or member functions, but don't
// deallocate all memory by calling operator delete.
buf_cout.~filebuf();
buf_cin.~filebuf();
buf_cerr.~filebuf();
buf_cout.~stdio_filebuf();
buf_cin.~stdio_filebuf();
buf_cerr.~stdio_filebuf();
#ifdef _GLIBCPP_USE_WCHAR_T
buf_wcout.~wfilebuf();
buf_wcin.~wfilebuf();
buf_wcerr.~wfilebuf();
buf_wcout.~stdio_filebuf();
buf_wcin.~stdio_filebuf();
buf_wcerr.~stdio_filebuf();
#endif
}

View File

@ -44,6 +44,7 @@
#include <istream>
#include <ostream>
#include <iomanip>
#include <ext/stdio_filebuf.h>
// NB: Unnecessary if the .h headers already include these.
#ifndef _GLIBCPP_FULLY_COMPLIANT_HEADERS
@ -264,4 +265,10 @@ namespace std
__copy_streambufs(basic_ios<wchar_t>&, basic_streambuf<wchar_t>*,
basic_streambuf<wchar_t>*);
#endif
using __gnu_cxx::stdio_filebuf;
template class stdio_filebuf<char>;
#ifdef _GLIBCPP_USE_WCHAR_T
template class stdio_filebuf<wchar_t>;
#endif
} //std

View File

@ -30,6 +30,7 @@
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ext/stdio_filebuf.h>
#include <testsuite_hooks.h>
const char name_01[] = "filebuf_members-1.tst";
@ -89,7 +90,7 @@ void test_02()
FILE* f2 = fopen(name_01, "r");
VERIFY( f2 != NULL );
{
std::filebuf fb(f2, std::ios_base::in, 512);
__gnu_cxx::stdio_filebuf<char> fb(f2, std::ios_base::in, 512);
}
close_num = fclose(f2);
VERIFY( close_num == 0 );
@ -115,7 +116,7 @@ void test_03()
VERIFY( first_fd != -1 );
FILE* first_file = ::fdopen(first_fd, "r");
VERIFY( first_file != NULL );
std::filebuf fb (first_file, std::ios_base::in);
__gnu_cxx::stdio_filebuf<char> fb(first_file, std::ios_base::in);
int second_fd = fb.fd();