2016-11-09 14:12:29 +00:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
|
2013-11-21 17:47:57 +00:00
|
|
|
#include "kwsysPrivate.h"
|
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
2016-11-09 14:12:29 +00:00
|
|
|
#pragma warning(disable : 4786)
|
2013-11-21 17:47:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include KWSYS_HEADER(Encoding.hxx)
|
2013-12-19 14:39:26 +00:00
|
|
|
#include KWSYS_HEADER(Encoding.h)
|
2013-11-21 17:47:57 +00:00
|
|
|
|
2016-12-02 14:36:32 +00:00
|
|
|
#include <algorithm>
|
2015-08-28 18:50:49 +00:00
|
|
|
#include <iostream>
|
2013-11-21 17:47:57 +00:00
|
|
|
#include <locale.h>
|
2013-12-19 14:39:26 +00:00
|
|
|
#include <stdlib.h>
|
2016-11-09 14:12:29 +00:00
|
|
|
#include <string.h>
|
2013-11-21 17:47:57 +00:00
|
|
|
|
|
|
|
// Work-around CMake dependency scanning limitation. This must
|
|
|
|
// duplicate the above list of headers.
|
|
|
|
#if 0
|
2016-11-09 14:12:29 +00:00
|
|
|
#include "Encoding.h.in"
|
|
|
|
#include "Encoding.hxx.in"
|
2013-11-21 17:47:57 +00:00
|
|
|
#endif
|
|
|
|
|
2016-11-09 14:12:29 +00:00
|
|
|
static const unsigned char helloWorldStrings[][32] = {
|
2013-11-21 17:47:57 +00:00
|
|
|
// English
|
2016-11-09 14:12:29 +00:00
|
|
|
{ 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', 0 },
|
2013-11-21 17:47:57 +00:00
|
|
|
// Japanese
|
2016-11-09 14:12:29 +00:00
|
|
|
{ 0xE3, 0x81, 0x93, 0xE3, 0x82, 0x93, 0xE3, 0x81, 0xAB, 0xE3, 0x81,
|
|
|
|
0xA1, 0xE3, 0x81, 0xAF, 0xE4, 0xB8, 0x96, 0xE7, 0x95, 0x8C, 0 },
|
|
|
|
// Arabic
|
|
|
|
{ 0xD9, 0x85, 0xD8, 0xB1, 0xD8, 0xAD, 0xD8, 0xA8, 0xD8, 0xA7, 0x20, 0xD8,
|
|
|
|
0xA7, 0xD9, 0x84, 0xD8, 0xB9, 0xD8, 0xA7, 0xD9, 0x84, 0xD9, 0x85, 0 },
|
2013-11-21 17:47:57 +00:00
|
|
|
// Yiddish
|
2016-11-09 14:12:29 +00:00
|
|
|
{ 0xD7, 0x94, 0xD7, 0xA2, 0xD7, 0x9C, 0xD7, 0x90, 0x20, 0xD7,
|
|
|
|
0x95, 0xD7, 0x95, 0xD7, 0xA2, 0xD7, 0x9C, 0xD7, 0x98, 0 },
|
2013-11-21 17:47:57 +00:00
|
|
|
// Russian
|
2016-11-09 14:12:29 +00:00
|
|
|
{ 0xD0, 0xBF, 0xD1, 0x80, 0xD0, 0xB8, 0xD0, 0xB2, 0xD0, 0xB5,
|
|
|
|
0xD1, 0x82, 0x20, 0xD0, 0xBC, 0xD0, 0xB8, 0xD1, 0x80, 0 },
|
2013-11-21 17:47:57 +00:00
|
|
|
// Latin
|
2016-11-09 14:12:29 +00:00
|
|
|
{ 0x4D, 0x75, 0x6E, 0x64, 0x75, 0x73, 0x20, 0x73, 0x61, 0x6C, 0x76, 0x65,
|
|
|
|
0 },
|
2013-11-21 17:47:57 +00:00
|
|
|
// Swahili
|
2016-11-09 14:12:29 +00:00
|
|
|
{ 0x68, 0x75, 0x6A, 0x61, 0x6D, 0x62, 0x6F, 0x20, 0x44, 0x75, 0x6E, 0x69,
|
|
|
|
0x61, 0 },
|
2013-11-21 17:47:57 +00:00
|
|
|
// Icelandic
|
2016-11-09 14:12:29 +00:00
|
|
|
{ 0x48, 0x61, 0x6C, 0x6C, 0xC3, 0xB3, 0x20, 0x68, 0x65, 0x69, 0x6D, 0x75,
|
|
|
|
0x72, 0 },
|
|
|
|
{ 0 }
|
2013-11-21 17:47:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int testHelloWorldEncoding()
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2016-11-09 14:12:29 +00:00
|
|
|
for (int i = 0; helloWorldStrings[i][0] != 0; i++) {
|
2013-11-21 17:47:57 +00:00
|
|
|
std::string str = reinterpret_cast<const char*>(helloWorldStrings[i]);
|
|
|
|
std::cout << str << std::endl;
|
|
|
|
std::wstring wstr = kwsys::Encoding::ToWide(str);
|
|
|
|
std::string str2 = kwsys::Encoding::ToNarrow(wstr);
|
2013-12-19 14:39:26 +00:00
|
|
|
wchar_t* c_wstr = kwsysEncoding_DupToWide(str.c_str());
|
|
|
|
char* c_str2 = kwsysEncoding_DupToNarrow(c_wstr);
|
2016-11-09 14:12:29 +00:00
|
|
|
if (!wstr.empty() && (str != str2 || strcmp(c_str2, str.c_str()))) {
|
2013-11-21 17:47:57 +00:00
|
|
|
std::cout << "converted string was different: " << str2 << std::endl;
|
2013-12-19 14:39:26 +00:00
|
|
|
std::cout << "converted string was different: " << c_str2 << std::endl;
|
2013-11-21 17:47:57 +00:00
|
|
|
ret++;
|
2016-11-09 14:12:29 +00:00
|
|
|
}
|
2013-12-19 14:39:26 +00:00
|
|
|
free(c_wstr);
|
|
|
|
free(c_str2);
|
2016-11-09 14:12:29 +00:00
|
|
|
}
|
2013-11-21 17:47:57 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int testRobustEncoding()
|
|
|
|
{
|
|
|
|
// test that the conversion functions handle invalid
|
|
|
|
// unicode correctly/gracefully
|
|
|
|
|
|
|
|
int ret = 0;
|
2016-11-09 14:12:29 +00:00
|
|
|
char cstr[] = { (char)-1, 0 };
|
2013-11-21 17:47:57 +00:00
|
|
|
// this conversion could fail
|
|
|
|
std::wstring wstr = kwsys::Encoding::ToWide(cstr);
|
|
|
|
|
|
|
|
wstr = kwsys::Encoding::ToWide(NULL);
|
2016-11-09 14:12:29 +00:00
|
|
|
if (wstr != L"") {
|
2013-11-21 17:47:57 +00:00
|
|
|
const wchar_t* wcstr = wstr.c_str();
|
|
|
|
std::cout << "ToWide(NULL) returned";
|
2016-11-09 14:12:29 +00:00
|
|
|
for (size_t i = 0; i < wstr.size(); i++) {
|
2013-11-21 17:47:57 +00:00
|
|
|
std::cout << " " << std::hex << (int)wcstr[i];
|
2016-11-09 14:12:29 +00:00
|
|
|
}
|
2013-11-21 17:47:57 +00:00
|
|
|
std::cout << std::endl;
|
|
|
|
ret++;
|
2016-11-09 14:12:29 +00:00
|
|
|
}
|
2013-11-21 17:47:57 +00:00
|
|
|
wstr = kwsys::Encoding::ToWide("");
|
2016-11-09 14:12:29 +00:00
|
|
|
if (wstr != L"") {
|
2013-11-21 17:47:57 +00:00
|
|
|
const wchar_t* wcstr = wstr.c_str();
|
|
|
|
std::cout << "ToWide(\"\") returned";
|
2016-11-09 14:12:29 +00:00
|
|
|
for (size_t i = 0; i < wstr.size(); i++) {
|
2013-11-21 17:47:57 +00:00
|
|
|
std::cout << " " << std::hex << (int)wcstr[i];
|
2016-11-09 14:12:29 +00:00
|
|
|
}
|
2013-11-21 17:47:57 +00:00
|
|
|
std::cout << std::endl;
|
|
|
|
ret++;
|
2016-11-09 14:12:29 +00:00
|
|
|
}
|
2013-11-21 17:47:57 +00:00
|
|
|
|
2016-03-29 18:28:37 +00:00
|
|
|
#ifdef _WIN32
|
2013-11-21 17:47:57 +00:00
|
|
|
// 16 bit wchar_t - we make an invalid surrogate pair
|
2016-11-09 14:12:29 +00:00
|
|
|
wchar_t cwstr[] = { 0xD801, 0xDA00, 0 };
|
2013-11-21 17:47:57 +00:00
|
|
|
// this conversion could fail
|
|
|
|
std::string win_str = kwsys::Encoding::ToNarrow(cwstr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::string str = kwsys::Encoding::ToNarrow(NULL);
|
2016-11-09 14:12:29 +00:00
|
|
|
if (str != "") {
|
2013-11-21 17:47:57 +00:00
|
|
|
std::cout << "ToNarrow(NULL) returned " << str << std::endl;
|
|
|
|
ret++;
|
2016-11-09 14:12:29 +00:00
|
|
|
}
|
2013-11-21 17:47:57 +00:00
|
|
|
|
|
|
|
str = kwsys::Encoding::ToNarrow(L"");
|
2016-11-09 14:12:29 +00:00
|
|
|
if (wstr != L"") {
|
2013-11-21 17:47:57 +00:00
|
|
|
std::cout << "ToNarrow(\"\") returned " << str << std::endl;
|
|
|
|
ret++;
|
2016-11-09 14:12:29 +00:00
|
|
|
}
|
2013-11-21 17:47:57 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-12-02 14:36:32 +00:00
|
|
|
static int testWithNulls()
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
std::vector<std::string> strings;
|
|
|
|
strings.push_back(std::string("ab") + '\0' + 'c');
|
|
|
|
strings.push_back(std::string("d") + '\0' + '\0' + 'e');
|
|
|
|
strings.push_back(std::string() + '\0' + 'f');
|
|
|
|
strings.push_back(std::string() + '\0' + '\0' + "gh");
|
|
|
|
strings.push_back(std::string("ij") + '\0');
|
|
|
|
strings.push_back(std::string("k") + '\0' + '\0');
|
|
|
|
strings.push_back(std::string("\0\0\0\0", 4) + "lmn" +
|
|
|
|
std::string("\0\0\0\0", 4));
|
|
|
|
for (std::vector<std::string>::iterator it = strings.begin();
|
|
|
|
it != strings.end(); ++it) {
|
|
|
|
std::wstring wstr = kwsys::Encoding::ToWide(*it);
|
|
|
|
std::string str = kwsys::Encoding::ToNarrow(wstr);
|
|
|
|
std::string s(*it);
|
|
|
|
std::replace(s.begin(), s.end(), '\0', ' ');
|
|
|
|
std::cout << "'" << s << "' (" << it->size() << ")" << std::endl;
|
|
|
|
if (str != *it) {
|
|
|
|
std::replace(str.begin(), str.end(), '\0', ' ');
|
|
|
|
std::cout << "string with null was different: '" << str << "' ("
|
|
|
|
<< str.size() << ")" << std::endl;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-19 15:01:48 +00:00
|
|
|
static int testCommandLineArguments()
|
|
|
|
{
|
|
|
|
int status = 0;
|
|
|
|
|
2016-11-09 14:12:29 +00:00
|
|
|
char const* argv[2] = { "./app.exe", (char const*)helloWorldStrings[1] };
|
2013-12-19 15:01:48 +00:00
|
|
|
|
|
|
|
kwsys::Encoding::CommandLineArguments args(2, argv);
|
|
|
|
kwsys::Encoding::CommandLineArguments arg2 =
|
|
|
|
kwsys::Encoding::CommandLineArguments(args);
|
|
|
|
|
|
|
|
char const* const* u8_argv = args.argv();
|
2016-11-09 14:12:29 +00:00
|
|
|
for (int i = 0; i < args.argc(); i++) {
|
2013-12-19 15:01:48 +00:00
|
|
|
char const* u8_arg = u8_argv[i];
|
2016-11-09 14:12:29 +00:00
|
|
|
if (strcmp(argv[i], u8_arg) != 0) {
|
|
|
|
std::cout << "argv[" << i << "] " << argv[i] << " != " << u8_arg
|
|
|
|
<< std::endl;
|
2013-12-19 15:01:48 +00:00
|
|
|
status++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
kwsys::Encoding::CommandLineArguments args3 =
|
|
|
|
kwsys::Encoding::CommandLineArguments::Main(2, argv);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2013-11-21 17:47:57 +00:00
|
|
|
|
2017-04-19 11:58:28 +00:00
|
|
|
static int testToWindowsExtendedPath()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
int ret = 0;
|
|
|
|
if (kwsys::Encoding::ToWindowsExtendedPath(
|
|
|
|
"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") !=
|
|
|
|
L"\\\\?\\L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") {
|
|
|
|
std::cout << "Problem with ToWindowsExtendedPath "
|
|
|
|
<< "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\""
|
|
|
|
<< std::endl;
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kwsys::Encoding::ToWindowsExtendedPath(
|
|
|
|
"L:/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
|
|
|
|
L"\\\\?\\L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") {
|
|
|
|
std::cout << "Problem with ToWindowsExtendedPath "
|
|
|
|
<< "\"L:/Local Mojo/Hex Power Pack/Iffy Voodoo\"" << std::endl;
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kwsys::Encoding::ToWindowsExtendedPath(
|
|
|
|
"\\\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") !=
|
|
|
|
L"\\\\?\\UNC\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") {
|
|
|
|
std::cout << "Problem with ToWindowsExtendedPath "
|
|
|
|
<< "\"\\\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\""
|
|
|
|
<< std::endl;
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kwsys::Encoding::ToWindowsExtendedPath(
|
|
|
|
"//Foo/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
|
|
|
|
L"\\\\?\\UNC\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") {
|
|
|
|
std::cout << "Problem with ToWindowsExtendedPath "
|
|
|
|
<< "\"//Foo/Local Mojo/Hex Power Pack/Iffy Voodoo\""
|
|
|
|
<< std::endl;
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kwsys::Encoding::ToWindowsExtendedPath("//") != L"//") {
|
|
|
|
std::cout << "Problem with ToWindowsExtendedPath "
|
|
|
|
<< "\"//\"" << std::endl;
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kwsys::Encoding::ToWindowsExtendedPath("\\\\.\\") != L"\\\\.\\") {
|
|
|
|
std::cout << "Problem with ToWindowsExtendedPath "
|
|
|
|
<< "\"\\\\.\\\"" << std::endl;
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kwsys::Encoding::ToWindowsExtendedPath("\\\\.\\X") != L"\\\\.\\X") {
|
|
|
|
std::cout << "Problem with ToWindowsExtendedPath "
|
|
|
|
<< "\"\\\\.\\X\"" << std::endl;
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kwsys::Encoding::ToWindowsExtendedPath("\\\\.\\X:") != L"\\\\?\\X:") {
|
|
|
|
std::cout << "Problem with ToWindowsExtendedPath "
|
|
|
|
<< "\"\\\\.\\X:\"" << std::endl;
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kwsys::Encoding::ToWindowsExtendedPath("\\\\.\\X:\\") !=
|
|
|
|
L"\\\\?\\X:\\") {
|
|
|
|
std::cout << "Problem with ToWindowsExtendedPath "
|
|
|
|
<< "\"\\\\.\\X:\\\"" << std::endl;
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kwsys::Encoding::ToWindowsExtendedPath("NUL") != L"\\\\.\\NUL") {
|
|
|
|
std::cout << "Problem with ToWindowsExtendedPath "
|
|
|
|
<< "\"NUL\"" << std::endl;
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-11-09 14:12:29 +00:00
|
|
|
int testEncoding(int, char* [])
|
2013-11-21 17:47:57 +00:00
|
|
|
{
|
|
|
|
const char* loc = setlocale(LC_ALL, "");
|
2016-11-09 14:12:29 +00:00
|
|
|
if (loc) {
|
2013-11-21 17:47:57 +00:00
|
|
|
std::cout << "Locale: " << loc << std::endl;
|
2016-11-09 14:12:29 +00:00
|
|
|
} else {
|
2013-11-21 17:47:57 +00:00
|
|
|
std::cout << "Locale: None" << std::endl;
|
2016-11-09 14:12:29 +00:00
|
|
|
}
|
2013-11-21 17:47:57 +00:00
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ret |= testHelloWorldEncoding();
|
|
|
|
ret |= testRobustEncoding();
|
2013-12-19 15:01:48 +00:00
|
|
|
ret |= testCommandLineArguments();
|
2016-12-02 14:36:32 +00:00
|
|
|
ret |= testWithNulls();
|
2017-04-19 11:58:28 +00:00
|
|
|
ret |= testToWindowsExtendedPath();
|
2013-11-21 17:47:57 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|