00001 //================================================================================================== 00002 // Copyright (C) 2010 Brian Tietz sdbtietz at yahoo dot com 00003 // 00004 // This program is free software; you can redistribute it and/or modify it under the terms of the 00005 // GNU General Public License as published by the Free Software Foundation, version 2.0 of the 00006 // License. 00007 // 00008 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 00009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00010 // General Public License for more details. 00011 // 00012 // You should have received a copy of the GNU General Public License along with this program; if 00013 // not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00014 // 02110-1301, USA. 00015 // 00016 // For commercial software, the copyright holder (Brian Tietz, email sdbtietz at yahoo dot com) 00017 // reserves the right and is willing to waive the proprietary source code disclosure aspects of that 00018 // license as applied to the UT library in exchange for either substantial contributions to the 00019 // development of the UT library or other forms of compensation. Any such waiver must be 00020 // established in writing between the copyright holder and the commercial entity obtaining such a 00021 // waiver. 00022 //================================================================================================== 00023 00024 00025 #ifndef _UT_COMMAND_INPUT_STREAM_H_ 00026 #define _UT_COMMAND_INPUT_STREAM_H_ 00027 00028 00029 //================================================================================================== 00030 //=== System headers 00031 //================================================================================================== 00032 #include <stdio.h> 00033 00034 00035 //================================================================================================== 00036 //=== Project headers 00037 //================================================================================================== 00038 #include "UTIOStreams.h" 00039 00040 00041 //================================================================================================== 00042 class UT_EXPORT CommandInputStream_t 00043 //================================================================================================== 00044 : public InputStream_t 00048 { 00049 //---------------------------------------------------------------------------------------------- 00050 public: 00051 //---------------------------------------------------------------------------------------------- 00052 CommandInputStream_t(); 00055 00056 virtual ~CommandInputStream_t(); 00060 00061 inline bool Execute(const utf8* command); 00065 00066 inline bool Execute(stringliteral* command); 00070 00071 bool Execute(const String_t& command); 00075 00076 virtual Status_t Read( byte* buffer, 00077 int bytes_to_read, 00078 bool force_read_all = true ); 00085 00086 virtual Status_t ReadLine(out String_t* line); 00092 00093 virtual Status_t Peek( byte* buffer, int bytes_to_read ); 00099 00100 virtual Status_t Position(out fileoff_t* position); 00108 00109 virtual Status_t Seek(fileoff_t position); 00116 00117 virtual Status_t Close(); 00123 00124 int ExitStatus(); 00127 00128 //---------------------------------------------------------------------------------------------- 00129 private: 00130 //---------------------------------------------------------------------------------------------- 00131 int ReadToBuffer(int min_attempt_read_bytes); 00132 // Buffers at least min_attempt_read_bytes unless end-of-file is 00133 // reached. Returns eERR_end_of_file if nothing could be read. Buffers 00134 // more than what was read if it would be more efficient under current 00135 // buffer size conditions. 00136 00137 //---------------------------------------------------------------------------------------------- 00138 private: 00139 //---------------------------------------------------------------------------------------------- 00140 bool m_executing; 00141 bool m_prev_error; 00142 FILE* m_pipe; 00143 int m_exit_status; 00144 00145 fileoff_t m_pos; 00146 byte* m_read_buffer; 00147 int m_read_buffer_size; 00148 int m_read_buffer_valid_size; 00149 int m_read_buffer_pos; 00150 }; 00151 00152 00153 00154 00155 //================================================================================================== 00156 //================================================================================================== 00157 //=== 00158 //=== Inline function implementations 00159 //=== 00160 //================================================================================================== 00161 //================================================================================================== 00162 inline bool 00163 CommandInputStream_t::Execute(const utf8* command) 00164 { 00165 return Execute( String_t( reinterpret_cast<stringliteral*>(command) ) ); 00166 } 00167 00168 00169 inline bool 00170 CommandInputStream_t::Execute(stringliteral* command) 00171 { 00172 return Execute( String_t(command) ); 00173 } 00174 00175 00176 #endif // _UT_COMMAND_INPUT_STREAM_H_