OSDN Git Service

2002-09-10 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / codecvt.cc
1 // Copyright (C) 2000, 2002 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 // As a special exception, you may use this file as part of a free software
20 // library without restriction.  Specifically, if other files instantiate
21 // templates or use macros or inline functions from this file, or you compile
22 // this file and link it with other files to produce an executable, this
23 // file does not by itself cause the resulting executable to be covered by
24 // the GNU General Public License.  This exception does not however
25 // invalidate any other reasons why the executable file might be covered by
26 // the GNU General Public License.
27
28 // Written by Benjamin Kosnik <bkoz@cygnus.com>
29
30 #include <locale>
31
32 namespace std 
33 {
34   // Definitions for locale::id of standard facets that are specialized.
35  locale::id codecvt<char, char, mbstate_t>::id;
36
37 #ifdef _GLIBCPP_USE_WCHAR_T  
38   locale::id codecvt<wchar_t, char, mbstate_t>::id;
39 #endif
40
41 #ifdef _GLIBCPP_USE___ENC_TRAITS
42   // Definitions for static const data members of __enc_traits.
43   const int __enc_traits::_S_max_size;
44 #endif 
45
46   codecvt<char, char, mbstate_t>::
47   codecvt(size_t __refs)
48   : __codecvt_abstract_base<char, char, mbstate_t>(__refs)
49   { _M_c_locale_codecvt = _S_c_locale; }
50
51   codecvt<char, char, mbstate_t>::
52   codecvt(__c_locale __cloc, size_t __refs)
53   : __codecvt_abstract_base<char, char, mbstate_t>(__refs)
54   { _M_c_locale_codecvt = _S_clone_c_locale(__cloc); }
55
56   codecvt<char, char, mbstate_t>::
57   ~codecvt()
58    {
59      if (_M_c_locale_codecvt != _S_c_locale)
60        _S_destroy_c_locale(_M_c_locale_codecvt);
61    }
62   
63   codecvt_base::result
64   codecvt<char, char, mbstate_t>::
65   do_out(state_type&, const intern_type* __from, 
66          const intern_type* __from_end, const intern_type*& __from_next,
67          extern_type* __to, extern_type* __to_end, 
68          extern_type*& __to_next) const
69   { 
70     size_t __len = min(__from_end - __from, __to_end - __to);
71     memcpy(__to, __from, __len);
72     __from_next = __from; 
73     __to_next = __to;
74     return noconv;  
75   }
76   
77   codecvt_base::result
78   codecvt<char, char, mbstate_t>::
79   do_unshift(state_type&, extern_type* __to,
80              extern_type*, extern_type*& __to_next) const
81   { 
82     __to_next = __to; 
83     return noconv; 
84   }
85   
86   codecvt_base::result
87   codecvt<char, char, mbstate_t>::
88   do_in(state_type&, const extern_type* __from, 
89         const extern_type* __from_end, const extern_type*& __from_next,
90         intern_type* __to, intern_type* __to_end, 
91         intern_type*& __to_next) const
92   { 
93     size_t __len = min(__from_end - __from, __to_end - __to);
94     memcpy(__to, __from, __len);
95     __from_next = __from; 
96     __to_next = __to;
97     return noconv;  
98   }
99
100   int 
101   codecvt<char, char, mbstate_t>::
102   do_encoding() const throw() 
103   { return 1; }
104   
105   bool 
106   codecvt<char, char, mbstate_t>::
107   do_always_noconv() const throw() 
108   { return true; }
109   
110   int 
111   codecvt<char, char, mbstate_t>::
112   do_length (const state_type&, const extern_type* __from,
113              const extern_type* __end, size_t __max) const
114   { return min(__max, static_cast<size_t>(__end - __from)); }
115   
116   int 
117   codecvt<char, char, mbstate_t>::
118   do_max_length() const throw() 
119   { return 1; }
120   
121 #ifdef _GLIBCPP_USE_WCHAR_T
122   // codecvt<wchar_t, char, mbstate_t> required specialization
123   codecvt<wchar_t, char, mbstate_t>::
124   codecvt(size_t __refs)
125   : __codecvt_abstract_base<wchar_t, char, mbstate_t>(__refs)
126   { _M_c_locale_codecvt = _S_c_locale; }  
127
128   codecvt<wchar_t, char, mbstate_t>::
129   codecvt(__c_locale __cloc, size_t __refs)
130   : __codecvt_abstract_base<wchar_t, char, mbstate_t>(__refs)
131   { _M_c_locale_codecvt = _S_clone_c_locale(__cloc); }
132
133   codecvt<wchar_t, char, mbstate_t>::
134   ~codecvt()
135   {
136     if (_M_c_locale_codecvt != _S_c_locale)
137       _S_destroy_c_locale(_M_c_locale_codecvt); 
138   }
139   
140   codecvt_base::result
141   codecvt<wchar_t, char, mbstate_t>::
142   do_unshift(state_type&, extern_type* __to,
143              extern_type*, extern_type*& __to_next) const
144   {
145     __to_next = __to;
146     return noconv;
147   }
148   
149   int 
150   codecvt<wchar_t, char, mbstate_t>::
151   do_encoding() const throw()
152   { return sizeof(wchar_t); }
153   
154   bool 
155   codecvt<wchar_t, char, mbstate_t>::
156   do_always_noconv() const throw()
157   { return false; }
158   
159   int 
160   codecvt<wchar_t, char, mbstate_t>::
161   do_length(const state_type&, const extern_type* __from,
162             const extern_type* __end, size_t __max) const
163   { return min(__max, static_cast<size_t>(__end - __from)); }
164
165   int 
166   codecvt<wchar_t, char, mbstate_t>::
167   do_max_length() const throw()
168   { return 1; }
169 #endif //  _GLIBCPP_USE_WCHAR_T
170 } // namespace std