exec-stream.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Copyright (C) 2004 Artem Khodush
  3. Redistribution and use in source and binary forms, with or without modification,
  4. are permitted provided that the following conditions are met:
  5. 1. Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. 2. Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. 3. The name of the author may not be used to endorse or promote products
  11. derived from this software without specific prior written permission.
  12. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  13. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  14. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  17. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  18. OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  19. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  20. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  21. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #ifndef exec_stream_h
  24. #define exec_stream_h
  25. #include <string>
  26. #include <exception>
  27. #include <istream>
  28. #include <ostream>
  29. #include <vector>
  30. class exec_stream_t {
  31. public:
  32. exec_stream_t();
  33. exec_stream_t( std::string const & program, std::string const & arguments );
  34. template< class iterator > exec_stream_t( std::string const & program, iterator args_begin, iterator args_end );
  35. ~exec_stream_t();
  36. enum stream_kind_t { s_in=1, s_out=2, s_err=4, s_all=s_in|s_out|s_err, s_child=8 };
  37. void set_buffer_limit( int stream_kind, std::size_t size );
  38. typedef unsigned long timeout_t;
  39. void set_wait_timeout( int stream_kind, timeout_t milliseconds );
  40. void set_binary_mode( int stream_kind );
  41. void set_text_mode( int stream_kind );
  42. void start( std::string const & program, std::string const & arguments );
  43. template< class iterator > void start( std::string const & program, iterator args_begin, iterator args_end );
  44. void start( std::string const & program, char const * arg1, char const * arg2 ); // to compensate for damage from the previous one
  45. void start( std::string const & program, char * arg1, char * arg2 );
  46. bool close_in();
  47. bool close();
  48. void kill();
  49. int exit_code();
  50. std::ostream & in();
  51. std::istream & out();
  52. std::istream & err();
  53. typedef unsigned long error_code_t;
  54. class error_t : public std::exception {
  55. public:
  56. error_t( std::string const & msg );
  57. error_t( std::string const & msg, error_code_t code );
  58. ~error_t() throw();
  59. virtual char const * what() const throw();
  60. protected:
  61. error_t();
  62. void compose( std::string const & msg, error_code_t code );
  63. std::string m_msg;
  64. };
  65. private:
  66. exec_stream_t( exec_stream_t const & );
  67. exec_stream_t & operator=( exec_stream_t const & );
  68. struct impl_t;
  69. friend struct impl_t;
  70. impl_t * m_impl;
  71. void exceptions( bool enable );
  72. // helpers for template member functions
  73. void new_impl();
  74. class next_arg_t {
  75. public:
  76. virtual ~next_arg_t()
  77. {
  78. }
  79. virtual std::string const * next()=0;
  80. };
  81. template< class iterator > class next_arg_impl_t : public next_arg_t {
  82. public:
  83. next_arg_impl_t( iterator args_begin, iterator args_end )
  84. : m_args_i( args_begin ), m_args_end( args_end )
  85. {
  86. }
  87. virtual std::string const * next()
  88. {
  89. if( m_args_i==m_args_end ) {
  90. return 0;
  91. }else {
  92. m_arg=*m_args_i;
  93. ++m_args_i;
  94. return &m_arg;
  95. }
  96. }
  97. private:
  98. iterator m_args_i;
  99. iterator m_args_end;
  100. std::string m_arg;
  101. };
  102. void start( std::string const & program, next_arg_t & next_arg );
  103. };
  104. template< class iterator > inline exec_stream_t::exec_stream_t( std::string const & program, iterator args_begin, iterator args_end )
  105. {
  106. new_impl();
  107. exceptions( true );
  108. start( program, args_begin, args_end );
  109. }
  110. template< class iterator > inline void exec_stream_t::start( std::string const & program, iterator args_begin, iterator args_end )
  111. {
  112. exec_stream_t::next_arg_impl_t< iterator > next_arg( args_begin, args_end );
  113. start( program, next_arg );
  114. }
  115. inline void exec_stream_t::start( std::string const & program, char const * arg1, char const * arg2 )
  116. {
  117. std::vector< std::string > args;
  118. args.push_back( std::string( arg1 ) );
  119. args.push_back( std::string( arg2 ) );
  120. start( program, args.begin(), args.end() );
  121. }
  122. inline void exec_stream_t::start( std::string const & program, char * arg1, char * arg2 )
  123. {
  124. std::vector< std::string > args;
  125. args.push_back( std::string( arg1 ) );
  126. args.push_back( std::string( arg2 ) );
  127. start( program, args.begin(), args.end() );
  128. }
  129. #endif