Version: 1.0.0
string_converter.cpp
Go to the documentation of this file.
1 #include "wx/database/wxprec.h"
2 
3 // Default the encoding converter
5  : m_Encoding(wxT("UTF-8"))
6 {
7 }
8 
10  : m_Encoding(charset)
11 {
12 }
13 
14 void wxDatabaseStringConverter::SetEncoding(wxFontEncoding encoding)
15 {
16  m_Encoding = wxCSConv(encoding);
17 }
18 
19 void wxDatabaseStringConverter::SetEncoding(const wxCSConv* conv)
20 {
21  m_Encoding = *conv;
22 }
23 
24 const wxCharBuffer wxDatabaseStringConverter::ConvertToUnicodeStream(const wxString& inputString)
25 {
26 #if wxUSE_UNICODE
27  // all the following do not work for Asian characters
28  //return wxConvUTF8.cWC2MB(inputString.wc_str(*wxConvCurrent));
29  //return wxConvUTF8.cWX2MB(inputString.wc_str(*wxConvCurrent));
30  //return wxCharBuffer(wxConvCurrent->cWX2WC(inputString.wc_str()));
31  //return inputString.c_str(); //this only works under some test cases
32  return wxSafeConvertWX2MB(inputString.wc_str());
33  //from docs for easy reference only:
34  //To print a wxChar buffer to a non-Unicode stdout:
35  //printf("Data: %s\n", (const char*) wxConvCurrent->cWX2MB(unicode_data));
36 #else
37  wxString str(inputString.wc_str(*wxConvCurrent), wxConvUTF8);
38  return str.mb_str();
39 #endif
40 }
41 
42 size_t wxDatabaseStringConverter::GetEncodedStreamLength(const wxString& inputString)
43 {
44  if(inputString == _T(""))
45  return 0;
46 #if wxUSE_UNICODE
47  size_t length = wxConvUTF8.WC2MB(NULL, inputString.wc_str(), (unsigned int)0);
48 #else
49  wxString str(inputString.wc_str(*wxConvCurrent), wxConvUTF8);
50  size_t length = str.Length();
51 #endif
52  if (length == 0)
53  {
54  wxCharBuffer tempCharBuffer = ConvertToUnicodeStream(inputString);
55  length = wxStrlen((wxChar*)(const char*)tempCharBuffer);
56  }
57 
58  return length;
59 }
60 
61 wxString wxDatabaseStringConverter::ConvertFromUnicodeStream(const char* inputBuffer)
62 {
63  wxString strReturn(wxConvUTF8.cMB2WC(inputBuffer), *wxConvCurrent);
64  // If the UTF-8 conversion didn't return anything, then try the default unicode conversion
65  if (strReturn == wxEmptyString)
66  strReturn << wxString(inputBuffer, *wxConvCurrent);
67 
68  return strReturn;
69 }
70 
71 wxString wxDatabaseStringConverter::ConvertFromUnicodeStream(const wxChar* inputBuffer)
72 {
73  wxString strReturn( wxConvUTF8.cWC2WX(inputBuffer),*wxConvCurrent);
74  // If the UTF-8 conversion didn't return anything, then try the default unicode conversion
75  if(strReturn==wxEmptyString)
76  strReturn<<wxString(inputBuffer,*wxConvCurrent);
77  return strReturn;
78 }
79 
80 const wxCharBuffer wxDatabaseStringConverter::ConvertToUnicodeStream(const wxString& inputString, const char* WXUNUSED(encoding))
81 {
82 #if wxUSE_UNICODE
83  // all the following do not work for Asian character issue
84  //return wxConvUTF8.cWC2MB(inputString.wc_str(*wxConvCurrent));
85  //return wxConvUTF8.cWX2MB(inputString.wc_str(*wxConvCurrent));
86  //return wxCharBuffer(wxConvCurrent->cWX2WC(inputString.wc_str()));
87  //return inputString.c_str(); //this only works under some test cases
88  return wxSafeConvertWX2MB(inputString.wc_str());
89  //from docs for easy reference only:
90  //To print a wxChar buffer to a non-Unicode stdout:
91  //printf("Data: %s\n", (const char*) wxConvCurrent->cWX2MB(unicode_data));
92 #else
93  wxString str(inputString.wc_str(*wxConvCurrent), wxConvUTF8);
94  return str.mb_str();
95 #endif
96 }
97 
98 size_t wxDatabaseStringConverter::GetEncodedStreamLength(const wxString& inputString, const char* encoding)
99 {
100  if(inputString == _T(""))
101  return 0;
102 #if wxUSE_UNICODE
103  size_t length = wxConvUTF8.WC2MB(NULL, inputString.wc_str(), (unsigned int)0);
104 #else
105  const wchar_t* str = inputString.wc_str(*wxConvCurrent);
106  size_t length = wxConvUTF8.WC2MB(NULL, str, (unsigned int)0);
107 #endif
108  if (length == 0)
109  {
110  wxCharBuffer tempCharBuffer = wxDatabaseStringConverter::ConvertToUnicodeStream(inputString, encoding);
111  length = wxStrlen((wxChar*)(const char*)tempCharBuffer);
112  }
113 
114  return length;
115 }
116 
117 wxString wxDatabaseStringConverter::ConvertFromUnicodeStream(const char* inputBuffer, const char* WXUNUSED(encoding))
118 {
119  wxString strReturn(wxConvUTF8.cMB2WC(inputBuffer), *wxConvCurrent);
120  // If the UTF-8 conversion didn't return anything, then try the default unicode conversion
121  if (strReturn == wxEmptyString)
122  strReturn << wxString(inputBuffer, *wxConvCurrent);
123 
124  return strReturn;
125 }
126 
127 wxString wxDatabaseStringConverter::ConvertFromUnicodeStream(const wxChar* inputBuffer, const char* WXUNUSED(encoding))
128 {
129  wxString strReturn( wxConvUTF8.cWC2WX(inputBuffer),*wxConvCurrent);
130  // If the UTF-8 conversion didn't return anything, then try the default unicode conversion
131  if(strReturn==wxEmptyString)
132  strReturn<<wxString(inputBuffer,*wxConvCurrent);
133  return strReturn;
134 }
135 
136 
137 
void SetEncoding(wxFontEncoding encoding)
virtual size_t GetEncodedStreamLength(const wxString &inputString)
virtual wxString ConvertFromUnicodeStream(const char *inputBuffer)
virtual const wxCharBuffer ConvertToUnicodeStream(const wxString &inputString)