summaryrefslogtreecommitdiffstats
path: root/Source/kwsys/EncodingCXX.cxx
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2016-12-05 14:55:34 (GMT)
committerBrad King <brad.king@kitware.com>2016-12-05 14:55:34 (GMT)
commit7f307d9ea6e77ee6fd7081d464314a64cbf64f32 (patch)
treea165c1c500907a74c6046dbbebcbbd68d4fda078 /Source/kwsys/EncodingCXX.cxx
parentd59010e47fa953afb7f638fbab1a65195478d3fa (diff)
parent1b50bd3f5c1f608a84df89c2f5e187a53f0be31d (diff)
downloadCMake-7f307d9ea6e77ee6fd7081d464314a64cbf64f32.zip
CMake-7f307d9ea6e77ee6fd7081d464314a64cbf64f32.tar.gz
CMake-7f307d9ea6e77ee6fd7081d464314a64cbf64f32.tar.bz2
Merge branch 'upstream-KWSys' into update-kwsys
* upstream-KWSys: KWSys 2016-12-02 (4967ccc0)
Diffstat (limited to 'Source/kwsys/EncodingCXX.cxx')
-rw-r--r--Source/kwsys/EncodingCXX.cxx60
1 files changed, 58 insertions, 2 deletions
diff --git a/Source/kwsys/EncodingCXX.cxx b/Source/kwsys/EncodingCXX.cxx
index 5c58bcb..e904c1a 100644
--- a/Source/kwsys/EncodingCXX.cxx
+++ b/Source/kwsys/EncodingCXX.cxx
@@ -125,12 +125,68 @@ char const* const* Encoding::CommandLineArguments::argv() const
std::wstring Encoding::ToWide(const std::string& str)
{
- return ToWide(str.c_str());
+ std::wstring wstr;
+#if defined(_WIN32)
+ const int wlength = MultiByteToWideChar(
+ KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.data(), int(str.size()), NULL, 0);
+ if (wlength > 0) {
+ wchar_t* wdata = new wchar_t[wlength];
+ int r = MultiByteToWideChar(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.data(),
+ int(str.size()), wdata, wlength);
+ if (r > 0) {
+ wstr = std::wstring(wdata, wlength);
+ }
+ delete[] wdata;
+ }
+#else
+ size_t pos = 0;
+ size_t nullPos = 0;
+ do {
+ if (pos < str.size() && str.at(pos) != '\0') {
+ wstr += ToWide(str.c_str() + pos);
+ }
+ nullPos = str.find('\0', pos);
+ if (nullPos != str.npos) {
+ pos = nullPos + 1;
+ wstr += wchar_t('\0');
+ }
+ } while (nullPos != str.npos);
+#endif
+ return wstr;
}
std::string Encoding::ToNarrow(const std::wstring& str)
{
- return ToNarrow(str.c_str());
+ std::string nstr;
+#if defined(_WIN32)
+ int length =
+ WideCharToMultiByte(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.c_str(),
+ int(str.size()), NULL, 0, NULL, NULL);
+ if (length > 0) {
+ char* data = new char[length];
+ int r =
+ WideCharToMultiByte(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.c_str(),
+ int(str.size()), data, length, NULL, NULL);
+ if (r > 0) {
+ nstr = std::string(data, length);
+ }
+ delete[] data;
+ }
+#else
+ size_t pos = 0;
+ size_t nullPos = 0;
+ do {
+ if (pos < str.size() && str.at(pos) != '\0') {
+ nstr += ToNarrow(str.c_str() + pos);
+ }
+ nullPos = str.find(wchar_t('\0'), pos);
+ if (nullPos != str.npos) {
+ pos = nullPos + 1;
+ nstr += '\0';
+ }
+ } while (nullPos != str.npos);
+#endif
+ return nstr;
}
std::wstring Encoding::ToWide(const char* cstr)