2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / testsuite / 27_io / basic_filebuf / underflow / wchar_t / 11544-1.cc
blob91d92e4d12746d043c1d4b60b541fa2e2fb28833
1 // Copyright (C) 2003 Free Software Foundation, Inc.
2 //
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 2, or (at your option)
7 // any later version.
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 COPYING. If not, write to the Free
16 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 // USA.
19 // 27.8.1.4 Overridden virtual functions
21 #include <fstream>
22 #include <locale>
23 #include <cstdio>
24 #include <testsuite_hooks.h>
26 template <typename InternT, typename StateT = mbstate_t>
27 class checksumcvt : public std::codecvt<InternT, char, StateT>
29 typedef std::codecvt<InternT, char, StateT> Base;
30 static const size_t width = sizeof(InternT) + 1;
32 public:
33 typedef InternT intern_type;
34 typedef char extern_type;
36 explicit checksumcvt(size_t refs = 0)
37 : Base(refs)
38 { }
40 protected:
41 virtual typename std::codecvt<InternT, char, StateT>::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);
50 while (len--)
52 const char* p = reinterpret_cast<const char*>(from);
53 unsigned char checksum = 0;
55 for (size_t i = 0; i < sizeof(intern_type); ++i)
57 *to++ = p[i];
58 checksum ^= static_cast<unsigned char>(p[i]);
61 *to++ = checksum;
62 ++from;
65 from_next = from;
66 to_next = to;
67 return from_next == from_end ? std::codecvt<InternT, char, StateT>::ok
68 : std::codecvt<InternT, char, StateT>::partial;
71 virtual typename std::codecvt<InternT, char, StateT>::result
72 do_unshift(StateT&, extern_type* to,
73 extern_type*, extern_type*& to_next) const
75 to_next = to;
76 return std::codecvt<InternT, char, StateT>::ok;
79 virtual typename std::codecvt<InternT, char, StateT>::result
80 do_in(StateT&, const extern_type* from,
81 const extern_type* from_end, const extern_type*& from_next,
82 intern_type* to, intern_type* to_end,
83 intern_type*& to_next) const
85 size_t len = std::min(static_cast<size_t>(to_end - to),
86 static_cast<size_t>(from_end - from) / width);
88 while (len)
90 const char* f = from;
91 intern_type tmp;
92 char* p = reinterpret_cast<char*>(&tmp);
93 unsigned char checksum = 0;
95 for (size_t i = 0; i < sizeof(intern_type); ++i)
97 p[i] = *f;
98 checksum ^= static_cast<unsigned char>(*f++);
101 if (*f++ != checksum)
102 break;
104 from = f;
105 *to++ = tmp;
106 len--;
109 from_next = from;
110 to_next = to;
111 return len ? std::codecvt<InternT, char, StateT>::error :
112 (from_next == from_end ? std::codecvt<InternT, char, StateT>::ok
113 : std::codecvt<InternT, char, StateT>::partial);
116 virtual int
117 do_encoding() const throw()
118 { return width; }
120 virtual int
121 do_length(StateT&, const extern_type* from,
122 const extern_type* end, size_t max) const
124 size_t len = std::min(max, static_cast<size_t>(end - from) / width);
126 int ret = 0;
127 while (len--)
129 unsigned char checksum = 0;
131 for (size_t i = 0; i < sizeof(intern_type); ++i)
133 checksum ^= static_cast<unsigned char>(*from++);
136 if (*from++ != checksum)
137 break;
139 ret++;
142 return ret;
145 virtual int
146 do_max_length() const throw()
147 { return width; }
149 virtual bool
150 do_always_noconv() const throw()
151 { return false; }
154 // libstdc++/11544 (incomplete character in file)
155 void test01()
157 using namespace std;
158 bool test __attribute__((unused)) = true;
160 locale loc(locale::classic(), new checksumcvt<wchar_t>);
162 const char* name = "tmp_11544-1";
164 FILE* f = fopen(name, "w");
165 putc('a', f);
166 fclose(f);
168 wifstream in;
169 in.imbue(loc);
170 in.open(name);
172 VERIFY( in.good() );
173 in.get();
174 VERIFY( !in.good() );
175 VERIFY( in.bad() );
176 VERIFY( !in.eof() );
178 in.close();
181 int main()
183 test01();
184 return 0;