cmProcess.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // Return true if the process starts
  30. bool StartProcess();
  31. // return the process status
  32. int GetProcessStatus();
  33. // Report the status of the program
  34. int ReportStatus();
  35. int GetId() { return this->Id; }
  36. void SetId(int id) { this->Id = id;}
  37. int GetExitValue() { return this->ExitValue;}
  38. double GetTotalTime() { return this->TotalTime;}
  39. int GetExitException();
  40. /**
  41. * Read one line of output but block for no more than timeout.
  42. * Returns:
  43. * cmsysProcess_Pipe_None = Process terminated and all output read
  44. * cmsysProcess_Pipe_STDOUT = Line came from stdout
  45. * cmsysProcess_Pipe_STDOUT = Line came from stderr
  46. * cmsysProcess_Pipe_Timeout = Timeout expired while waiting
  47. */
  48. int GetNextOutputLine(std::string& line, double timeout);
  49. private:
  50. double Timeout;
  51. double StartTime;
  52. double TotalTime;
  53. cmsysProcess* Process;
  54. class Buffer: public std::vector<char>
  55. {
  56. // Half-open index range of partial line already scanned.
  57. size_type First;
  58. size_type Last;
  59. public:
  60. Buffer(): First(0), Last(0) {}
  61. bool GetLine(std::string& line);
  62. bool GetLast(std::string& line);
  63. };
  64. Buffer StdErr;
  65. Buffer StdOut;
  66. std::string Command;
  67. std::string WorkingDirectory;
  68. std::vector<std::string> Arguments;
  69. std::vector<const char*> ProcessArgs;
  70. std::string Output;
  71. int Id;
  72. int ExitValue;
  73. };
  74. #endif