EncodingCXX.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. # include <shellapi.h>
  34. #endif
  35. namespace KWSYS_NAMESPACE
  36. {
  37. Encoding::CommandLineArguments
  38. Encoding::CommandLineArguments::Main(int argc, char const* const* argv)
  39. {
  40. #ifdef _WIN32
  41. (void) argc;
  42. (void) argv;
  43. int ac;
  44. LPWSTR* w_av = CommandLineToArgvW(GetCommandLineW(), &ac);
  45. std::vector<std::string> av1(ac);
  46. std::vector<char const*> av2(ac);
  47. for(int i=0; i<ac; i++)
  48. {
  49. av1[i] = ToNarrow(w_av[i]);
  50. av2[i] = av1[i].c_str();
  51. }
  52. LocalFree(w_av);
  53. return CommandLineArguments(ac, &av2[0]);
  54. #else
  55. return CommandLineArguments(argc, argv);
  56. #endif
  57. }
  58. Encoding::CommandLineArguments::CommandLineArguments(int ac,
  59. char const* const* av)
  60. {
  61. this->argv_.resize(ac+1);
  62. for(int i=0; i<ac; i++)
  63. {
  64. this->argv_[i] = strdup(av[i]);
  65. }
  66. this->argv_[ac] = 0;
  67. }
  68. Encoding::CommandLineArguments::CommandLineArguments(int ac,
  69. wchar_t const* const* av)
  70. {
  71. this->argv_.resize(ac+1);
  72. for(int i=0; i<ac; i++)
  73. {
  74. this->argv_[i] = kwsysEncoding_DupToNarrow(av[i]);
  75. }
  76. this->argv_[ac] = 0;
  77. }
  78. Encoding::CommandLineArguments::~CommandLineArguments()
  79. {
  80. for(size_t i=0; i<this->argv_.size(); i++)
  81. {
  82. free(argv_[i]);
  83. }
  84. }
  85. Encoding::CommandLineArguments::
  86. CommandLineArguments(const CommandLineArguments& other)
  87. {
  88. this->argv_.resize(other.argv_.size());
  89. for(size_t i=0; i<this->argv_.size(); i++)
  90. {
  91. this->argv_[i] = other.argv_[i] ? strdup(other.argv_[i]) : 0;
  92. }
  93. }
  94. Encoding::CommandLineArguments&
  95. Encoding::CommandLineArguments::operator=(const CommandLineArguments& other)
  96. {
  97. if(this != &other)
  98. {
  99. size_t i;
  100. for(i=0; i<this->argv_.size(); i++)
  101. {
  102. free(this->argv_[i]);
  103. }
  104. this->argv_.resize(other.argv_.size());
  105. for(i=0; i<this->argv_.size(); i++)
  106. {
  107. this->argv_[i] = other.argv_[i] ? strdup(other.argv_[i]) : 0;
  108. }
  109. }
  110. return *this;
  111. }
  112. int Encoding::CommandLineArguments::argc() const
  113. {
  114. return static_cast<int>(this->argv_.size() - 1);
  115. }
  116. char const* const* Encoding::CommandLineArguments::argv() const
  117. {
  118. return &this->argv_[0];
  119. }
  120. #if KWSYS_STL_HAS_WSTRING
  121. std::wstring Encoding::ToWide(const std::string& str)
  122. {
  123. return ToWide(str.c_str());
  124. }
  125. std::string Encoding::ToNarrow(const std::wstring& str)
  126. {
  127. return ToNarrow(str.c_str());
  128. }
  129. std::wstring Encoding::ToWide(const char* cstr)
  130. {
  131. std::wstring wstr;
  132. size_t length = kwsysEncoding_mbstowcs(0, cstr, 0) + 1;
  133. if(length > 0)
  134. {
  135. std::vector<wchar_t> wchars(length);
  136. if(kwsysEncoding_mbstowcs(&wchars[0], cstr, length) > 0)
  137. {
  138. wstr = &wchars[0];
  139. }
  140. }
  141. return wstr;
  142. }
  143. std::string Encoding::ToNarrow(const wchar_t* wcstr)
  144. {
  145. std::string str;
  146. size_t length = kwsysEncoding_wcstombs(0, wcstr, 0) + 1;
  147. if(length > 0)
  148. {
  149. std::vector<char> chars(length);
  150. if(kwsysEncoding_wcstombs(&chars[0], wcstr, length) > 0)
  151. {
  152. str = &chars[0];
  153. }
  154. }
  155. return str;
  156. }
  157. #endif // KWSYS_STL_HAS_WSTRING
  158. } // namespace KWSYS_NAMESPACE