1 #ifndef __PT_PROCESS_H 2 #define __PT_PROCESS_H 3 //======================================================================== 4 // PtProcess.h 5 //======================================================================== 6 // Portable Process with pipes Handling 7 // Features easy File and Pipe/Process opening and reading uniformization 8 // support of read "r" or write "w" popen modes 9 // Copyright F. Costantini 2006 10 // Version 1.0 11 //======================================================================== 12 // This program is free software; you can redistribute it and/or modify 13 // it under the terms of the GNU General Public License v2 as published 14 // by the Free Software Foundation. 15 //======================================================================== 16 17 #ifdef WIN32 18 #include <windows.h> 19 #endif 20 #include <stdio.h> 21 22 class CPtProcess { 23 public: 24 // construction / destruction 25 CPtProcess(); 26 virtual ~CPtProcess(); 27 28 FILE * popen (const char *cmd, const char *mode="r"); 29 FILE * fopen (const char *file, const char *mode="r"); 30 31 int close(); 32 is_open()33 bool is_open() const { return _fpt!=NULL ? true : false ;} desc()34 FILE * desc() const { return _fpt;} 35 get_line(char * line,size_t s)36 char * get_line(char * line, size_t s) const { 37 return _fpt ? fgets(line, s, _fpt) : NULL; 38 } 39 40 #ifdef WIN32 41 public: 42 static bool createPipe(HANDLE * h, BOOL bInheritHnd=TRUE); 43 44 protected: 45 HANDLE pin[2], pout[2], perr[2]; 46 char ptmode; 47 PROCESS_INFORMATION pi; 48 STARTUPINFO si; 49 private: 50 FILE * freeHandles(); 51 static void clean_close(HANDLE& h); 52 #endif 53 54 protected: 55 FILE * _fpt; 56 }; 57 58 #endif 59 60