cmProcess.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. std::string GetExitExceptionString();
  38. /**
  39. * Read one line of output but block for no more than timeout.
  40. * Returns:
  41. * cmsysProcess_Pipe_None = Process terminated and all output read
  42. * cmsysProcess_Pipe_STDOUT = Line came from stdout or stderr
  43. * cmsysProcess_Pipe_Timeout = Timeout expired while waiting
  44. */
  45. int GetNextOutputLine(std::string& line, double timeout);
  46. private:
  47. double Timeout;
  48. double StartTime;
  49. double TotalTime;
  50. cmsysProcess* Process;
  51. class Buffer : public std::vector<char>
  52. {
  53. // Half-open index range of partial line already scanned.
  54. size_type First;
  55. size_type Last;
  56. public:
  57. Buffer()
  58. : First(0)
  59. , Last(0)
  60. {
  61. }
  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