testSystemTools.cxx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include "testSystemTools.h"
  23. //----------------------------------------------------------------------------
  24. const char* toUnixPaths[][2] =
  25. {
  26. { "/usr/local/bin/passwd", "/usr/local/bin/passwd" },
  27. { "/usr/lo cal/bin/pa sswd", "/usr/lo cal/bin/pa sswd" },
  28. { "/usr/lo\\ cal/bin/pa\\ sswd", "/usr/lo\\ cal/bin/pa\\ sswd" },
  29. { "c:/usr/local/bin/passwd", "c:/usr/local/bin/passwd" },
  30. { "c:/usr/lo cal/bin/pa sswd", "c:/usr/lo cal/bin/pa sswd" },
  31. { "c:/usr/lo\\ cal/bin/pa\\ sswd", "c:/usr/lo\\ cal/bin/pa\\ sswd" },
  32. { "\\usr\\local\\bin\\passwd", "/usr/local/bin/passwd" },
  33. { "\\usr\\lo cal\\bin\\pa sswd", "/usr/lo cal/bin/pa sswd" },
  34. { "\\usr\\lo\\ cal\\bin\\pa\\ sswd", "/usr/lo\\ cal/bin/pa\\ sswd" },
  35. { "c:\\usr\\local\\bin\\passwd", "c:/usr/local/bin/passwd" },
  36. { "c:\\usr\\lo cal\\bin\\pa sswd", "c:/usr/lo cal/bin/pa sswd" },
  37. { "c:\\usr\\lo\\ cal\\bin\\pa\\ sswd", "c:/usr/lo\\ cal/bin/pa\\ sswd" },
  38. { "\\\\usr\\local\\bin\\passwd", "//usr/local/bin/passwd" },
  39. { "\\\\usr\\lo cal\\bin\\pa sswd", "//usr/lo cal/bin/pa sswd" },
  40. { "\\\\usr\\lo\\ cal\\bin\\pa\\ sswd", "//usr/lo\\ cal/bin/pa\\ sswd" },
  41. {0, 0}
  42. };
  43. bool CheckConvertToUnixSlashes(kwsys_stl::string input,
  44. kwsys_stl::string output)
  45. {
  46. kwsys_stl::string result = input;
  47. kwsys::SystemTools::ConvertToUnixSlashes(result);
  48. if ( result != output )
  49. {
  50. kwsys_ios::cerr
  51. << "Problem with ConvertToUnixSlashes - input: " << input.c_str()
  52. << " output: " << result.c_str() << " expected: " << output.c_str()
  53. << kwsys_ios::endl;
  54. return false;
  55. }
  56. return true;
  57. }
  58. //----------------------------------------------------------------------------
  59. const char* checkEscapeChars[][4] =
  60. {
  61. { "1 foo 2 bar 2", "12", "\\", "\\1 foo \\2 bar \\2"},
  62. { " {} ", "{}", "#", " #{#} "},
  63. {0, 0, 0, 0}
  64. };
  65. bool CheckEscapeChars(kwsys_stl::string input,
  66. const char *chars_to_escape,
  67. char escape_char,
  68. kwsys_stl::string output)
  69. {
  70. kwsys_stl::string result = kwsys::SystemTools::EscapeChars(
  71. input.c_str(), chars_to_escape, escape_char);
  72. if (result != output)
  73. {
  74. kwsys_ios::cerr
  75. << "Problem with CheckEscapeChars - input: " << input.c_str()
  76. << " output: " << result.c_str() << " expected: " << output.c_str()
  77. << kwsys_ios::endl;
  78. return false;
  79. }
  80. return true;
  81. }
  82. //----------------------------------------------------------------------------
  83. bool CheckDetectFileType()
  84. {
  85. bool res = true;
  86. if (kwsys::SystemTools::DetectFileType(TEST_SYSTEMTOOLS_EXE_FILE) !=
  87. kwsys::SystemTools::FileTypeBinary)
  88. {
  89. kwsys_ios::cerr
  90. << "Problem with DetectFileType - failed to detect type of: "
  91. << TEST_SYSTEMTOOLS_EXE_FILE << kwsys_ios::endl;
  92. res = false;
  93. }
  94. if (kwsys::SystemTools::DetectFileType(TEST_SYSTEMTOOLS_SRC_FILE) !=
  95. kwsys::SystemTools::FileTypeText)
  96. {
  97. kwsys_ios::cerr
  98. << "Problem with DetectFileType - failed to detect type of: "
  99. << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
  100. res = false;
  101. }
  102. return res;
  103. }
  104. //----------------------------------------------------------------------------
  105. int main(/*int argc, char* argv*/)
  106. {
  107. bool res = true;
  108. int cc;
  109. for ( cc = 0; toUnixPaths[cc][0]; cc ++ )
  110. {
  111. res &= CheckConvertToUnixSlashes(toUnixPaths[cc][0], toUnixPaths[cc][1]);
  112. }
  113. // Special check for ~
  114. kwsys_stl::string output;
  115. if(kwsys::SystemTools::GetEnv("HOME", output))
  116. {
  117. output += "/foo bar/lala";
  118. res &= CheckConvertToUnixSlashes("~/foo bar/lala", output);
  119. }
  120. for (cc = 0; checkEscapeChars[cc][0]; cc ++ )
  121. {
  122. res &= CheckEscapeChars(checkEscapeChars[cc][0], checkEscapeChars[cc][1],
  123. *checkEscapeChars[cc][2], checkEscapeChars[cc][3]);
  124. }
  125. res &= CheckDetectFileType();
  126. return res ? 0 : 1;
  127. }