aGrUM  0.13.2
cast_unicode_inl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
28 #ifndef CAST_UNICODE_H
29 #define CAST_UNICODE_H
30 
31 #include <iostream>
32 #include <locale>
33 #include <sstream>
34 #include <string>
35 
37 
38 INLINE std::wstring widen(const std::string& str) {
39  std::wostringstream wstm;
40  const std::ctype< wchar_t >& ctfacet =
41  std::use_facet< std::ctype< wchar_t > >(wstm.getloc());
42 
43  for (size_t i = 0; i < str.size(); ++i) {
44  wstm << ctfacet.widen(str[i]);
45  }
46 
47  return wstm.str();
48 }
49 
50 INLINE std::string narrow(const std::wstring& str) {
51  std::ostringstream stm;
52  const std::ctype< char >& ctfacet =
53  std::use_facet< std::ctype< char > >(stm.getloc());
54 
55  for (std::size_t i = 0; i < str.size(); ++i) {
56  stm << ctfacet.narrow(str[i], 0);
57  }
58 
59  return stm.str();
60 }
61 
62 #endif // CAST_UNICODE_H
INLINE std::string narrow(const std::wstring &str)
Cast a std::wstring into a std::string.
INLINE std::wstring widen(const std::string &str)
Cast a std::string into a std::wstring.
Unicode helper functions.