VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gconvert.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program 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
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gconvert.h
19 ///
20 
21 #ifndef G_CONVERT_H
22 #define G_CONVERT_H
23 
24 #include "gdef.h"
25 #include "gexception.h"
26 #include <string>
27 
28 namespace G
29 {
30  class Convert ;
31  class ConvertImp ;
32 }
33 
34 /// \class G::Convert
35 /// A static class which provides string encoding conversion functions. Supported
36 /// encodings are "ANSI" (CP-1252/ISO-8859-1) char, UTF-16 wchar, and UTF-8
37 /// multi-byte char. Conversions that can fail take a ThrowOnError parameter
38 /// which is used to add context information to the G::Convert::Error exception
39 /// that is thrown.
40 ///
41 /// Eg:
42 /// \code
43 /// std::string to_utf8( const std::wstring & wide_input )
44 /// {
45 /// G::Convert::utf8 utf8_result ;
46 /// G::Convert::convert( utf8_result , wide_input ) ;
47 /// return utf8_result.s ;
48 /// }
49 /// std::string to_ansi( const std::wstring & wide_input )
50 /// {
51 /// std::string ansi_result ;
52 /// G::Convert::convert( ansi_result , wide_input , G::Convert::ThrowOnError("to_ansi") ) ;
53 /// return ansi_result ;
54 /// }
55 /// \endcode
56 ///
58 {
59 public:
60  G_EXCEPTION_CLASS( Error , "string conversion error" ) ;
61  typedef std::basic_string<TCHAR> tstring ;
62 
63  struct utf8 /// A string wrapper that indicates UTF-8 encoding.
64  {
65  utf8() {}
66  explicit utf8( const std::string & s_ ) : s(s_) {}
67  std::string s ;
68  } ;
69 
70  struct ThrowOnError /// Holds context information which convert() adds to the exception when it fails.
71  {
72  ThrowOnError() {}
73  explicit ThrowOnError( const std::string & context_ ) : context(context_) {}
74  std::string context ;
75  } ;
76 
77  static void convert( utf8 & utf_out , const std::string & in_ ) ;
78  ///< Converts between string types/encodings:
79  ///< ansi to utf8.
80 
81  static void convert( utf8 & utf_out , const utf8 & in_ ) ;
82  ///< Converts between string types/encodings:
83  ///< utf8 to utf8.
84 
85  static void convert( utf8 & utf_out , const std::wstring & in_ ) ;
86  ///< Converts between string types/encodings:
87  ///< utf16 to utf8.
88 
89  static void convert( std::string & ansi_out , const std::string & in_ ) ;
90  ///< Converts between string types/encodings:
91  ///< ansi to ansi.
92 
93  static void convert( std::string & ansi_out , const utf8 & in_ , const ThrowOnError & ) ;
94  ///< Converts between string types/encodings:
95  ///< utf8 to ansi.
96 
97  static void convert( std::string & ansi_out , const std::wstring & in_ , const ThrowOnError & ) ;
98  ///< Converts between string types/encodings:
99  ///< utf16 to ansi.
100 
101  static void convert( std::wstring & wide_out , const std::string & ansi_in ) ;
102  ///< Converts between string types/encodings:
103  ///< ansi to utf16.
104 
105  static void convert( std::wstring & wide_out , const utf8 & utf_in ) ;
106  ///< Converts between string types/encodings:
107  ///< utf8 to utf16.
108 
109  static void convert( std::wstring & wide_out , const std::wstring & wide_in ) ;
110  ///< Converts between string types/encodings:
111  ///< utf8 to utf16.
112 
113  static void convert( std::string & ansi_out , const std::string & in_ , const ThrowOnError & ) ;
114  ///< An overload for TCHAR shenanigans on windows.
115  ///< Converts between string types/encodings:
116  ///< ansi to ansi.
117 
118 private:
119  static std::string narrow( const std::wstring & s , bool is_utf8 , const std::string & = std::string() ) ;
120  static std::wstring widen( const std::string & s , bool is_utf8 , const std::string & = std::string() ) ;
121  Convert() ; // not implemented
122 } ;
123 
124 #endif
static void convert(utf8 &utf_out, const std::string &in_)
Converts between string types/encodings: ansi to utf8.
Definition: gconvert.cpp:44
A static class which provides string encoding conversion functions.
Definition: gconvert.h:57
Holds context information which convert() adds to the exception when it fails.
Definition: gconvert.h:70
A string wrapper that indicates UTF-8 encoding.
Definition: gconvert.h:63