ccommand.cxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmMakefile.h"
  14. #include "cmSystemTools.h"
  15. #include "time.h"
  16. void CMakeCommandUsage(const char* program)
  17. {
  18. std::strstream errorStream;
  19. errorStream
  20. << "cmake version " << cmMakefile::GetMajorVersion()
  21. << "." << cmMakefile::GetMinorVersion() << "\n";
  22. errorStream
  23. << "Usage: " << program << " [command] [arguments ...]\n"
  24. << "Available commands: \n"
  25. << " copy file destination - copy file to destination (either file or directory)\n"
  26. << " remove file1 file2 ... - remove the file(s)\n"
  27. << " time command [args] ... - run command and return elapsed time\n"
  28. #if defined(_WIN32) && !defined(__CYGWIN__)
  29. << " write_regv key value - write registry value\n"
  30. << " delete_regv key - delete registry value\n"
  31. #endif
  32. << std::ends;
  33. cmSystemTools::Error(errorStream.str());
  34. }
  35. int main(int ac, char** av)
  36. {
  37. std::vector<std::string> args;
  38. for(int i =0; i < ac; ++i)
  39. {
  40. args.push_back(av[i]);
  41. }
  42. if (args.size() > 1)
  43. {
  44. // Copy file
  45. if (args[1] == "copy" && args.size() == 4)
  46. {
  47. cmSystemTools::cmCopyFile(args[2].c_str(), args[3].c_str());
  48. return cmSystemTools::GetErrorOccuredFlag();
  49. }
  50. // Remove file
  51. else if (args[1] == "remove" && args.size() > 2)
  52. {
  53. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  54. {
  55. if(args[cc] != "-f")
  56. {
  57. if(args[cc] == "\\-f")
  58. {
  59. args[cc] = "-f";
  60. }
  61. cmSystemTools::RemoveFile(args[cc].c_str());
  62. }
  63. }
  64. return 0;
  65. }
  66. // Clock command
  67. else if (args[1] == "time" && args.size() > 2)
  68. {
  69. std::string command = args[2];
  70. std::string output;
  71. for (std::string::size_type cc = 3; cc < args.size(); cc ++)
  72. {
  73. command += " ";
  74. command += args[cc];
  75. }
  76. clock_t clock_start, clock_finish;
  77. time_t time_start, time_finish;
  78. time(&time_start);
  79. clock_start = clock();
  80. cmSystemTools::RunCommand(command.c_str(), output, 0, true);
  81. clock_finish = clock();
  82. time(&time_finish);
  83. std::cout << output.c_str();
  84. double clocks_per_sec = (double)CLOCKS_PER_SEC;
  85. std::cout << "Elapsed time: "
  86. << (time_t)(time_finish - time_start) << " s. (time)"
  87. << ", "
  88. << (double)(clock_finish - clock_start) / clocks_per_sec
  89. << " s. (clock)"
  90. << "\n";
  91. return 0;
  92. }
  93. #if defined(_WIN32) && !defined(__CYGWIN__)
  94. // Write registry value
  95. else if (args[1] == "write_regv" && args.size() > 3)
  96. {
  97. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  98. args[3].c_str()) ? 0 : 1;
  99. }
  100. // Delete registry value
  101. else if (args[1] == "delete_regv" && args.size() > 2)
  102. {
  103. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  104. }
  105. #endif
  106. }
  107. ::CMakeCommandUsage(args[0].c_str());
  108. return 1;
  109. }