nxcl  @VERSION@
notQt.h
Go to the documentation of this file.
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 #include <unistd.h>
00036 extern "C" {
00037 #include <sys/poll.h>
00038 }
00039 #define NOTQTPROCESS_MAIN_APP 0
00040 #define NOTQTPROCESS_FAILURE -1
00041 
00042 // Possible errors to be generated
00043 #define NOTQPROCNOERROR       0
00044 #define NOTQPROCFAILEDTOSTART 1
00045 #define NOTQPROCCRASHED       2
00046 #define NOTQPROCTIMEDOUT      3
00047 #define NOTQPROCWRITEERR      4
00048 #define NOTQPROCREADERR       5
00049 #define NOTQPROCUNKNOWN       6
00050 
00051 using namespace std;
00052 
00053 #ifdef DEBUG
00054 extern ofstream debugLogFile;
00055 # define dbgln(msg)  debugLogFile << __FUNCTION__ << ": " << msg << endl;
00056 # define dbglln(msg) debugLogFile << __PRETTY_FUNCTION__ << ": " << msg << endl;
00057 # define dbg(msg)    debugLogFile << msg;
00058 #else
00059 # define dbgln(msg)
00060 # define dbglln(msg)
00061 # define dbg(msg)
00062 #endif
00063 
00064 namespace nxcl {
00065 
00071         class notQProcessCallbacks
00072         {
00073         public:
00074                 notQProcessCallbacks() {}
00075                 virtual ~notQProcessCallbacks() {}
00076                 virtual void startedSignal (string) {}
00077                 virtual void errorSignal (int) {}
00078                 virtual void processFinishedSignal (string) {}
00079                 virtual void readyReadStandardOutputSignal (void) {}
00080                 virtual void readyReadStandardErrorSignal (void) {}
00081         };
00082 
00086         class notQProcess
00087         {
00088         public:
00089                 notQProcess();
00090                 ~notQProcess();
00094                 void writeIn (string& input);
00098                 int start (const string& program, const list<string>& args);
00102                 void terminate (void);
00103 
00112                 void probeProcess (void);
00113 
00118                 pid_t getPid (void) { return this->pid; }
00119                 int getError (void) { return this->error; }
00120                 void setError (int e) { this->error = e; }
00121                 
00122                 int getParentFD() 
00123                 { 
00124                         this->parentFD = this->parentToChild[1];
00125                         close(this->childToParent[0]);
00126 
00127                         // Create new pipes
00128                         pipe(this->parentToChild);
00129                         pipe(this->childToParent);
00130 
00131                         return this->parentFD;
00132                 }
00133 
00137                 void setCallbacks (notQProcessCallbacks * cb) { this->callbacks = cb; }
00139 
00144                 string readAllStandardOutput (void);
00145                 string readAllStandardError (void);
00151                 bool waitForStarted (void);
00153         private:
00157                 string progName;
00161                 list<string> environment;
00165                 int error;
00169                 pid_t pid;
00175                 bool signalledStart;
00179                 int parentToChild[2];
00183                 int childToParent[2];
00187                 int childErrToParent[2];
00191                 struct pollfd * p;
00195                 notQProcessCallbacks * callbacks;
00196 
00200                 int parentFD;
00201         };
00202 
00206         class notQTemporaryFile
00207         {
00208         public:
00209                 notQTemporaryFile();
00210                 ~notQTemporaryFile();
00216                 void open (void);
00220                 void write (string input);
00224                 void close (void);
00228                 string fileName (void);
00232                 void remove (void);
00233 
00234         private:
00238                 string theFileName;
00242                 fstream f;
00243         };
00244 
00248         class notQtUtilities
00249         {
00250         public:
00251                 notQtUtilities();
00252                 ~notQtUtilities();
00253 
00255                 static string simplify (string& input);
00259                 static void splitString (string& line, char token, vector<string>& rtn);
00263                 static void splitString (string& line, char token, list<string>& rtn);
00267                 static int ensureUnixNewlines (std::string& input);
00268         };
00269 
00270 } // namespace
00271 #endif