cmProcess.h 2.1 KB

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