testSystemTools.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. #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 with <> instead of "" to avoid getting any in-source copy
  23. // left on disk.
  24. #include <testSystemTools.h>
  25. #include <string.h> /* strcmp */
  26. //----------------------------------------------------------------------------
  27. const char* toUnixPaths[][2] =
  28. {
  29. { "/usr/local/bin/passwd", "/usr/local/bin/passwd" },
  30. { "/usr/lo cal/bin/pa sswd", "/usr/lo cal/bin/pa sswd" },
  31. { "/usr/lo\\ cal/bin/pa\\ sswd", "/usr/lo\\ cal/bin/pa\\ sswd" },
  32. { "c:/usr/local/bin/passwd", "c:/usr/local/bin/passwd" },
  33. { "c:/usr/lo cal/bin/pa sswd", "c:/usr/lo cal/bin/pa sswd" },
  34. { "c:/usr/lo\\ cal/bin/pa\\ sswd", "c:/usr/lo\\ cal/bin/pa\\ sswd" },
  35. { "\\usr\\local\\bin\\passwd", "/usr/local/bin/passwd" },
  36. { "\\usr\\lo cal\\bin\\pa sswd", "/usr/lo cal/bin/pa sswd" },
  37. { "\\usr\\lo\\ cal\\bin\\pa\\ sswd", "/usr/lo\\ cal/bin/pa\\ sswd" },
  38. { "c:\\usr\\local\\bin\\passwd", "c:/usr/local/bin/passwd" },
  39. { "c:\\usr\\lo cal\\bin\\pa sswd", "c:/usr/lo cal/bin/pa sswd" },
  40. { "c:\\usr\\lo\\ cal\\bin\\pa\\ sswd", "c:/usr/lo\\ cal/bin/pa\\ sswd" },
  41. { "\\\\usr\\local\\bin\\passwd", "//usr/local/bin/passwd" },
  42. { "\\\\usr\\lo cal\\bin\\pa sswd", "//usr/lo cal/bin/pa sswd" },
  43. { "\\\\usr\\lo\\ cal\\bin\\pa\\ sswd", "//usr/lo\\ cal/bin/pa\\ sswd" },
  44. {0, 0}
  45. };
  46. bool CheckConvertToUnixSlashes(kwsys_stl::string input,
  47. kwsys_stl::string output)
  48. {
  49. kwsys_stl::string result = input;
  50. kwsys::SystemTools::ConvertToUnixSlashes(result);
  51. if ( result != output )
  52. {
  53. kwsys_ios::cerr
  54. << "Problem with ConvertToUnixSlashes - input: " << input.c_str()
  55. << " output: " << result.c_str() << " expected: " << output.c_str()
  56. << kwsys_ios::endl;
  57. return false;
  58. }
  59. return true;
  60. }
  61. //----------------------------------------------------------------------------
  62. const char* checkEscapeChars[][4] =
  63. {
  64. { "1 foo 2 bar 2", "12", "\\", "\\1 foo \\2 bar \\2"},
  65. { " {} ", "{}", "#", " #{#} "},
  66. {0, 0, 0, 0}
  67. };
  68. bool CheckEscapeChars(kwsys_stl::string input,
  69. const char *chars_to_escape,
  70. char escape_char,
  71. kwsys_stl::string output)
  72. {
  73. kwsys_stl::string result = kwsys::SystemTools::EscapeChars(
  74. input.c_str(), chars_to_escape, escape_char);
  75. if (result != output)
  76. {
  77. kwsys_ios::cerr
  78. << "Problem with CheckEscapeChars - input: " << input.c_str()
  79. << " output: " << result.c_str() << " expected: " << output.c_str()
  80. << kwsys_ios::endl;
  81. return false;
  82. }
  83. return true;
  84. }
  85. //----------------------------------------------------------------------------
  86. bool CheckFileOperations()
  87. {
  88. bool res = true;
  89. if (kwsys::SystemTools::DetectFileType(TEST_SYSTEMTOOLS_BIN_FILE) !=
  90. kwsys::SystemTools::FileTypeBinary)
  91. {
  92. kwsys_ios::cerr
  93. << "Problem with DetectFileType - failed to detect type of: "
  94. << TEST_SYSTEMTOOLS_BIN_FILE << kwsys_ios::endl;
  95. res = false;
  96. }
  97. if (kwsys::SystemTools::DetectFileType(TEST_SYSTEMTOOLS_SRC_FILE) !=
  98. kwsys::SystemTools::FileTypeText)
  99. {
  100. kwsys_ios::cerr
  101. << "Problem with DetectFileType - failed to detect type of: "
  102. << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
  103. res = false;
  104. }
  105. if (kwsys::SystemTools::FileLength(TEST_SYSTEMTOOLS_BIN_FILE) != 766)
  106. {
  107. kwsys_ios::cerr
  108. << "Problem with FileLength - incorrect length for: "
  109. << TEST_SYSTEMTOOLS_BIN_FILE << kwsys_ios::endl;
  110. res = false;
  111. }
  112. return res;
  113. }
  114. //----------------------------------------------------------------------------
  115. bool CheckStringOperations()
  116. {
  117. bool res = true;
  118. kwsys_stl::string test = "mary had a little lamb.";
  119. if (kwsys::SystemTools::CapitalizedWords(test) != "Mary Had A Little Lamb.")
  120. {
  121. kwsys_ios::cerr
  122. << "Problem with CapitalizedWords "
  123. << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
  124. res = false;
  125. }
  126. test = "Mary Had A Little Lamb.";
  127. if (kwsys::SystemTools::UnCapitalizedWords(test) !=
  128. "mary had a little lamb.")
  129. {
  130. kwsys_ios::cerr
  131. << "Problem with UnCapitalizedWords "
  132. << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
  133. res = false;
  134. }
  135. test = "MaryHadTheLittleLamb.";
  136. if (kwsys::SystemTools::AddSpaceBetweenCapitalizedWords(test) !=
  137. "Mary Had The Little Lamb.")
  138. {
  139. kwsys_ios::cerr
  140. << "Problem with AddSpaceBetweenCapitalizedWords "
  141. << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
  142. res = false;
  143. }
  144. char * cres =
  145. kwsys::SystemTools::AppendStrings("Mary Had A"," Little Lamb.");
  146. if (strcmp(cres,"Mary Had A Little Lamb."))
  147. {
  148. kwsys_ios::cerr
  149. << "Problem with AppendStrings "
  150. << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
  151. res = false;
  152. }
  153. delete [] cres;
  154. cres =
  155. kwsys::SystemTools::AppendStrings("Mary Had"," A ","Little Lamb.");
  156. if (strcmp(cres,"Mary Had A Little Lamb."))
  157. {
  158. kwsys_ios::cerr
  159. << "Problem with AppendStrings "
  160. << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
  161. res = false;
  162. }
  163. delete [] cres;
  164. if (kwsys::SystemTools::CountChar("Mary Had A Little Lamb.",'a') != 3)
  165. {
  166. kwsys_ios::cerr
  167. << "Problem with CountChar "
  168. << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
  169. res = false;
  170. }
  171. cres =
  172. kwsys::SystemTools::RemoveChars("Mary Had A Little Lamb.","aeiou");
  173. if (strcmp(cres,"Mry Hd A Lttl Lmb."))
  174. {
  175. kwsys_ios::cerr
  176. << "Problem with RemoveChars "
  177. << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
  178. res = false;
  179. }
  180. delete [] cres;
  181. cres =
  182. kwsys::SystemTools::RemoveCharsButUpperHex("Mary Had A Little Lamb.");
  183. if (strcmp(cres,"A"))
  184. {
  185. kwsys_ios::cerr
  186. << "Problem with RemoveCharsButUpperHex "
  187. << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
  188. res = false;
  189. }
  190. delete [] cres;
  191. char *cres2 = new char [strlen("Mary Had A Little Lamb.")+1];
  192. strcpy(cres2,"Mary Had A Little Lamb.");
  193. kwsys::SystemTools::ReplaceChars(cres2,"aeiou",'X');
  194. if (strcmp(cres2,"MXry HXd A LXttlX LXmb."))
  195. {
  196. kwsys_ios::cerr
  197. << "Problem with ReplaceChars "
  198. << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
  199. res = false;
  200. }
  201. delete [] cres2;
  202. if (!kwsys::SystemTools::StringStartsWith("Mary Had A Little Lamb.",
  203. "Mary "))
  204. {
  205. kwsys_ios::cerr
  206. << "Problem with StringStartsWith "
  207. << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
  208. res = false;
  209. }
  210. if (!kwsys::SystemTools::StringEndsWith("Mary Had A Little Lamb.",
  211. " Lamb."))
  212. {
  213. kwsys_ios::cerr
  214. << "Problem with StringEndsWith "
  215. << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
  216. res = false;
  217. }
  218. cres = kwsys::SystemTools::DuplicateString("Mary Had A Little Lamb.");
  219. if (strcmp(cres,"Mary Had A Little Lamb."))
  220. {
  221. kwsys_ios::cerr
  222. << "Problem with DuplicateString "
  223. << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
  224. res = false;
  225. }
  226. delete [] cres;
  227. test = "Mary Had A Little Lamb.";
  228. if (kwsys::SystemTools::CropString(test,13) !=
  229. "Mary ...Lamb.")
  230. {
  231. kwsys_ios::cerr
  232. << "Problem with CropString "
  233. << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
  234. res = false;
  235. }
  236. kwsys_stl::vector<kwsys_stl::string> lines;
  237. kwsys::SystemTools::Split("Mary Had A Little Lamb.",lines,' ');
  238. if (lines[0] != "Mary" || lines[1] != "Had" ||
  239. lines[2] != "A" || lines[3] != "Little" || lines[4] != "Lamb.")
  240. {
  241. kwsys_ios::cerr
  242. << "Problem with Split "
  243. << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
  244. res = false;
  245. }
  246. if (kwsys::SystemTools::ConvertToWindowsOutputPath
  247. ("L://Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  248. "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"")
  249. {
  250. kwsys_ios::cerr
  251. << "Problem with ConvertToWindowsOutputPath "
  252. << kwsys_ios::endl;
  253. res = false;
  254. }
  255. if (kwsys::SystemTools::ConvertToWindowsOutputPath
  256. ("//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  257. "\"\\\\grayson\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"")
  258. {
  259. kwsys_ios::cerr
  260. << "Problem with ConvertToWindowsOutputPath "
  261. << kwsys_ios::endl;
  262. res = false;
  263. }
  264. if (kwsys::SystemTools::ConvertToUnixOutputPath
  265. ("//Local Mojo/Hex Power Pack/Iffy Voodoo") !=
  266. "/Local\\ Mojo/Hex\\ Power\\ Pack/Iffy\\ Voodoo")
  267. {
  268. kwsys_ios::cerr
  269. << "Problem with ConvertToUnixOutputPath "
  270. << kwsys_ios::endl;
  271. res = false;
  272. }
  273. int targc;
  274. char **targv;
  275. kwsys::SystemTools::ConvertWindowsCommandLineToUnixArguments
  276. ("\"Local Mojo\\Voodoo.asp\" -CastHex \"D:\\My Secret Mojo\\Voodoo.mp3\"", &targc, &targv);
  277. if (targc != 4 || strcmp(targv[1],"Local Mojo\\Voodoo.asp") ||
  278. strcmp(targv[2],"-CastHex") ||
  279. strcmp(targv[3],"D:\\My Secret Mojo\\Voodoo.mp3"))
  280. {
  281. kwsys_ios::cerr
  282. << "Problem with ConvertWindowsCommandLineToUnixArguments"
  283. << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
  284. res = false;
  285. }
  286. for (;targc >=0; --targc)
  287. {
  288. delete [] targv[targc];
  289. }
  290. delete [] targv;
  291. return res;
  292. }
  293. //----------------------------------------------------------------------------
  294. int testSystemTools(int, char*[])
  295. {
  296. bool res = true;
  297. int cc;
  298. for ( cc = 0; toUnixPaths[cc][0]; cc ++ )
  299. {
  300. res &= CheckConvertToUnixSlashes(toUnixPaths[cc][0], toUnixPaths[cc][1]);
  301. }
  302. // Special check for ~
  303. kwsys_stl::string output;
  304. if(kwsys::SystemTools::GetEnv("HOME", output))
  305. {
  306. output += "/foo bar/lala";
  307. res &= CheckConvertToUnixSlashes("~/foo bar/lala", output);
  308. }
  309. for (cc = 0; checkEscapeChars[cc][0]; cc ++ )
  310. {
  311. res &= CheckEscapeChars(checkEscapeChars[cc][0], checkEscapeChars[cc][1],
  312. *checkEscapeChars[cc][2], checkEscapeChars[cc][3]);
  313. }
  314. res &= CheckFileOperations();
  315. res &= CheckStringOperations();
  316. return res ? 0 : 1;
  317. }