00001
00002
00003
00004 #ifndef _SystemCmd_h
00005 #define _SystemCmd_h
00006
00007
00008 #include <fstream>
00009 #include <string>
00010 #include <vector>
00011
00012 using std::vector;
00013 using std::ifstream;
00014
00015 #define DBG(x)
00016
00017 #define IDX_STDOUT 0
00018 #define IDX_STDERR 1
00019
00020 enum SpecialTreatment
00021 {
00022 ST_NONE,
00023 ST_NO_ABORT
00024 };
00025
00026 class SystemCmd
00027 {
00028 public:
00029 SystemCmd( const char* Command_Cv,
00030 bool UseTmp_bv=false, SpecialTreatment=ST_NONE );
00031 SystemCmd( bool UseTmp_bv=false, SpecialTreatment=ST_NONE );
00032 virtual ~SystemCmd();
00033 int Execute( string Command_Cv );
00034 int ExecuteBackground( string Command_Cv );
00035 void SetOutputHandler( void (*Handle_f)( void *, string, bool ),
00036 void * Par_p );
00037 int Select( string Reg_Cv, bool Invert_bv=false,
00038 unsigned Idx_ii=IDX_STDOUT );
00039 const string* GetString( unsigned Idx_ii=IDX_STDOUT );
00040 const string* GetLine( unsigned Num_iv, bool Selected_bv=false,
00041 unsigned Idx_ii=IDX_STDOUT );
00042 int NumLines( bool Selected_bv=false, unsigned Idx_ii=IDX_STDOUT );
00043 void SetCombine( const bool Combine_b=true );
00044 void AppendTo( string File_Cv, unsigned Idx_ii=IDX_STDOUT );
00045 int Retcode( ) { return Ret_i; };
00046 string GetFilename( unsigned Idx_ii=IDX_STDOUT );
00047
00048 int GetStdout( vector<string> &Ret_Cr, const bool Append_bv = false )
00049 { return PlaceOutput( IDX_STDOUT, Ret_Cr, Append_bv); }
00050 int GetStderr( vector<string> &Ret_Cr, const bool Append_bv = false )
00051 { return PlaceOutput( IDX_STDERR, Ret_Cr, Append_bv); }
00052
00053 protected:
00054
00055 int PlaceOutput( unsigned Which_iv, vector<string> &Ret_Cr, const bool Append_bv );
00056
00057 void Invalidate();
00058 void InitFile();
00059 void OpenFiles();
00060 int DoExecute( string Cmd_Cv );
00061 bool DoWait( bool Hang_bv, int& Ret_ir );
00062 void InitCmd( string CmdIn_rv, string& CmdRedir_Cr );
00063 void CheckOutput();
00064 void GetUntilEOF( ifstream& File_Cr, vector<string>& Lines_Cr,
00065 bool& NewLineSeen_br, bool Stderr_bv );
00066 void ExtractNewline( char* Buf_ti, int Cnt_ii, bool& NewLineSeen_br,
00067 string& Text_Cr, vector<string>& Lines_Cr );
00068 void AddLine( string Text_Cv, vector<string>& Lines_Cr );
00069
00070 string FileName_aC[2];
00071 string Text_aC[2];
00072 bool Valid_ab[2];
00073 ifstream File_aC[2];
00074 vector<string> Lines_aC[2];
00075 vector<string*> SelLines_aC[2];
00076 bool Append_ab[2];
00077 bool NewLineSeen_ab[2];
00078 bool Combine_b;
00079 bool UseTmp_b;
00080 bool Background_b;
00081 int Ret_i;
00082 int Pid_i;
00083 SpecialTreatment Spec_e;
00084 void (* OutputHandler_f)( void*, string, bool );
00085 void *HandlerPar_p;
00086 static int Nr_i;
00087 };
00088
00089 #endif