00001 /* -*-c++-*- */ 00002 /*************************************************************************** 00003 notQt.h: A set of Qt like functionality, especially related 00004 to the starting of processes. 00005 ------------------- 00006 begin : June 2007 00007 copyright : (C) 2007 Embedded Software Foundry Ltd. (U.K.) 00008 : Author: Sebastian James 00009 email : seb@esfnet.co.uk 00010 ***************************************************************************/ 00011 00012 /*************************************************************************** 00013 * * 00014 * This program is free software; you can redistribute it and/or modify * 00015 * it under the terms of the GNU General Public License as published by * 00016 * the Free Software Foundation; either version 2 of the License, or * 00017 * (at your option) any later version. * 00018 * * 00019 ***************************************************************************/ 00020 00028 #ifndef _NOTQT_H_ 00029 #define _NOTQT_H_ 00030 00031 #include <list> 00032 #include <vector> 00033 #include <string> 00034 #include <fstream> 00035 extern "C" { 00036 #include <sys/poll.h> 00037 } 00038 #define NOTQTPROCESS_MAIN_APP 0 00039 #define NOTQTPROCESS_FAILURE -1 00040 00041 // Possible errors to be generated 00042 #define NOTQPROCNOERROR 0 00043 #define NOTQPROCFAILEDTOSTART 1 00044 #define NOTQPROCCRASHED 2 00045 #define NOTQPROCTIMEDOUT 3 00046 #define NOTQPROCWRITEERR 4 00047 #define NOTQPROCREADERR 5 00048 #define NOTQPROCUNKNOWN 6 00049 00050 using namespace std; 00051 00052 #ifdef DEBUG 00053 extern ofstream debugLogFile; 00054 # define dbgln(msg) debugLogFile << __FUNCTION__ << ": " << msg << endl; 00055 # define dbglln(msg) debugLogFile << __PRETTY_FUNCTION__ << ": " << msg << endl; 00056 # define dbg(msg) debugLogFile << msg; 00057 #else 00058 # define dbgln(msg) 00059 # define dbglln(msg) 00060 # define dbg(msg) 00061 #endif 00062 00063 namespace nxcl { 00064 00070 class notQProcessCallbacks 00071 { 00072 public: 00073 notQProcessCallbacks() {} 00074 virtual ~notQProcessCallbacks() {} 00075 virtual void startedSignal (string) {} 00076 virtual void errorSignal (int) {} 00077 virtual void processFinishedSignal (string) {} 00078 virtual void readyReadStandardOutputSignal (void) {} 00079 virtual void readyReadStandardErrorSignal (void) {} 00080 }; 00081 00085 class notQProcess 00086 { 00087 public: 00088 notQProcess(); 00089 ~notQProcess(); 00093 void writeIn (string& input); 00097 int start (const string& program, const list<string>& args); 00101 void terminate (void); 00102 00111 void probeProcess (void); 00112 00117 pid_t getPid (void) { return this->pid; } 00118 int getError (void) { return this->error; } 00119 void setError (int e) { this->error = e; } 00120 00124 void setCallbacks (notQProcessCallbacks * cb) { this->callbacks = cb; } 00126 00131 string readAllStandardOutput (void); 00132 string readAllStandardError (void); 00138 bool waitForStarted (void); 00140 private: 00144 string progName; 00148 list<string> environment; 00152 int error; 00156 pid_t pid; 00162 bool signalledStart; 00166 int parentToChild[2]; 00170 int childToParent[2]; 00174 int childErrToParent[2]; 00178 struct pollfd * p; 00182 notQProcessCallbacks * callbacks; 00183 }; 00184 00188 class notQTemporaryFile 00189 { 00190 public: 00191 notQTemporaryFile(); 00192 ~notQTemporaryFile(); 00198 void open (void); 00202 void write (string input); 00206 void close (void); 00210 string fileName (void); 00214 void remove (void); 00215 00216 private: 00220 string theFileName; 00224 fstream f; 00225 }; 00226 00230 class notQtUtilities 00231 { 00232 public: 00233 notQtUtilities(); 00234 ~notQtUtilities(); 00235 00237 static string simplify (string& input); 00241 static void splitString (string& line, char token, vector<string>& rtn); 00245 static void splitString (string& line, char token, list<string>& rtn); 00249 static int ensureUnixNewlines (std::string& input); 00250 }; 00251 00252 } // namespace 00253 #endif