1 // Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // 27.8.1.4 Overridden virtual functions
20 // { dg-require-binary-io "" }
24 #include <testsuite_hooks.h>
26 template <typename InternT, typename StateT = std::mbstate_t>
27 class checksumcvt : public std::codecvt<InternT, char, StateT>
29 typedef std::codecvt<InternT, char, StateT> Base;
30 static const std::size_t width = sizeof(InternT) + 1;
33 typedef InternT intern_type;
34 typedef char extern_type;
36 explicit checksumcvt(std::size_t refs = 0)
41 virtual std::codecvt_base::result
42 do_out(StateT&, const intern_type* from,
43 const intern_type* from_end, const intern_type*& from_next,
44 extern_type* to, extern_type* to_end,
45 extern_type*& to_next) const
47 size_t len = std::min(static_cast<size_t>(from_end - from),
48 static_cast<size_t>(to_end - to) / width);
53 reinterpret_cast<const char*>(from);
54 unsigned char checksum = 0;
56 for (std::size_t i = 0; i < sizeof(intern_type); ++i)
59 checksum ^= static_cast<unsigned char>(p[i]);
68 return from_next == from_end ? std::codecvt_base::ok
69 : std::codecvt_base::partial;
72 virtual std::codecvt_base::result
73 do_unshift(StateT&, extern_type* to,
74 extern_type*, extern_type*& to_next) const
77 return std::codecvt_base::ok;
80 virtual std::codecvt_base::result
81 do_in(StateT&, const extern_type* from,
82 const extern_type* from_end, const extern_type*& from_next,
83 intern_type* to, intern_type* to_end,
84 intern_type*& to_next) const
86 size_t len = std::min(static_cast<size_t>(to_end - to),
87 static_cast<size_t>(from_end - from) / width);
93 char* p = reinterpret_cast<char*>(&tmp);
94 unsigned char checksum = 0;
96 for (std::size_t i = 0; i < sizeof(intern_type); ++i)
99 checksum ^= static_cast<unsigned char>(*f++);
102 if (*f++ != checksum)
112 return len ? std::codecvt_base::error :
113 (from_next == from_end ? std::codecvt_base::ok
114 : std::codecvt_base::partial);
118 do_encoding() const throw()
122 do_length(const StateT&, const extern_type* from,
123 const extern_type* end, size_t max) const
125 size_t len = std::min(max,
126 static_cast<size_t>(end - from) / width);
131 unsigned char checksum = 0;
133 for (std::size_t i = 0; i < sizeof(intern_type); ++i)
135 checksum ^= static_cast<unsigned char>(*from++);
138 if (*from++ != checksum)
148 do_max_length() const throw()
152 do_always_noconv() const throw()
156 class Buf : public std::wfilebuf
159 std::streamsize pub_showmanyc()
160 { return showmanyc(); }
161 std::wfilebuf::int_type pub_underflow()
162 { return underflow(); }
169 bool test __attribute__((unused)) = true;
172 fbout.open("tmp_11603", ios_base::out);
173 fbout.sputn("aaaab", 5);
176 locale loc(locale::classic(), new checksumcvt<wchar_t>);
180 fb.open("tmp_11603", ios_base::in);
181 VERIFY( fb.pub_showmanyc() == 1 );
185 wfilebuf::int_type ret = fb.pub_underflow();
186 VERIFY( ret != wfilebuf::traits_type::eof() );
188 ret = fb.pub_underflow();
189 VERIFY( ret == wfilebuf::traits_type::eof() );