@@ -22,22 +22,45 @@ with this program; if not, write to the Free Software Foundation, Inc.,
22
22
#include " numeric.h"
23
23
#include " log.h"
24
24
25
+ #include " sha1.h"
25
26
#include " base64.h"
26
27
#include " hex.h"
27
- #include " sha1.h"
28
28
#include " ../porting.h"
29
29
30
30
#include < algorithm>
31
31
#include < sstream>
32
32
#include < iomanip>
33
33
#include < map>
34
34
35
+ static bool parseHexColorString (const std::string &value, video::SColor &color);
36
+ static bool parseNamedColorString (const std::string &value, video::SColor &color);
37
+
38
+
39
+ // You must free the returned string!
40
+ // The returned string is allocated using new
41
+ wchar_t *narrow_to_wide_c (const char *str)
42
+ {
43
+ wchar_t * nstr = 0 ;
35
44
#if defined(_WIN32)
36
- #include < windows.h> // MultiByteToWideChar
45
+ int nResult = MultiByteToWideChar (CP_UTF8, 0 , (LPCSTR) str, -1 , 0 , 0 );
46
+ if (nResult == 0 ) {
47
+ errorstream<<" gettext: MultiByteToWideChar returned null" <<std::endl;
48
+ } else {
49
+ nstr = new wchar_t [nResult];
50
+ MultiByteToWideChar (CP_UTF8, 0 , (LPCSTR) str, -1 , (WCHAR *) nstr, nResult);
51
+ }
52
+ #else
53
+ size_t len = strlen (str);
54
+ nstr = new wchar_t [len+1 ];
55
+
56
+ std::wstring intermediate = narrow_to_wide (str);
57
+ memset (nstr, 0 , (len + 1 ) * sizeof (wchar_t ));
58
+ memcpy (nstr, intermediate.c_str (), len * sizeof (wchar_t ));
37
59
#endif
38
60
39
- static bool parseHexColorString (const std::string &value, video::SColor &color);
40
- static bool parseNamedColorString (const std::string &value, video::SColor &color);
61
+ return nstr;
62
+ }
63
+
41
64
42
65
#ifdef __ANDROID__
43
66
@@ -63,84 +86,55 @@ int wctomb(char *s, wchar_t wc)
63
86
64
87
int mbtowc (wchar_t *pwc, const char *s, size_t n)
65
88
{
66
- if (s == NULL || *s == ' \0 ' )
67
- return -1 ;
89
+ std::wstring intermediate = narrow_to_wide (s);
68
90
69
- const wchar_t *tmp = narrow_to_wide_c (s);
70
- bool success = tmp[0 ] != ' \0 ' ;
91
+ if (intermediate.length () > 0 ) {
92
+ *pwc = intermediate[0 ];
93
+ return 1 ;
94
+ }
95
+ else {
96
+ return -1 ;
97
+ }
98
+ }
71
99
72
- if (success)
73
- *pwc = tmp[ 0 ] ;
100
+ std::wstring narrow_to_wide ( const std::string &mbs) {
101
+ size_t wcl = mbs. size () ;
74
102
75
- delete tmp ;
103
+ std::wstring retval = L" " ;
76
104
77
- return success ? 1 : -1 ;
78
- }
105
+ for (unsigned int i = 0 ; i < wcl; i++) {
106
+ if (((unsigned char ) mbs[i] >31 ) &&
107
+ ((unsigned char ) mbs[i] < 127 )) {
79
108
80
- // You must free the returned string!
81
- const wchar_t *narrow_to_wide_c (const char *mbs)
82
- {
83
- size_t mbl = strlen (mbs);
84
- wchar_t *wcs = new wchar_t [mbl + 1 ];
85
-
86
- size_t i, dest_i = 0 ;
87
- for (i = 0 ; i < mbl; i++) {
88
- if (((unsigned char ) mbs[i] > 31 ) &&
89
- ((unsigned char ) mbs[i] < 127 )) {
90
- wcs[dest_i++] = wide_chars[(unsigned char ) mbs[i] - 32 ];
109
+ retval += wide_chars[(unsigned char ) mbs[i] -32 ];
91
110
}
92
111
// handle newline
93
112
else if (mbs[i] == ' \n ' ) {
94
- wcs[dest_i++] = L' \n ' ;
113
+ retval + = L' \n ' ;
95
114
}
96
115
}
97
- wcs[dest_i] = ' \0 ' ;
98
-
99
- return wcs;
100
- }
101
116
102
- #else
103
-
104
- // You must free the returned string!
105
- const wchar_t *narrow_to_wide_c (const char *mbs)
106
- {
107
- wchar_t *wcs = NULL ;
108
- #if defined(_WIN32)
109
- int nResult = MultiByteToWideChar (CP_UTF8, 0 , (LPCSTR) mbs, -1 , 0 , 0 );
110
- if (nResult == 0 ) {
111
- errorstream << " gettext: MultiByteToWideChar returned null" << std::endl;
112
- } else {
113
- wcs = new wchar_t [nResult];
114
- MultiByteToWideChar (CP_UTF8, 0 , (LPCSTR) mbs, -1 , (WCHAR *) wcs, nResult);
115
- }
116
- #else
117
- size_t wcl = mbstowcs (NULL , mbs, 0 );
118
- if (wcl == (size_t ) -1 )
119
- return NULL ;
120
- wcs = new wchar_t [wcl + 1 ];
121
- size_t l = mbstowcs (wcs, mbs, wcl);
122
- assert (l != (size_t ) -1 ); // Should never happen if the last call worked
123
- wcs[l] = ' \0 ' ;
124
- #endif
125
-
126
- return wcs;
117
+ return retval;
127
118
}
128
119
129
- #endif
120
+ #else // not Android
130
121
131
- std::wstring narrow_to_wide (const std::string& mbs)
122
+ std::wstring narrow_to_wide (const std::string & mbs)
132
123
{
133
124
size_t wcl = mbs.size ();
134
125
Buffer<wchar_t > wcs (wcl + 1 );
135
- size_t l = mbstowcs (*wcs, mbs.c_str (), wcl);
136
- if (l == (size_t )(-1 ))
126
+ size_t len = mbstowcs (*wcs, mbs.c_str (), wcl);
127
+ if (len == (size_t )(-1 ))
137
128
return L" <invalid multibyte string>" ;
138
- wcs[l ] = 0 ;
129
+ wcs[len ] = 0 ;
139
130
return *wcs;
140
131
}
141
132
133
+ #endif
134
+
142
135
#ifdef __ANDROID__
143
- std::string wide_to_narrow (const std::wstring& wcs) {
136
+
137
+ std::string wide_to_narrow (const std::wstring &wcs) {
144
138
size_t mbl = wcs.size ()*4 ;
145
139
146
140
std::string retval = " " ;
@@ -165,17 +159,18 @@ std::string wide_to_narrow(const std::wstring& wcs) {
165
159
166
160
return retval;
167
161
}
168
- #else
169
- std::string wide_to_narrow (const std::wstring& wcs)
162
+
163
+ #else // not Android
164
+
165
+ std::string wide_to_narrow (const std::wstring &wcs)
170
166
{
171
- size_t mbl = wcs.size ()* 4 ;
167
+ size_t mbl = wcs.size () * 4 ;
172
168
SharedBuffer<char > mbs (mbl+1 );
173
- size_t l = wcstombs (*mbs, wcs.c_str (), mbl);
174
- if (l == (size_t )(-1 )) {
169
+ size_t len = wcstombs (*mbs, wcs.c_str (), mbl);
170
+ if (len == (size_t )(-1 ))
175
171
return " Character conversion failed!" ;
176
- }
177
172
else
178
- mbs[l ] = 0 ;
173
+ mbs[len ] = 0 ;
179
174
return *mbs;
180
175
}
181
176
@@ -188,7 +183,7 @@ std::string wide_to_narrow(const std::wstring& wcs)
188
183
// compatibility with password-less players).
189
184
std::string translatePassword (std::string playername, std::wstring password)
190
185
{
191
- if (password.length () == 0 )
186
+ if (password.length () == 0 )
192
187
return " " ;
193
188
194
189
std::string slt = playername + wide_to_narrow (password);
0 commit comments