testSystemTools.cxx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*=========================================================================
  2. Program: KWSys - Kitware System Library
  3. Module: $RCSfile$
  4. Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
  5. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  8. PURPOSE. See the above copyright notices for more information.
  9. =========================================================================*/
  10. #include "kwsysPrivate.h"
  11. #if defined(_MSC_VER)
  12. # pragma warning (disable:4786)
  13. #endif
  14. #include KWSYS_HEADER(SystemTools.hxx)
  15. #include KWSYS_HEADER(ios/iostream)
  16. // Work-around CMake dependency scanning limitation. This must
  17. // duplicate the above list of headers.
  18. #if 0
  19. # include "SystemTools.hxx.in"
  20. # include "kwsys_ios_iostream.h.in"
  21. #endif
  22. //----------------------------------------------------------------------------
  23. const char* toUnixPaths[][2] =
  24. {
  25. { "/usr/local/bin/passwd", "/usr/local/bin/passwd" },
  26. { "/usr/lo cal/bin/pa sswd", "/usr/lo cal/bin/pa sswd" },
  27. { "/usr/lo\\ cal/bin/pa\\ sswd", "/usr/lo\\ cal/bin/pa\\ sswd" },
  28. { "c:/usr/local/bin/passwd", "c:/usr/local/bin/passwd" },
  29. { "c:/usr/lo cal/bin/pa sswd", "c:/usr/lo cal/bin/pa sswd" },
  30. { "c:/usr/lo\\ cal/bin/pa\\ sswd", "c:/usr/lo\\ cal/bin/pa\\ sswd" },
  31. { "\\usr\\local\\bin\\passwd", "/usr/local/bin/passwd" },
  32. { "\\usr\\lo cal\\bin\\pa sswd", "/usr/lo cal/bin/pa sswd" },
  33. { "\\usr\\lo\\ cal\\bin\\pa\\ sswd", "/usr/lo\\ cal/bin/pa\\ sswd" },
  34. { "c:\\usr\\local\\bin\\passwd", "c:/usr/local/bin/passwd" },
  35. { "c:\\usr\\lo cal\\bin\\pa sswd", "c:/usr/lo cal/bin/pa sswd" },
  36. { "c:\\usr\\lo\\ cal\\bin\\pa\\ sswd", "c:/usr/lo\\ cal/bin/pa\\ sswd" },
  37. { "\\\\usr\\local\\bin\\passwd", "//usr/local/bin/passwd" },
  38. { "\\\\usr\\lo cal\\bin\\pa sswd", "//usr/lo cal/bin/pa sswd" },
  39. { "\\\\usr\\lo\\ cal\\bin\\pa\\ sswd", "//usr/lo\\ cal/bin/pa\\ sswd" },
  40. {0, 0}
  41. };
  42. bool CheckConvertToUnixSlashes(kwsys_stl::string input,
  43. kwsys_stl::string output)
  44. {
  45. kwsys_stl::string result = input;
  46. kwsys::SystemTools::ConvertToUnixSlashes(result);
  47. if ( result != output )
  48. {
  49. kwsys_ios::cerr
  50. << "Problem with ConvertToUnixSlashes - input: " << input.c_str()
  51. << " output: " << result.c_str() << " expected: " << output.c_str()
  52. << kwsys_ios::endl;
  53. return false;
  54. }
  55. return true;
  56. }
  57. //----------------------------------------------------------------------------
  58. const char* checkEscapeChars[][4] =
  59. {
  60. { "1 foo 2 bar 2", "12", "\\", "\\1 foo \\2 bar \\2"},
  61. { " {} ", "{}", "#", " #{#} "},
  62. {0, 0, 0, 0}
  63. };
  64. bool CheckEscapeChars(kwsys_stl::string input,
  65. const char *chars_to_escape,
  66. char escape_char,
  67. kwsys_stl::string output)
  68. {
  69. kwsys_stl::string result = kwsys::SystemTools::EscapeChars(
  70. input.c_str(), chars_to_escape, escape_char);
  71. if (result != output)
  72. {
  73. kwsys_ios::cerr
  74. << "Problem with CheckEscapeChars - input: " << input.c_str()
  75. << " output: " << result.c_str() << " expected: " << output.c_str()
  76. << kwsys_ios::endl;
  77. return false;
  78. }
  79. return true;
  80. }
  81. //----------------------------------------------------------------------------
  82. int main(/*int argc, char* argv*/)
  83. {
  84. bool res = true;
  85. int cc;
  86. for ( cc = 0; toUnixPaths[cc][0]; cc ++ )
  87. {
  88. res &= CheckConvertToUnixSlashes(toUnixPaths[cc][0], toUnixPaths[cc][1]);
  89. }
  90. // Special check for ~
  91. kwsys_stl::string output;
  92. if(kwsys::SystemTools::GetEnv("HOME", output))
  93. {
  94. output += "/foo bar/lala";
  95. res &= CheckConvertToUnixSlashes("~/foo bar/lala", output);
  96. }
  97. for (cc = 0; checkEscapeChars[cc][0]; cc ++ )
  98. {
  99. res &= CheckEscapeChars(checkEscapeChars[cc][0], checkEscapeChars[cc][1],
  100. *checkEscapeChars[cc][2], checkEscapeChars[cc][3]);
  101. }
  102. return res ? 0 : 1;
  103. }