PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: Process.h * 00003 * 00004 * Copyright (C) 2002-2012 The PixelLight Team (http://www.pixellight.org/) 00005 * 00006 * This file is part of PixelLight. 00007 * 00008 * PixelLight is free software: you can redistribute it and/or modify 00009 * it under the terms of the GNU Lesser General Public License as published by 00010 * the Free Software Foundation, either version 3 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * PixelLight is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public License 00019 * along with PixelLight. If not, see <http://www.gnu.org/licenses/>. 00020 \*********************************************************/ 00021 00022 00023 #ifndef __PLCORE_PROCESS_H__ 00024 #define __PLCORE_PROCESS_H__ 00025 #pragma once 00026 00027 00028 //[-------------------------------------------------------] 00029 //[ Includes ] 00030 //[-------------------------------------------------------] 00031 #include "PLCore/System/Pipe.h" 00032 #include "PLCore/File/File.h" 00033 00034 00035 //[-------------------------------------------------------] 00036 //[ Namespace ] 00037 //[-------------------------------------------------------] 00038 namespace PLCore { 00039 00040 00041 //[-------------------------------------------------------] 00042 //[ Classes ] 00043 //[-------------------------------------------------------] 00044 /** 00045 * @brief 00046 * Class for running and controlling an external process 00047 */ 00048 class Process { 00049 00050 00051 //[-------------------------------------------------------] 00052 //[ Public functions ] 00053 //[-------------------------------------------------------] 00054 public: 00055 /** 00056 * @brief 00057 * Constructor 00058 */ 00059 inline Process(); 00060 00061 /** 00062 * @brief 00063 * Destructor 00064 */ 00065 PLCORE_API ~Process(); 00066 00067 /** 00068 * @brief 00069 * Starts a process 00070 * 00071 * @param[in] sCommand 00072 * Command to run 00073 * @param[in] sArguments 00074 * Arguments for the command 00075 */ 00076 PLCORE_API void Execute(const String &sCommand, const String &sArguments); 00077 00078 /** 00079 * @brief 00080 * Starts a process with redirected input/output 00081 * 00082 * @param[in] sCommand 00083 * Command to run 00084 * @param[in] sArguments 00085 * Arguments for the command 00086 * 00087 * @remarks 00088 * This function creates unnamed pipes to redirect input and output. To access 00089 * theses pipes, use the appropriate functions like GetInput(), GetOutput() a.s.o. 00090 * If you want to specify the pipes by yourself, see the other two variations of 00091 * ExecuteRedirectIO(). 00092 */ 00093 PLCORE_API void ExecuteRedirectIO(const String &sCommand, const String &sArguments); 00094 00095 /** 00096 * @brief 00097 * Starts a process with redirected input/output 00098 * 00099 * @param[in] sCommand 00100 * Command to run 00101 * @param[in] sArguments 00102 * Arguments for the command 00103 * @param[in] cPipeIn 00104 * Pipe for input 00105 * @param[in] cPipeOut 00106 * Pipe for output 00107 * @param[in] cPipeErr 00108 * Pipe for error 00109 */ 00110 PLCORE_API void ExecuteRedirectIO(const String &sCommand, const String &sArguments, 00111 const Pipe &cPipeIn, const Pipe &cPipeOut, const Pipe &cPipeErr); 00112 00113 /** 00114 * @brief 00115 * Starts a process with redirected input/output 00116 * 00117 * @param[in] sCommand 00118 * Command to run 00119 * @param[in] sArguments 00120 * Arguments for the command 00121 * @param[in] hPipeIn 00122 * System handle for the input pipe 00123 * @param[in] hPipeOut 00124 * System handle for the output pipe 00125 * @param[in] hPipeErr 00126 * System handle for the error pipe 00127 * 00128 * @note 00129 * - On Linux, the handle is a file handle of type int 00130 * - On Windows, the handle is a file handle of type HANDLE 00131 */ 00132 PLCORE_API void ExecuteRedirectIO(const String &sCommand, const String &sArguments, 00133 handle hPipeIn, handle hPipeOut, handle hPipeErr); 00134 00135 /** 00136 * @brief 00137 * Returns if the process is running 00138 * 00139 * @return 00140 * 'true' if running, else 'false' 00141 */ 00142 PLCORE_API bool IsRunning() const; 00143 00144 /** 00145 * @brief 00146 * Terminates the process 00147 */ 00148 PLCORE_API void Terminate(); 00149 00150 /** 00151 * @brief 00152 * Get input stream 00153 * 00154 * @return 00155 * Input file for the process 00156 */ 00157 inline File &GetInput(); 00158 00159 /** 00160 * @brief 00161 * Get output stream 00162 * 00163 * @return 00164 * Output file for the process 00165 */ 00166 inline File &GetOutput(); 00167 00168 /** 00169 * @brief 00170 * Get error stream 00171 * 00172 * @return 00173 * Error file for the process 00174 */ 00175 inline File &GetError(); 00176 00177 /** 00178 * @brief 00179 * Get input pipe 00180 * 00181 * @return 00182 * Input pipe for the process 00183 */ 00184 inline const Pipe &GetPipeInput() const; 00185 00186 /** 00187 * @brief 00188 * Get output pipe 00189 * 00190 * @return 00191 * Output pipe for the process 00192 */ 00193 inline const Pipe &GetPipeOutput() const; 00194 00195 /** 00196 * @brief 00197 * Get error pipe 00198 * 00199 * @return 00200 * Error pipe for the process 00201 */ 00202 inline const Pipe &GetPipeError() const; 00203 00204 00205 //[-------------------------------------------------------] 00206 //[ Private functions ] 00207 //[-------------------------------------------------------] 00208 private: 00209 /** 00210 * @brief 00211 * Copy constructor 00212 * 00213 * @param[in] cSource 00214 * Source to copy from 00215 */ 00216 Process(const Process &cSource); 00217 00218 /** 00219 * @brief 00220 * Copy operator 00221 * 00222 * @param[in] cSource 00223 * Source to copy from 00224 * 00225 * @return 00226 * Reference to this instance 00227 */ 00228 Process &operator =(const Process &cSource); 00229 00230 /** 00231 * @brief 00232 * Creates a process with redirected IO 00233 * 00234 * @param[in] sCommand 00235 * Command to run 00236 * @param[in] sArguments 00237 * Arguments for the command 00238 * @param[in] hPipeInRd 00239 * System handle for the input pipe (read-end) 00240 * @param[in] hPipeInWr 00241 * System handle for the input pipe (write-end) 00242 * @param[in] hPipeOutRd 00243 * System handle for the output pipe (read-end) 00244 * @param[in] hPipeOutWr 00245 * System handle for the output pipe (write-end) 00246 * @param[in] hPipeErrRd 00247 * System handle for the error pipe (read-end) 00248 * @param[in] hPipeErrWr 00249 * System handle for the error pipe (write-end) 00250 * 00251 * @return 00252 * 'true' if all went fine, else 'false' 00253 */ 00254 bool CreateProcessRedirectIO(const String &sCommand, const String &sArguments, 00255 handle hPipeInRd, handle hPipeInWr, 00256 handle hPipeOutRd, handle hPipeOutWr, 00257 handle hPipeErrRd, handle hPipeErrWr); 00258 00259 00260 //[-------------------------------------------------------] 00261 //[ Private data ] 00262 //[-------------------------------------------------------] 00263 private: 00264 // Input/output redirection 00265 Pipe m_cPipeInput; /**< Pipe 'Input' */ 00266 Pipe m_cPipeOutput; /**< Pipe 'Output' */ 00267 Pipe m_cPipeError; /**< Pipe 'Error' */ 00268 File m_cFileInput; /**< File 'Input' */ 00269 File m_cFileOutput; /**< File 'Output' */ 00270 File m_cFileError; /**< File 'Error' */ 00271 00272 // System dependent process handle 00273 #if defined(LINUX) 00274 int m_hProcess; 00275 #elif defined(WIN32) 00276 handle m_hProcess; 00277 #endif 00278 00279 00280 }; 00281 00282 00283 //[-------------------------------------------------------] 00284 //[ Namespace ] 00285 //[-------------------------------------------------------] 00286 } // PLCore 00287 00288 00289 //[-------------------------------------------------------] 00290 //[ Implementation ] 00291 //[-------------------------------------------------------] 00292 #include "PLCore/System/Process.inl" 00293 00294 00295 #endif // __PLCORE_PROCESS_H__
|