1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2019-2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "nel/misc/string_common.h"
23 #include "nel/misc/sstring.h"
24 #include "nel/misc/utf_string_view.h"
35 string
addSlashR(const string
&str
)
38 // replace \n with \r\n
39 for (uint i
= 0; i
< str
.size(); i
++)
41 if (str
[i
] == '\n' && i
> 0 && str
[i
- 1] != '\r')
45 formatedStr
+= str
[i
];
50 string
removeSlashR(const string
&str
)
54 for (uint i
= 0; i
< str
.size(); i
++)
57 formatedStr
+= str
[i
];
62 bool fromString(const std::string
&str
, bool &val
)
64 if (str
.length() == 1)
66 const char c
= str
[0];
93 std::string strl
= toLowerAscii(str
);
94 if (strl
== "true" || strl
== "yes")
98 else if (strl
== "false" || strl
== "no")
112 #if defined(NL_OS_WINDOWS)
114 std::string
winWideToCp(const wchar_t *str
, size_t len
, UINT cp
)
119 return std::string();
121 // Convert from wide to codepage
122 char *tmp
= (char *)_malloca((len
+ 1) * 4);
124 return std::string();
125 int tmpLen
= WideCharToMultiByte(cp
, 0,
127 tmp
, (int)((len
+ 1) * 4),
132 return std::string();
135 std::string res
= tmp
;
140 std::string
winCpToCp(const char *str
, size_t len
, UINT srcCp
, UINT dstCp
)
145 return std::string();
147 // First convert from codepage to wide
148 wchar_t *tmp
= (wchar_t *)_malloca((len
+ 1) * 4);
150 return std::string();
151 int tmpLen
= MultiByteToWideChar(srcCp
, 0,
152 str
, (int)(len
+ 1), /* include null-termination */
153 tmp
, (int)((len
+ 1) * 2));
157 return std::string();
160 // Then convert from wide to codepage
161 std::string res
= winWideToCp(tmp
, (size_t)tmpLen
- 1, dstCp
); /* tmpLen includes null-term */
166 std::wstring
winCpToWide(const char *str
, size_t len
, UINT cp
)
171 return std::wstring();
173 // Convert from codepage to wide
174 wchar_t *tmp
= (wchar_t *)_malloca((len
+ 1) * 4);
176 return std::wstring();
177 int tmpLen
= MultiByteToWideChar(cp
, 0,
178 str
, (int)(len
+ 1), /* include null-termination */
179 tmp
, (int)((len
+ 1) * 2));
183 return std::wstring();
186 std::wstring res
= tmp
;
193 // Convert local codepage to UTF-8
194 // On Windows, the local codepage is undetermined
195 // On Linux, the local codepage is always UTF-8 (no-op)
196 std::string
mbcsToUtf8(const char *str
, size_t len
)
198 #if defined(NL_OS_WINDOWS)
199 UINT codePage
= GetACP();
200 // Windows 10 allows setting the local codepage to UTF-8
201 if (codePage
== CP_UTF8
) /* 65001 */
203 return winCpToCp(str
, len
, CP_ACP
, CP_UTF8
);
205 return str
; /* no-op */
209 std::string
mbcsToUtf8(const std::string
&str
)
211 #if defined(NL_OS_WINDOWS)
214 UINT codePage
= GetACP();
215 // Windows 10 allows setting the local codepage to UTF-8
216 if (codePage
== CP_UTF8
) /* 65001 */
218 return winCpToCp(str
.c_str(), str
.size(), CP_ACP
, CP_UTF8
);
220 return str
; /* no-op */
224 // Convert wide codepage to UTF-8
225 // On Windows, the wide codepage is UTF-16
226 // On Linux, the wide codepage is UTF-32
227 std::string
wideToUtf8(const wchar_t *str
, size_t len
)
229 #if defined(NL_OS_WINDOWS)
230 return winWideToCp(str
, len
, CP_UTF8
);
232 return CUtfStringView(str
, len
).toUtf8();
236 std::string
wideToUtf8(const std::wstring
&str
)
238 return wideToUtf8(str
.c_str(), str
.size());
241 // Convert UTF-8 to wide character set
242 std::wstring
utf8ToWide(const char *str
, size_t len
)
244 #if defined(NL_OS_WINDOWS)
245 return winCpToWide(str
, len
, CP_UTF8
); // UTF-16
247 return CUtfStringView(str
, len
).toWide(); // UTF-32
251 std::wstring
utf8ToWide(const std::string
&str
)
253 return utf8ToWide(str
.c_str(), str
.size());
256 // Convert UTF-8 to local multibyte character set
257 std::string
utf8ToMbcs(const char *str
, size_t len
)
259 #if defined(NL_OS_WINDOWS)
260 UINT codePage
= GetACP();
261 // Windows 10 allows setting the local codepage to UTF-8
262 if (codePage
== CP_UTF8
) /* 65001 */
264 return winCpToCp(str
, len
, CP_UTF8
, CP_ACP
);
266 return str
; /* no-op */
270 std::string
utf8ToMbcs(const std::string
&str
)
272 #if defined(NL_OS_WINDOWS)
275 UINT codePage
= GetACP();
276 // Windows 10 allows setting the local codepage to UTF-8
277 if (codePage
== CP_UTF8
) /* 65001 */
279 return winCpToCp(str
.c_str(), str
.size(), CP_UTF8
, CP_ACP
);
281 return str
; /* no-op */
285 // Convert wide to local multibyte character set
286 std::string
wideToMbcs(const wchar_t *str
, size_t len
)
288 #if defined(NL_OS_WINDOWS)
289 return winWideToCp(str
, len
, CP_ACP
);
291 return wideToUtf8(str
, len
);
295 std::string
wideToMbcs(const std::wstring
&str
)
297 #if defined(NL_OS_WINDOWS)
298 return winWideToCp(str
.c_str(), str
.size(), CP_ACP
);
300 return wideToUtf8(str
);
304 // Convert local multibyte to wide character set
305 std::wstring
mbcsToWide(const char *str
, size_t len
)
307 #if defined(NL_OS_WINDOWS)
308 return winCpToWide(str
, len
, CP_ACP
);
310 return utf8ToWide(str
, len
);
314 std::wstring
mbcsToWide(const std::string
&str
)
316 #if defined(NL_OS_WINDOWS)
317 return winCpToWide(str
.c_str(), str
.size(), CP_ACP
);
319 return utf8ToWide(str
);