diff options
author | Paolo Carlini <pcarlini@suse.de> | 2004-01-11 15:17:02 +0000 |
---|---|---|
committer | Paolo Carlini <paolo@gcc.gnu.org> | 2004-01-11 15:17:02 +0000 |
commit | 09625c1681b30febed411fa7526cf4b620a54ab3 (patch) | |
tree | 265078aa8a455b8d2943d54accb0d8d8122148c4 | |
parent | df9bad81839fd9e5323543cbfe74d9859eb4dc1a (diff) | |
download | gcc-09625c1681b30febed411fa7526cf4b620a54ab3.tar.gz |
re PR libstdc++/13582 (basic_filebuf::imbue drops characters)
2004-01-11 Paolo Carlini <pcarlini@suse.de>
PR libstdc++/13582
* include/bits/fstream.tcc (imbue): Exploit the external
buffer to imbue 'on the fly' a new locale and convert its
remainder with the new codecvt facet.
(underflow): Tweak slightly to deal with this special case.
* testsuite/27_io/basic_filebuf/imbue/char/13582-2.cc: New.
* testsuite/27_io/basic_filebuf/imbue/wchar_t/13582-2.cc: Ditto.
* testsuite/27_io/basic_filebuf/imbue/wchar_t/13582-3.cc: Ditto.
* testsuite/27_io/objects/wchar_t/13582-1_xin.cc: Ditto.
* testsuite/27_io/objects/wchar_t/13582-1_xin.in: Ditto.
From-SVN: r75677
7 files changed, 359 insertions, 17 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 8e5ca1e5d9a..1c425f13887 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,16 @@ +2004-01-11 Paolo Carlini <pcarlini@suse.de> + + PR libstdc++/13582 + * include/bits/fstream.tcc (imbue): Exploit the external + buffer to imbue 'on the fly' a new locale and convert its + remainder with the new codecvt facet. + (underflow): Tweak slightly to deal with this special case. + * testsuite/27_io/basic_filebuf/imbue/char/13582-2.cc: New. + * testsuite/27_io/basic_filebuf/imbue/wchar_t/13582-2.cc: Ditto. + * testsuite/27_io/basic_filebuf/imbue/wchar_t/13582-3.cc: Ditto. + * testsuite/27_io/objects/wchar_t/13582-1_xin.cc: Ditto. + * testsuite/27_io/objects/wchar_t/13582-1_xin.in: Ditto. + 2004-01-10 Paolo Carlini <pcarlini@suse.de> * docs/html/ext/lwg-active.html, docs/html/ext/lwg-defects.html: diff --git a/libstdc++-v3/include/bits/fstream.tcc b/libstdc++-v3/include/bits/fstream.tcc index 998e0c34429..ed2fb6aace9 100644 --- a/libstdc++-v3/include/bits/fstream.tcc +++ b/libstdc++-v3/include/bits/fstream.tcc @@ -1,6 +1,6 @@ // File based streams -*- C++ -*- -// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003 +// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 // Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free @@ -171,7 +171,7 @@ namespace std } return __ret; } - + template<typename _CharT, typename _Traits> typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>:: @@ -222,20 +222,25 @@ namespace std } const streamsize __remainder = _M_ext_end - _M_ext_next; __rlen = __rlen > __remainder ? __rlen - __remainder : 0; - + + // An imbue in 'read' mode implies first converting the external + // chars already present. + if (_M_reading && this->egptr() == this->eback() && __remainder) + __rlen = 0; + // Allocate buffer if necessary and move unconverted // bytes to front. if (_M_ext_buf_size < __blen) { char* __buf = new char[__blen]; - if (__remainder > 0) + if (__remainder) std::memcpy(__buf, _M_ext_next, __remainder); delete [] _M_ext_buf; _M_ext_buf = __buf; _M_ext_buf_size = __blen; } - else if (__remainder > 0) + else if (__remainder) std::memmove(_M_ext_buf, _M_ext_next, __remainder); _M_ext_next = _M_ext_buf; @@ -738,22 +743,52 @@ namespace std basic_filebuf<_CharT, _Traits>:: imbue(const locale& __loc) { - bool __testfail = false; - if (this->is_open()) - { - const pos_type __ret = this->seekoff(0, ios_base::cur, - this->_M_mode); - const bool __teststate = __check_facet(_M_codecvt).encoding() == -1; - __testfail = __teststate && __ret != pos_type(off_type(0)); - } + bool __testvalid = true; + + const __codecvt_type* _M_codecvt_tmp = 0; + if (__builtin_expect(has_facet<__codecvt_type>(__loc), true)) + _M_codecvt_tmp = &use_facet<__codecvt_type>(__loc); - if (!__testfail) + if (this->is_open()) { - if (__builtin_expect(has_facet<__codecvt_type>(__loc), true)) - _M_codecvt = &use_facet<__codecvt_type>(__loc); + // encoding() == -1 is ok only at the beginning. + if ((_M_reading || _M_writing) + && __check_facet(_M_codecvt).encoding() == -1) + __testvalid = false; else - _M_codecvt = 0; + { + if (_M_reading) + { + if (__check_facet(_M_codecvt).always_noconv()) + { + if (_M_codecvt_tmp + && !__check_facet(_M_codecvt_tmp).always_noconv()) + __testvalid = this->seekoff(0, ios_base::cur, this->_M_mode) + != pos_type(off_type(-1)); + } + else + { + // External position corresponding to gptr(). + _M_ext_next = _M_ext_buf + + _M_codecvt->length(_M_state_last, _M_ext_buf, _M_ext_next, + this->gptr() - this->eback()); + const streamsize __remainder = _M_ext_end - _M_ext_next; + if (__remainder) + std::memmove(_M_ext_buf, _M_ext_next, __remainder); + + _M_ext_next = _M_ext_buf; + _M_ext_end = _M_ext_buf + __remainder; + _M_set_buffer(-1); + _M_state_last = _M_state_cur = _M_state_beg; + } + } + else if (_M_writing && (__testvalid = _M_terminate_output())) + _M_set_buffer(-1); + } } + + if (__testvalid) + _M_codecvt = _M_codecvt_tmp; } // Inhibit implicit instantiations for required instantiations, diff --git a/libstdc++-v3/testsuite/27_io/basic_filebuf/imbue/char/13582-2.cc b/libstdc++-v3/testsuite/27_io/basic_filebuf/imbue/char/13582-2.cc new file mode 100644 index 00000000000..7bab4c5b439 --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_filebuf/imbue/char/13582-2.cc @@ -0,0 +1,80 @@ +// 2004-01-11 Petur Runolfsson <peturr02@ru.is> + +// Copyright (C) 2004 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, +// USA. + +// 27.8.1.4 Overridden virtual functions + +#include <fstream> +#include <locale> + +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> + +#include <testsuite_hooks.h> + +// libstdc++/13582 +void test01() +{ + bool test __attribute__((unused)) = true; + using namespace std; + + locale loc_en(__gnu_test::try_named_locale("en_US")); + locale loc_fr(__gnu_test::try_named_locale("fr_FR")); + + const char* name = "tmp_fifo_13582-2"; + unlink(name); + mkfifo(name, S_IRWXU); + + int child = fork(); + if (child == 0) + { + filebuf fbout; + fbout.open(name, ios_base::out); + fbout.sputn("12345", 5); + fbout.pubsync(); + sleep(2); + fbout.close(); + exit(0); + } + + filebuf fbin; + fbin.open(name, ios_base::in); + sleep(1); + filebuf::int_type n = fbin.sbumpc(); + VERIFY( n == '1' ); + fbin.pubimbue(loc_en); + n = fbin.sbumpc(); + VERIFY( n == '2' ); + fbin.pubimbue(loc_fr); + n = fbin.sbumpc(); + VERIFY( n == '3' ); + n = fbin.sbumpc(); + VERIFY( n == '4' ); + n = fbin.sbumpc(); + VERIFY( n == '5' ); + n = fbin.sbumpc(); + VERIFY( n == filebuf::traits_type::eof() ); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/27_io/basic_filebuf/imbue/wchar_t/13582-2.cc b/libstdc++-v3/testsuite/27_io/basic_filebuf/imbue/wchar_t/13582-2.cc new file mode 100644 index 00000000000..83ce03b0b37 --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_filebuf/imbue/wchar_t/13582-2.cc @@ -0,0 +1,80 @@ +// 2004-01-11 Petur Runolfsson <peturr02@ru.is> + +// Copyright (C) 2004 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, +// USA. + +// 27.8.1.4 Overridden virtual functions + +#include <fstream> +#include <locale> + +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> + +#include <testsuite_hooks.h> + +// libstdc++/13582 +void test01() +{ + bool test __attribute__((unused)) = true; + using namespace std; + + locale loc_en(__gnu_test::try_named_locale("en_US")); + locale loc_fr(__gnu_test::try_named_locale("fr_FR")); + + const char* name = "tmp_fifo_13582-2"; + unlink(name); + mkfifo(name, S_IRWXU); + + int child = fork(); + if (child == 0) + { + filebuf fbout; + fbout.open(name, ios_base::out); + fbout.sputn("12345", 5); + fbout.pubsync(); + sleep(2); + fbout.close(); + exit(0); + } + + wfilebuf fbin; + fbin.open(name, ios_base::in); + sleep(1); + wfilebuf::int_type n = fbin.sbumpc(); + VERIFY( n == L'1' ); + fbin.pubimbue(loc_en); + n = fbin.sbumpc(); + VERIFY( n == L'2' ); + fbin.pubimbue(loc_fr); + n = fbin.sbumpc(); + VERIFY( n == L'3' ); + n = fbin.sbumpc(); + VERIFY( n == L'4' ); + n = fbin.sbumpc(); + VERIFY( n == L'5' ); + n = fbin.sbumpc(); + VERIFY( n == wfilebuf::traits_type::eof() ); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/27_io/basic_filebuf/imbue/wchar_t/13582-3.cc b/libstdc++-v3/testsuite/27_io/basic_filebuf/imbue/wchar_t/13582-3.cc new file mode 100644 index 00000000000..afda81412cb --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_filebuf/imbue/wchar_t/13582-3.cc @@ -0,0 +1,72 @@ +// 2004-01-11 Petur Runolfsson <peturr02@ru.is> + +// Copyright (C) 2004 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, +// USA. + +// 27.8.1.4 Overridden virtual functions + +#include <fstream> +#include <locale> +#include <testsuite_hooks.h> + +// libstdc++/13582 +int test01() +{ + bool test __attribute__((unused)) = true; + using namespace std; + + locale loc_en(__gnu_test::try_named_locale("en_US")); + locale loc_fr(__gnu_test::try_named_locale("fr_FR")); + + const char* name = "tmp_13582-3.tst"; + + { + filebuf fbout; + fbout.open(name, ios_base::out); + fbout.sputn("AbCdE", 5); + fbout.close(); + } + + { + wfilebuf fbin; + fbin.open(name, ios_base::in); + wfilebuf::int_type n = fbin.sbumpc(); + VERIFY( n == L'A' ); + fbin.pubimbue(loc_en); + fbin.pubseekoff(0, ios_base::cur); + n = fbin.sbumpc(); + VERIFY( n == L'b' ); + fbin.pubimbue(loc_fr); + n = fbin.sbumpc(); + VERIFY( n == L'C' ); + n = fbin.sbumpc(); + VERIFY( n == L'd' ); + fbin.pubseekoff(0, ios_base::cur); + n = fbin.sbumpc(); + VERIFY( n == L'E' ); + n = fbin.sbumpc(); + VERIFY( n == wfilebuf::traits_type::eof() ); + fbin.close(); + } +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/27_io/objects/wchar_t/13582-1_xin.cc b/libstdc++-v3/testsuite/27_io/objects/wchar_t/13582-1_xin.cc new file mode 100644 index 00000000000..008bf7e9399 --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/objects/wchar_t/13582-1_xin.cc @@ -0,0 +1,61 @@ +// 2004-01-11 Petur Runolfsson <peturr02@ru.is> + +// Copyright (C) 2004 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, +// USA. + +#include <iostream> +#include <string> +#include <locale> + +// libstdc++/13582 +void test01() +{ + bool test __attribute__((unused)) = true; + using namespace std; + + ios_base::sync_with_stdio(false); + wcout << "Type in 12345\n"; + + wstring str; + wchar_t c; + + if (wcin.get(c) && !isspace(c, wcin.getloc())) + { + str.push_back(c); + wcin.imbue(locale("en_US")); + } + + if (wcin.get(c) && !isspace(c, wcin.getloc())) + { + str.push_back(c); + wcin.imbue(locale("fr_FR")); + } + + while (wcin.get(c) && !isspace(c, wcin.getloc())) + { + str.push_back(c); + } + + wcout << str << endl; +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/27_io/objects/wchar_t/13582-1_xin.in b/libstdc++-v3/testsuite/27_io/objects/wchar_t/13582-1_xin.in new file mode 100644 index 00000000000..e56e15bb7dd --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/objects/wchar_t/13582-1_xin.in @@ -0,0 +1 @@ +12345 |