libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
unicode_conversions.hpp
Go to the documentation of this file.
1 #ifndef LIBJMMCG_CORE_UNICODE_CONVERSIONS_HPP
2 #define LIBJMMCG_CORE_UNICODE_CONVERSIONS_HPP
3 
4 /******************************************************************************
5 ** Copyright © 2002 by J.M.McGuiness, coder@hussar.me.uk
6 **
7 ** This library is free software; you can redistribute it and/or
8 ** modify it under the terms of the GNU Lesser General Public
9 ** License as published by the Free Software Foundation; either
10 ** version 2.1 of the License, or (at your option) any later version.
11 **
12 ** This library is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ** Lesser General Public License for more details.
16 **
17 ** You should have received a copy of the GNU Lesser General Public
18 ** License along with this library; if not, write to the Free Software
19 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 // DESCRIPTION:
22 // Utilities to convert from Unicode to ASCII and back. Also functions that
23 // do the conversion or nothing, depending upon the definition of "_UNICODE".
24 
25 #include "ttypes.hpp"
26 
27 #include <locale>
28 #include <memory>
29 
30 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE {
31 
32  struct widen : public std::unary_function<char,wchar_t> {
34  : ct(std::use_facet<std::ctype<wchar_t> >(std::locale())) {
35  }
36  wchar_t operator()(char c) const {
37  return ct.widen(c);
38  }
39 
40  private:
41  std::ctype<wchar_t> const & ct;
42  };
43 
44  struct narrow : public std::unary_function<wchar_t,char> {
46  : ct(std::use_facet<std::ctype<wchar_t> >(std::locale())) {
47  }
48  char operator()(wchar_t c) const {
49  return ct.narrow(c,' ');
50  }
51 
52  private:
53  std::ctype<wchar_t> const & ct;
54  };
55 
56  inline std::wstring __fastcall
57  StringToWString(const std::string &in) {
58  std::wstring ret(in.size(), std::wstring::value_type(0));
59  // M$ Broke the declaration of "std::use_facet()" by adding extra, non-default arguments...
60  // So we have to hack around it...
61 #if defined(_MSC_VER) && (_MSC_VER < 1300)
62  std::ctype<wchar_t>().widen(in.data(),in.data()+in.size(),&(*ret.begin()));
63 #else
64  std::transform(in.begin(),in.end(),ret.begin(),widen());
65 #endif // _MSC_VER
66  return ret;
67  }
68 
69  inline std::string __fastcall
71  const std::wstring &in
72 #if defined(_MSC_VER) && (_MSC_VER < 1300)
73  ,const char dflt='@'
74 #endif // _MSC_VER
75  ) {
76  std::string ret(in.size(), std::string::value_type(0));
77  // M$ Broke the declaration of "std::use_facet()" by adding extra, non-default arguments...
78  // So we have to hack around it...
79 #if defined(_MSC_VER) && (_MSC_VER < 1300)
80  std::ctype<wchar_t>().narrow(in.data(),in.data()+in.size(),dflt,&(*ret.begin()));
81 #else
82  std::transform(in.begin(),in.end(),ret.begin(),narrow());
83 #endif // _MSC_VER
84  return ret;
85  }
86 
87  inline tstring __fastcall
88  WStringToTString(const std::wstring &in)
89 #ifdef _UNICODE
90  noexcept(true) {
91  return in;
92 #else
93  {
94  return WStringToString(in);
95 #endif // _UNICODE
96  }
97 
98  inline std::wstring __fastcall
100 #ifdef _UNICODE
101  noexcept(true) {
102  return in;
103 #else
104  {
105  return StringToWString(in);
106 #endif // _UNICODE
107  }
108 
109  inline tstring __fastcall
110  StringToTString(const std::string &in)
111 #ifdef _UNICODE
112  {
113  return StringToWString(in);
114 #else
115  noexcept(true) {
116  return in;
117 #endif // _UNICODE
118  }
119 
120  inline std::string __fastcall
122 #ifdef _UNICODE
123  {
124  return WStringToString(in);
125 #else
126  noexcept(true) {
127  return in;
128 #endif // _UNICODE
129  }
130 
131 } }
132 
133 #ifdef _MSC_VER
134 
135 #pragma warning(default:4284)
136 
137 #endif // _MSC_VER
138 
139 #endif