cmProcess.h 2.4 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. /**
  40. * Read one line of output but block for no more than timeout.
  41. * Returns:
  42. * cmsysProcess_Pipe_None = Process terminated and all output read
  43. * cmsysProcess_Pipe_STDOUT = Line came from stdout
  44. * cmsysProcess_Pipe_STDOUT = Line came from stderr
  45. * cmsysProcess_Pipe_Timeout = Timeout expired while waiting
  46. */
  47. int GetNextOutputLine(std::string& line, double timeout);
  48. private:
  49. double Timeout;
  50. double StartTime;
  51. double TotalTime;
  52. cmsysProcess* Process;
  53. class Buffer: public std::vector<char>
  54. {
  55. // Half-open index range of partial line already scanned.
  56. size_type First;
  57. size_type Last;
  58. public:
  59. Buffer(): First(0), Last(0) {}
  60. bool GetLine(std::string& line);
  61. bool GetLast(std::string& line);
  62. };
  63. Buffer StdErr;
  64. Buffer StdOut;
  65. std::string Command;
  66. std::string WorkingDirectory;
  67. std::vector<std::string> Arguments;
  68. std::vector<const char*> ProcessArgs;
  69. std::string Output;
  70. int Id;
  71. int ExitValue;
  72. };
  73. #endif