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.
8 
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.
13 
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.
18 
19 // 27.8.1.4 Overridden virtual functions
20 
21 #include <fstream>
22 #include <locale>
23 #include <algorithm>
24 #include <cstring>
25 #include <testsuite_hooks.h>
26 
27 class Cvt : public std::codecvt<wchar_t, char, mbstate_t>
28 {
29 protected:
30   virtual std::codecvt_base::result
do_out(std::mbstate_t &,const wchar_t * from,const wchar_t * from_end,const wchar_t * & from_next,char * to,char * to_end,char * & to_next) const31   do_out(std::mbstate_t&, const wchar_t* from, const wchar_t* from_end,
32 	 const wchar_t*& from_next, char* to, char* to_end,
33 	 char*& to_next) const
34   {
35     std::size_t from_len = from_end - from;
36     std::size_t to_len = (to_end - to) / sizeof(wchar_t);
37     std::size_t len = std::min(from_len, to_len);
38     std::memcpy(to, from, len * sizeof(wchar_t));
39     from_next = from + len;
40     to_next = to + len * sizeof(wchar_t);
41     return from_next == from_end ? std::codecvt_base::ok :
42            std::codecvt_base::partial;
43   }
44 
45   virtual std::codecvt_base::result
do_in(std::mbstate_t &,const char * from,const char * from_end,const char * & from_next,wchar_t * to,wchar_t * to_end,wchar_t * & to_next) const46   do_in(std::mbstate_t&, const char* from, const char* from_end,
47 	const char*& from_next, wchar_t* to, wchar_t* to_end,
48 	wchar_t*& to_next) const
49   {
50     std::size_t from_len =
51       (from_end - from) / sizeof(wchar_t);
52     std::size_t to_len = to_end - to;
53     std::size_t len = std::min(from_len, to_len);
54     std::memcpy(to, from, len * sizeof(wchar_t));
55     from_next = from + len * sizeof(wchar_t);
56     to_next = to + len;
57     return from_next == from_end ? std::codecvt_base::ok :
58            std::codecvt_base::partial;
59   }
60 
61   virtual std::codecvt_base::result
do_unshift(std::mbstate_t &,char *,char *,char * &) const62   do_unshift(std::mbstate_t&, char*, char*, char*&) const
63   { return std::codecvt_base::noconv; }
64 
do_encoding() const65   virtual int do_encoding() const throw() { return sizeof(wchar_t); }
do_always_noconv() const66   virtual bool do_always_noconv() const throw() { return false; }
67 
68   virtual int
do_length(std::mbstate_t &,const char * from,const char * end,std::size_t max)69   do_length(std::mbstate_t&, const char* from, const char* end,
70 	    std::size_t max)
71   {
72     std::size_t len = (end - from) / sizeof(wchar_t);
73     return std::min(len, max) * sizeof(wchar_t);
74   }
75 
do_max_length() const76   virtual int do_max_length() const throw() { return sizeof(wchar_t); }
77 };
78 
test01()79 void test01()
80 {
81   using namespace std;
82   bool test __attribute__((unused)) = true;
83 
84   // seekpos
85   wfilebuf fb;
86   fb.pubimbue(locale(locale::classic(), new Cvt));
87   fb.open("tmp_9875_seekpos", ios_base::out | ios_base::in | ios_base::trunc);
88   fb.sputn(L"0123456789", 10);
89 
90   streampos p1 = fb.pubseekoff(0, ios_base::cur);
91   VERIFY( p1 != streampos(-1) );
92   fb.sputc(L'a');
93 
94   streampos p2 = fb.pubseekpos(p1);
95   VERIFY( p2 != streampos(-1) );
96   VERIFY( p2 == p1 );
97   VERIFY( fb.sgetc() == L'a' );
98 
99   fb.pubseekoff(0, ios_base::beg);
100   wchar_t buf[11];
101   streamsize s1 = fb.sgetn(buf, 11);
102   VERIFY( s1 == 11 );
103   VERIFY( !wmemcmp(buf, L"0123456789a", 11) );
104 
105   fb.close();
106 }
107 
main()108 int main()
109 {
110   test01();
111   return 0;
112 }
113