cmProcess.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  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. #ifndef cmProcess_h
  11. #define cmProcess_h
  12. #include "cmStandardIncludes.h"
  13. #include <cmsys/Process.h>
  14. /** \class cmProcess
  15. * \brief run a process with c++
  16. *
  17. * cmProcess wraps the kwsys process stuff in a c++ class.
  18. */
  19. class cmProcess
  20. {
  21. public:
  22. cmProcess();
  23. ~cmProcess();
  24. const char* GetCommand() { return this->Command.c_str();}
  25. void SetCommand(const char* command);
  26. void SetCommandArguments(std::vector<std::string> const& arg);
  27. void SetWorkingDirectory(const char* dir) { this->WorkingDirectory = dir;}
  28. void SetTimeout(double t) { this->Timeout = t;}
  29. void ChangeTimeout(double t);
  30. void ResetStartTime();
  31. // Return true if the process starts
  32. bool StartProcess();
  33. // return the process status
  34. int GetProcessStatus();
  35. // Report the status of the program
  36. int ReportStatus();
  37. int GetId() { return this->Id; }
  38. void SetId(int id) { this->Id = id;}
  39. int GetExitValue() { return this->ExitValue;}
  40. double GetTotalTime() { return this->TotalTime;}
  41. int GetExitException();
  42. /**
  43. * Read one line of output but block for no more than timeout.
  44. * Returns:
  45. * cmsysProcess_Pipe_None = Process terminated and all output read
  46. * cmsysProcess_Pipe_STDOUT = Line came from stdout or stderr
  47. * cmsysProcess_Pipe_Timeout = Timeout expired while waiting
  48. */
  49. int GetNextOutputLine(std::string& line, double timeout);
  50. private:
  51. double Timeout;
  52. double StartTime;
  53. double TotalTime;
  54. cmsysProcess* Process;
  55. class Buffer: public std::vector<char>
  56. {
  57. // Half-open index range of partial line already scanned.
  58. size_type First;
  59. size_type Last;
  60. public:
  61. Buffer(): First(0), Last(0) {}
  62. bool GetLine(std::string& line);
  63. bool GetLast(std::string& line);
  64. };
  65. Buffer Output;
  66. std::string Command;
  67. std::string WorkingDirectory;
  68. std::vector<std::string> Arguments;
  69. std::vector<const char*> ProcessArgs;
  70. int Id;
  71. int ExitValue;
  72. };
  73. #endif