pipe.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #pragma once
  2. #include "input.h"
  3. #include "output.h"
  4. #include <util/system/pipe.h>
  5. #include <util/generic/ptr.h>
  6. #include <util/generic/string.h>
  7. /**
  8. * @addtogroup Streams_Pipes
  9. * @{
  10. */
  11. /**
  12. * Base class for starting a process and communicating with it via pipes.
  13. */
  14. class TPipeBase {
  15. protected:
  16. /**
  17. * Starts a new process and opens a pipe.
  18. *
  19. * @param command Command line to start a process with.
  20. * @param mode Data transfer mode for the pipe. Use
  21. * "r" for reading and "w" for writing.
  22. */
  23. TPipeBase(const TString& command, const char* mode);
  24. virtual ~TPipeBase();
  25. protected:
  26. class TImpl;
  27. THolder<TImpl> Impl_;
  28. };
  29. /**
  30. * Input stream that binds to a standard output stream of a newly started process.
  31. *
  32. * Note that if the process ends with non-zero exit status, `Read` function will
  33. * throw an exception.
  34. */
  35. class TPipeInput: protected TPipeBase, public IInputStream {
  36. public:
  37. /**
  38. * Starts a new process and opens a pipe.
  39. *
  40. * @param command Command line to start a process with.
  41. */
  42. TPipeInput(const TString& command);
  43. private:
  44. size_t DoRead(void* buf, size_t len) override;
  45. };
  46. /**
  47. * Output stream that binds to a standard input stream of a newly started process.
  48. *
  49. * Note that if the process ends with non-zero exit status, `Close` function will
  50. * throw an exception.
  51. */
  52. class TPipeOutput: protected TPipeBase, public IOutputStream {
  53. public:
  54. /**
  55. * Starts a new process and opens a pipe.
  56. *
  57. * @param command Command line to start a process with.
  58. */
  59. TPipeOutput(const TString& command);
  60. /**
  61. * Waits for the process to terminate and throws an exception if it ended
  62. * with a non-zero exit status.
  63. */
  64. void Close();
  65. private:
  66. void DoWrite(const void* buf, size_t len) override;
  67. };
  68. class TPipedBase {
  69. protected:
  70. TPipedBase(PIPEHANDLE fd);
  71. virtual ~TPipedBase();
  72. protected:
  73. TPipeHandle Handle_;
  74. };
  75. /**
  76. * Input stream that binds to a standard output stream of an existing process.
  77. */
  78. class TPipedInput: public TPipedBase, public IInputStream {
  79. public:
  80. TPipedInput(PIPEHANDLE fd);
  81. ~TPipedInput() override;
  82. private:
  83. size_t DoRead(void* buf, size_t len) override;
  84. };
  85. /**
  86. * Output stream that binds to a standard input stream of an existing process.
  87. */
  88. class TPipedOutput: public TPipedBase, public IOutputStream {
  89. public:
  90. TPipedOutput(PIPEHANDLE fd);
  91. ~TPipedOutput() override;
  92. private:
  93. void DoWrite(const void* buf, size_t len) override;
  94. };
  95. /** @} */