| 1 |
// Copyright (C) 2003-2025 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 3, 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 COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// 27.8.1.4 Overridden virtual functions |
| 19 |
|
| 20 |
// { dg-require-fileio "" } |
| 21 |
|
| 22 |
#include <fstream> |
| 23 |
#include <locale> |
| 24 |
#include <testsuite_hooks.h> |
| 25 |
|
| 26 |
struct MyState |
| 27 |
{ |
| 28 |
}; |
| 29 |
|
| 30 |
struct MyCharTraits : std::char_traits<char> |
| 31 |
{ |
| 32 |
typedef std::fpos<MyState> pos_type; |
| 33 |
typedef MyState state_type; |
| 34 |
}; |
| 35 |
|
| 36 |
namespace std |
| 37 |
{ |
| 38 |
template <> |
| 39 |
class codecvt<char, char, MyState> : |
| 40 |
public locale::facet, public codecvt_base |
| 41 |
{ |
| 42 |
public: |
| 43 |
typedef char intern_type; |
| 44 |
typedef char extern_type; |
| 45 |
typedef MyState state_type; |
| 46 |
|
| 47 |
explicit codecvt(size_t refs = 0) |
| 48 |
: locale::facet(refs) { } |
| 49 |
|
| 50 |
result out(state_type& state, const intern_type* from, |
| 51 |
const intern_type* from_end, const intern_type*& from_next, |
| 52 |
extern_type* to, extern_type* to_limit, |
| 53 |
extern_type*& to_next) const |
| 54 |
{ return do_out(state, from, from_end, from_next, |
| 55 |
to, to_limit, to_next); } |
| 56 |
|
| 57 |
result unshift(state_type& state, extern_type* to, extern_type* to_limit, |
| 58 |
extern_type*& to_next) const |
| 59 |
{ return do_unshift(state, to, to_limit, to_next); } |
| 60 |
|
| 61 |
result in(state_type& state, const extern_type* from, |
| 62 |
const extern_type* from_end, const extern_type*& from_next, |
| 63 |
intern_type* to, intern_type* to_limit, |
| 64 |
intern_type*& to_next) const |
| 65 |
{ return do_in(state, from, from_end, from_next, |
| 66 |
to, to_limit, to_next); } |
| 67 |
|
| 68 |
int encoding() const throw() |
| 69 |
{ return do_encoding(); } |
| 70 |
|
| 71 |
bool always_noconv() const throw() |
| 72 |
{ return do_always_noconv(); } |
| 73 |
|
| 74 |
int length(state_type& state, const extern_type* from, |
| 75 |
const extern_type* end, size_t max) const |
| 76 |
{ return do_length(state, from, end, max); } |
| 77 |
|
| 78 |
int max_length() const throw() |
| 79 |
{ return do_max_length(); } |
| 80 |
|
| 81 |
static locale::id id; |
| 82 |
|
| 83 |
protected: |
| 84 |
virtual ~codecvt(); |
| 85 |
|
| 86 |
virtual result do_out(state_type&, const intern_type* from, |
| 87 |
const intern_type*, const intern_type*& from_next, |
| 88 |
extern_type* to, extern_type*, |
| 89 |
extern_type*& to_next) const |
| 90 |
{ |
| 91 |
from_next = from; |
| 92 |
to_next = to; |
| 93 |
return noconv; |
| 94 |
} |
| 95 |
|
| 96 |
virtual result do_in(state_type&, const extern_type* from, |
| 97 |
const extern_type*, const extern_type*& from_next, |
| 98 |
intern_type* to, intern_type*, |
| 99 |
intern_type*& to_next) const |
| 100 |
{ |
| 101 |
from_next = from; |
| 102 |
to_next = to; |
| 103 |
return noconv; |
| 104 |
} |
| 105 |
|
| 106 |
virtual result do_unshift(state_type&, extern_type*, extern_type*, |
| 107 |
extern_type*&) const |
| 108 |
{ return noconv; } |
| 109 |
|
| 110 |
virtual int do_encoding() const throw() |
| 111 |
{ return 1; } |
| 112 |
|
| 113 |
virtual bool do_always_noconv() const throw() |
| 114 |
{ return true; } |
| 115 |
|
| 116 |
virtual int do_length(state_type&, const extern_type* from, |
| 117 |
const extern_type* end, size_t max) const |
| 118 |
{ |
| 119 |
size_t len = end - from; |
| 120 |
return std::min(max, len); |
| 121 |
} |
| 122 |
|
| 123 |
virtual int do_max_length() const throw() |
| 124 |
{ return 1; } |
| 125 |
}; |
| 126 |
|
| 127 |
locale::id codecvt<char, char, MyState>::id; |
| 128 |
|
| 129 |
codecvt<char, char, MyState>::~codecvt() |
| 130 |
{ } |
| 131 |
} |
| 132 |
|
| 133 |
void test01() |
| 134 |
{ |
| 135 |
std::locale loc(std::locale::classic(), |
| 136 |
new std::codecvt<char, char, MyState>); |
| 137 |
std::basic_filebuf<char, MyCharTraits> fb; |
| 138 |
fb.pubimbue(loc); |
| 139 |
fb.open("tmp_11543", std::ios_base::out); |
| 140 |
VERIFY( fb.is_open() ); |
| 141 |
MyCharTraits::pos_type pos = fb.pubseekoff(0, std::ios_base::beg); |
| 142 |
VERIFY( pos != MyCharTraits::pos_type(MyCharTraits::off_type(-1)) ); |
| 143 |
fb.close(); |
| 144 |
} |
| 145 |
|
| 146 |
int main() |
| 147 |
{ |
| 148 |
test01(); |
| 149 |
return 0; |
| 150 |
} |