aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/src/localename.cc
blob: 45c4732d007fe1dbfff8fbefacab771961698b17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING.  If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.

// As a special exception, you may use this file as part of a free software
// library without restriction.  Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License.  This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.

#include <clocale>
#include <cstring>
#include <locale>
#include <vector>
#include <stdexcept>

namespace std
{
  locale::_Impl::
  ~_Impl() throw()
  {
    __vec_facet::iterator it = _M_facets->begin();
    for (; it != _M_facets->end(); ++it)
      if (*it)
	(*it)->_M_remove_reference();
    delete _M_facets;
  }

  // Clone existing _Impl object.
  locale::_Impl::
  _Impl(const _Impl& __imp, size_t __refs)
  : _M_references(__refs), _M_facets(0) // XXX
  {
    try
      {  _M_facets = new __vec_facet(*(__imp._M_facets)); }
    catch(...) 
      {
	delete _M_facets;
	__throw_exception_again;
      }

    for (size_t i = 0; i < _S_num_categories; ++i)
      _M_names[i] = __imp._M_names[i];

    __vec_facet::iterator __it = _M_facets->begin();
    for (; __it != _M_facets->end(); ++__it)
      if (*__it)
	(*__it)->_M_add_reference();
  }

  // Construct named _Impl, including the standard "C" locale.
  locale::_Impl::
  _Impl(string __str, size_t __refs)
  : _M_references(__refs), _M_facets(0)
  {
    // Initialize the underlying locale model, which also checks to
    // see if the given name is valid.
    __c_locale __cloc;
    locale::facet::_S_create_c_locale(__cloc, __str.c_str());

    // This is needed as presently "C" locales != required data in
    // __timepunct, numpunct, and moneypunct.
    __c_locale __cloc_c = NULL;
    if (__str != "C" && __str != "POSIX")
      __cloc_c = __cloc;

    // Allocate facet container.
    try
      {  _M_facets = new __vec_facet(_S_num_facets, NULL); }
    catch(...) 
      {
	delete _M_facets;
	__throw_exception_again;
      }

    // Name all the categories.
    for (size_t i = 0; i < _S_num_categories; ++i)
      _M_names[i] = __str;

    // Construct all standard facets and add them to _M_facets.
    _M_init_facet(new std::collate<char>(__cloc));
    _M_init_facet(new std::ctype<char>(__cloc));
    _M_init_facet(new codecvt<char, char, mbstate_t>);
    _M_init_facet(new moneypunct<char, false>(__cloc_c));
    _M_init_facet(new moneypunct<char, true>(__cloc_c));
    _M_init_facet(new money_get<char>);
    _M_init_facet(new money_put<char>);
    _M_init_facet(new numpunct<char>(__cloc_c));
    _M_init_facet(new num_get<char>);
    _M_init_facet(new num_put<char>);
    _M_init_facet(new __timepunct<char>(__cloc_c, __str.c_str()));
    _M_init_facet(new time_get<char>);
    _M_init_facet(new time_put<char>);
    _M_init_facet(new std::messages<char>(__cloc, __str.c_str()));
    
#ifdef  _GLIBCPP_USE_WCHAR_T
    _M_init_facet(new std::collate<wchar_t>(__cloc));
    _M_init_facet(new std::ctype<wchar_t>(__cloc));
    _M_init_facet(new codecvt<wchar_t, char, mbstate_t>);
    _M_init_facet(new moneypunct<wchar_t, false>(__cloc_c));
    _M_init_facet(new moneypunct<wchar_t, true>(__cloc_c));
    _M_init_facet(new money_get<wchar_t>);
    _M_init_facet(new money_put<wchar_t>);
    _M_init_facet(new numpunct<wchar_t>(__cloc_c));
    _M_init_facet(new num_get<wchar_t>);
    _M_init_facet(new num_put<wchar_t>);
    _M_init_facet(new __timepunct<wchar_t>(__cloc_c, __str.c_str()));
    _M_init_facet(new time_get<wchar_t>);
    _M_init_facet(new time_put<wchar_t>);
    _M_init_facet(new std::messages<wchar_t>(__cloc, __str.c_str()));
#endif	  
    locale::facet::_S_destroy_c_locale(__cloc);
  }
  
  void
  locale::_Impl::
  _M_replace_categories(const _Impl* __imp, category __cat)
  {
    const string __none("*");
    category __mask;
    for (unsigned int __ix = 0; __ix < _S_num_categories; ++__ix)
      {
	__mask = 1 << __ix;
	if (__mask & __cat)
	  {
	    // Need to replace entry in _M_facets with other locale's info.
	    _M_replace_category(__imp, _S_facet_categories[__ix]);
	    // If both have names, go ahead and mangle.
	    if (_M_names[__ix] != __none && __imp->_M_names[__ix] != __none)
	      _M_names[__ix] = __imp->_M_names[__ix];
	  }
      }
  }

  void
  locale::_Impl::
  _M_replace_category(const _Impl* __imp, const locale::id* const* __idpp)
  {
    for (; *__idpp; ++__idpp)
      _M_replace_facet(__imp, *__idpp);
  }
  
  void
  locale::_Impl::
  _M_replace_facet(const _Impl* __imp, const locale::id* __idp)
  {
    size_t __index = __idp->_M_index;
    if (__index == 0 
	|| __imp->_M_facets->size() <= __index 
	|| (*(__imp->_M_facets))[__index] == 0)
      __throw_runtime_error("no locale facet");
	
    _M_install_facet(__idp, (*(__imp->_M_facets))[__index]); 
  }

  void
  locale::_Impl::
  _M_install_facet(const locale::id* __idp, facet* __fp)
  {
    if (__fp)
      {
	size_t& __index = __idp->_M_index;
	if (!__index)
	  __index = ++locale::id::_S_highwater;  // XXX MT
	
	if (__index >= _M_facets->size())
	  _M_facets->resize(__index + 1, 0);  // might throw

	facet*& __fpr = (*_M_facets)[__index];
	if (__fpr)
	  {
	    // Replacing an existing facet.
	    // Order matters, here:
	    __fp->_M_add_reference();
	    __fpr->_M_remove_reference();
	    __fpr = __fp;
	  }
	else
	  {
	    // Installing a newly created facet into an empty
	    // _M_facets container, say a newly-constructed,
	    // swanky-fresh _Impl.
	    (*_M_facets)[__index] = __fp;
	  }
      }
  }
} // namespace std