1 // Copyright (C) 2003, 2004, 2009 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
22 #include <testsuite_hooks.h>
24 template <typename InternT, typename StateT = std::mbstate_t>
25 class checksumcvt : public std::codecvt<InternT, char, StateT>
27 typedef std::codecvt<InternT, char, StateT> Base;
28 static const std::size_t width = sizeof(InternT) + 1;
31 typedef InternT intern_type;
32 typedef char extern_type;
34 explicit checksumcvt(std::size_t refs = 0)
39 virtual std::codecvt_base::result
40 do_out(StateT&, const intern_type* from,
41 const intern_type* from_end, const intern_type*& from_next,
42 extern_type* to, extern_type* to_end,
43 extern_type*& to_next) const
45 size_t len = std::min(static_cast<size_t>(from_end - from),
46 static_cast<size_t>(to_end - to) / width);
51 reinterpret_cast<const char*>(from);
52 unsigned char checksum = 0;
54 for (std::size_t i = 0; i < sizeof(intern_type); ++i)
57 checksum ^= static_cast<unsigned char>(p[i]);
66 return from_next == from_end ? std::codecvt_base::ok
67 : std::codecvt_base::partial;
70 virtual std::codecvt_base::result
71 do_unshift(StateT&, extern_type* to,
72 extern_type*, extern_type*& to_next) const
75 return std::codecvt_base::ok;
78 virtual std::codecvt_base::result
79 do_in(StateT&, const extern_type* from,
80 const extern_type* from_end, const extern_type*& from_next,
81 intern_type* to, intern_type* to_end,
82 intern_type*& to_next) const
84 size_t len = std::min(static_cast<size_t>(to_end - to),
85 static_cast<size_t>(from_end - from) / width);
91 char* p = reinterpret_cast<char*>(&tmp);
92 unsigned char checksum = 0;
94 for (std::size_t i = 0; i < sizeof(intern_type); ++i)
97 checksum ^= static_cast<unsigned char>(*f++);
100 if (*f++ != checksum)
110 return len ? std::codecvt_base::error :
111 (from_next == from_end ? std::codecvt_base::ok
112 : std::codecvt_base::partial);
116 do_encoding() const throw()
120 do_length(const StateT&, const extern_type* from,
121 const extern_type* end, size_t max) const
123 size_t len = std::min(max,
124 static_cast<size_t>(end - from) / width);
129 unsigned char checksum = 0;
131 for (std::size_t i = 0; i < sizeof(intern_type); ++i)
133 checksum ^= static_cast<unsigned char>(*from++);
136 if (*from++ != checksum)
146 do_max_length() const throw()
150 do_always_noconv() const throw()
154 class Buf : public std::wfilebuf
157 std::streamsize pub_showmanyc()
158 { return showmanyc(); }
159 std::wfilebuf::int_type pub_underflow()
160 { return underflow(); }
167 bool test __attribute__((unused)) = true;
170 fbout.open("tmp_11603", ios_base::out);
171 fbout.sputn("aaaab", 5);
174 locale loc(locale::classic(), new checksumcvt<wchar_t>);
178 fb.open("tmp_11603", ios_base::in);
179 VERIFY( fb.pub_showmanyc() == 1 );
183 wfilebuf::int_type ret = fb.pub_underflow();
184 VERIFY( ret != wfilebuf::traits_type::eof() );
186 ret = fb.pub_underflow();
187 VERIFY( ret == wfilebuf::traits_type::eof() );