cmProcess.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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()
  62. : First(0)
  63. , Last(0)
  64. {
  65. }
  66. bool GetLine(std::string& line);
  67. bool GetLast(std::string& line);
  68. };
  69. Buffer Output;
  70. std::string Command;
  71. std::string WorkingDirectory;
  72. std::vector<std::string> Arguments;
  73. std::vector<const char*> ProcessArgs;
  74. int Id;
  75. int ExitValue;
  76. };
  77. #endif