aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3
diff options
context:
space:
mode:
authorPaolo Carlini <pcarlini@suse.de>2003-11-18 08:21:38 +0000
committerPaolo Carlini <paolo@gcc.gnu.org>2003-11-18 08:21:38 +0000
commit2934246345f8f64dfcf01a9ea79e3bb1279c877c (patch)
treedaa2ff7e839fcb7ddd139eef47a4d6daedfcfab6 /libstdc++-v3
parent0ec5fef2187bc6f4f962e3afe1b2d829b7474d80 (diff)
downloadgcc-2934246345f8f64dfcf01a9ea79e3bb1279c877c.zip
gcc-2934246345f8f64dfcf01a9ea79e3bb1279c877c.tar.gz
gcc-2934246345f8f64dfcf01a9ea79e3bb1279c877c.tar.bz2
re PR libstdc++/12868 (basic_filebuf::imbue fails too easily)
2003-11-18 Paolo Carlini <pcarlini@suse.de> PR libstdc++/12868 * include/bits/fstream.tcc (imbue): For encodings != -1 it's always ok to imbue a new locale, provided seekoff(0, cur, ...) doesn't fail, of course. (underflow): In order for the above to work, deal gracefully with _M_codecvt->in returning codecvt_base::error while (__ilen = __iend - this->eback()) > 0: it just means __ilen correctly converted internal characters before an error. * testsuite/27_io/basic_filebuf/imbue/wchar_t/12868.cc: New. From-SVN: r73697
Diffstat (limited to 'libstdc++-v3')
-rw-r--r--libstdc++-v3/ChangeLog12
-rw-r--r--libstdc++-v3/include/bits/fstream.tcc31
-rw-r--r--libstdc++-v3/testsuite/27_io/basic_filebuf/imbue/wchar_t/12868.cc60
3 files changed, 85 insertions, 18 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index 4bce163..88079fd 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,3 +1,15 @@
+2003-11-18 Paolo Carlini <pcarlini@suse.de>
+
+ PR libstdc++/12868
+ * include/bits/fstream.tcc (imbue): For encodings != -1 it's
+ always ok to imbue a new locale, provided seekoff(0, cur, ...)
+ doesn't fail, of course.
+ (underflow): In order for the above to work, deal gracefully
+ with _M_codecvt->in returning codecvt_base::error while
+ (__ilen = __iend - this->eback()) > 0: it just means __ilen
+ correctly converted internal characters before an error.
+ * testsuite/27_io/basic_filebuf/imbue/wchar_t/12868.cc: New.
+
2003-11-17 Paolo Carlini <pcarlini@suse.de>
* include/bits/locale_facets.tcc: Fix typo in comment.
diff --git a/libstdc++-v3/include/bits/fstream.tcc b/libstdc++-v3/include/bits/fstream.tcc
index 0104711..0e1e36d 100644
--- a/libstdc++-v3/include/bits/fstream.tcc
+++ b/libstdc++-v3/include/bits/fstream.tcc
@@ -269,9 +269,7 @@ namespace std
__r = _M_codecvt->in(_M_state_cur, _M_ext_next,
_M_ext_end, _M_ext_next, this->eback(),
this->eback() + __buflen, __iend);
- if (__r == codecvt_base::ok || __r == codecvt_base::partial)
- __ilen = __iend - this->eback();
- else if (__r == codecvt_base::noconv)
+ if (__r == codecvt_base::noconv)
{
size_t __avail = _M_ext_end - _M_ext_buf;
__ilen = std::min(__avail, __buflen);
@@ -279,11 +277,15 @@ namespace std
reinterpret_cast<char_type*>(_M_ext_buf), __ilen);
_M_ext_next = _M_ext_buf + __ilen;
}
- else
- {
- __ilen = 0;
- break;
- }
+ else
+ __ilen = __iend - this->eback();
+
+ // _M_codecvt->in may return error while __ilen > 0: this is
+ // ok, and actually occurs in case of mixed encodings (e.g.,
+ // XML files).
+ if (__r == codecvt_base::error)
+ break;
+
__rlen = 1;
}
while (!__got_eof && __ilen == 0);
@@ -747,13 +749,13 @@ namespace std
bool __testfail = false;
if (this->is_open())
{
- const bool __testbeg =
+ const bool __testseek =
this->seekoff(0, ios_base::cur, this->_M_mode) ==
- pos_type(off_type(0));
+ pos_type(off_type(-1));
const bool __teststate =
__check_facet(_M_codecvt).encoding() == -1;
- __testfail = !__testbeg || __teststate;
+ __testfail = __testseek || __teststate;
}
if (!__testfail)
@@ -762,13 +764,6 @@ namespace std
_M_codecvt = &use_facet<__codecvt_type>(__loc);
else
_M_codecvt = 0;
-
- // NB This may require the reconversion of previously
- // converted chars. This in turn may cause the
- // reconstruction of the original file. YIKES!! This
- // implementation interprets this requirement as requiring
- // the file position be at the beginning, and a stateless
- // encoding, or that the filebuf be closed. Opinions may differ.
}
}
}
diff --git a/libstdc++-v3/testsuite/27_io/basic_filebuf/imbue/wchar_t/12868.cc b/libstdc++-v3/testsuite/27_io/basic_filebuf/imbue/wchar_t/12868.cc
new file mode 100644
index 0000000..8e4484b
--- /dev/null
+++ b/libstdc++-v3/testsuite/27_io/basic_filebuf/imbue/wchar_t/12868.cc
@@ -0,0 +1,60 @@
+// Copyright (C) 2003 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 <ostream>
+#include <fstream>
+#include <locale>
+#include <string>
+#include <testsuite_hooks.h>
+
+// libstdc++/12868
+void test01()
+{
+ using namespace std;
+ bool test __attribute__((unused)) = true;
+
+ locale loc_is(__gnu_test::try_named_locale("is_IS"));
+
+ {
+ wofstream out("tmp_12868");
+ out << L"<? xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
+ out.imbue(loc_is);
+ VERIFY( out.rdbuf()->getloc() == loc_is );
+ out << L"<greeting>Hall\u00f3 heimur</greeting>\n";
+ }
+
+ {
+ wifstream in("tmp_12868");
+ wstring str;
+ getline(in, str);
+ if (str.find(L"encoding=\"UTF-8\"") != wstring::npos)
+ {
+ in.imbue(loc_is);
+ VERIFY( in.rdbuf()->getloc() == loc_is );
+ }
+ getline(in, str);
+ VERIFY( str == L"<greeting>Hall\u00f3 heimur</greeting>" );
+ }
+}
+
+int main()
+{
+ test01();
+}