root/core/unicode_conversions.hpp

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. StringToWString
  2. WStringToString
  3. WStringToTString
  4. TStringToWString
  5. StringToTString
  6. noexcept
  7. TStringToString
  8. noexcept

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

/* [<][>][^][v][top][bottom][index][help] */