EncodingCXX.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifdef __osf__
  11. # define _OSF_SOURCE
  12. # define _POSIX_C_SOURCE 199506L
  13. # define _XOPEN_SOURCE_EXTENDED
  14. #endif
  15. #include "kwsysPrivate.h"
  16. #include KWSYS_HEADER(Encoding.hxx)
  17. #include KWSYS_HEADER(Encoding.h)
  18. // Work-around CMake dependency scanning limitation. This must
  19. // duplicate the above list of headers.
  20. #if 0
  21. # include "Encoding.hxx.in"
  22. # include "Encoding.h.in"
  23. #endif
  24. #include <vector>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #ifdef _MSC_VER
  28. # pragma warning (disable: 4786)
  29. #endif
  30. // Windows API.
  31. #if defined(_WIN32)
  32. # include <windows.h>
  33. #endif
  34. namespace KWSYS_NAMESPACE
  35. {
  36. Encoding::CommandLineArguments
  37. Encoding::CommandLineArguments::Main(int argc, char const* const* argv)
  38. {
  39. #ifdef _WIN32
  40. (void) argc;
  41. (void) argv;
  42. int ac;
  43. LPWSTR* w_av = CommandLineToArgvW(GetCommandLineW(), &ac);
  44. std::vector<std::string> av1(ac);
  45. std::vector<char const*> av2(ac);
  46. for(int i=0; i<ac; i++)
  47. {
  48. av1[i] = ToNarrow(w_av[i]);
  49. av2[i] = av1[i].c_str();
  50. }
  51. LocalFree(w_av);
  52. return CommandLineArguments(ac, &av2[0]);
  53. #else
  54. return CommandLineArguments(argc, argv);
  55. #endif
  56. }
  57. Encoding::CommandLineArguments::CommandLineArguments(int ac,
  58. char const* const* av)
  59. {
  60. this->argv_.resize(ac+1);
  61. for(int i=0; i<ac; i++)
  62. {
  63. this->argv_[i] = strdup(av[i]);
  64. }
  65. this->argv_[ac] = 0;
  66. }
  67. Encoding::CommandLineArguments::CommandLineArguments(int ac,
  68. wchar_t const* const* av)
  69. {
  70. this->argv_.resize(ac+1);
  71. for(int i=0; i<ac; i++)
  72. {
  73. this->argv_[i] = kwsysEncoding_DupToNarrow(av[i]);
  74. }
  75. this->argv_[ac] = 0;
  76. }
  77. Encoding::CommandLineArguments::~CommandLineArguments()
  78. {
  79. for(size_t i=0; i<this->argv_.size(); i++)
  80. {
  81. free(argv_[i]);
  82. }
  83. }
  84. Encoding::CommandLineArguments::
  85. CommandLineArguments(const CommandLineArguments& other)
  86. {
  87. this->argv_.resize(other.argv_.size());
  88. for(size_t i=0; i<this->argv_.size(); i++)
  89. {
  90. this->argv_[i] = other.argv_[i] ? strdup(other.argv_[i]) : 0;
  91. }
  92. }
  93. Encoding::CommandLineArguments&
  94. Encoding::CommandLineArguments::operator=(const CommandLineArguments& other)
  95. {
  96. if(this != &other)
  97. {
  98. size_t i;
  99. for(i=0; i<this->argv_.size(); i++)
  100. {
  101. free(this->argv_[i]);
  102. }
  103. this->argv_.resize(other.argv_.size());
  104. for(i=0; i<this->argv_.size(); i++)
  105. {
  106. this->argv_[i] = other.argv_[i] ? strdup(other.argv_[i]) : 0;
  107. }
  108. }
  109. return *this;
  110. }
  111. int Encoding::CommandLineArguments::argc() const
  112. {
  113. return static_cast<int>(this->argv_.size() - 1);
  114. }
  115. char const* const* Encoding::CommandLineArguments::argv() const
  116. {
  117. return &this->argv_[0];
  118. }
  119. #if KWSYS_STL_HAS_WSTRING
  120. std::wstring Encoding::ToWide(const std::string& str)
  121. {
  122. return ToWide(str.c_str());
  123. }
  124. std::string Encoding::ToNarrow(const std::wstring& str)
  125. {
  126. return ToNarrow(str.c_str());
  127. }
  128. std::wstring Encoding::ToWide(const char* cstr)
  129. {
  130. std::wstring wstr;
  131. size_t length = kwsysEncoding_mbstowcs(0, cstr, 0) + 1;
  132. if(length > 0)
  133. {
  134. std::vector<wchar_t> wchars(length);
  135. if(kwsysEncoding_mbstowcs(&wchars[0], cstr, length) > 0)
  136. {
  137. wstr = &wchars[0];
  138. }
  139. }
  140. return wstr;
  141. }
  142. std::string Encoding::ToNarrow(const wchar_t* wcstr)
  143. {
  144. std::string str;
  145. size_t length = kwsysEncoding_wcstombs(0, wcstr, 0) + 1;
  146. if(length > 0)
  147. {
  148. std::vector<char> chars(length);
  149. if(kwsysEncoding_wcstombs(&chars[0], wcstr, length) > 0)
  150. {
  151. str = &chars[0];
  152. }
  153. }
  154. return str;
  155. }
  156. #endif // KWSYS_STL_HAS_WSTRING
  157. } // namespace KWSYS_NAMESPACE