File: notQt.h

package info (click to toggle)
nxcl 0.9-3.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch, wheezy
  • size: 560 kB
  • ctags: 412
  • sloc: cpp: 3,149; makefile: 114
file content (253 lines) | stat: -rw-r--r-- 6,466 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* -*-c++-*- */
/***************************************************************************
           notQt.h: A set of Qt like functionality, especially related
                       to the starting of processes.
                             -------------------
    begin                : June 2007
    copyright            : (C) 2007 Embedded Software Foundry Ltd. (U.K.)
                         :     Author: Sebastian James
    email                : seb@esfnet.co.uk
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

/*!
 * \file notQt.h Simple re-implementations of some Qt-like
 * functionality.  In particular, there's a QProcess-like (though much
 * simplified) class, a QTemporaryFile like class and a couple of the
 * methods that you get with QString.
 */

#ifndef _NOTQT_H_
#define _NOTQT_H_

#include <list>
#include <vector>
#include <string>
#include <fstream>
extern "C" {
#include <sys/poll.h>
}
#define NOTQTPROCESS_MAIN_APP 0
#define NOTQTPROCESS_FAILURE -1

// Possible errors to be generated
#define NOTQPROCNOERROR       0
#define NOTQPROCFAILEDTOSTART 1
#define NOTQPROCCRASHED       2
#define NOTQPROCTIMEDOUT      3
#define NOTQPROCWRITEERR      4
#define NOTQPROCREADERR       5
#define NOTQPROCUNKNOWN       6

using namespace std;

#ifdef DEBUG
extern ofstream debugLogFile;
# define dbgln(msg)  debugLogFile << __FUNCTION__ << ": " << msg << endl;
# define dbglln(msg) debugLogFile << __PRETTY_FUNCTION__ << ": " << msg << endl;
# define dbg(msg)    debugLogFile << msg;
#else
# define dbgln(msg)
# define dbglln(msg)
# define dbg(msg)
#endif

namespace nxcl {

	/*!
	 * A set of virtual callbacks. These should be derived in the
	 * client code. They're called by notQProcess via the
	 * notQProcessCallbacks* callbacks member variable.
	 */
	class notQProcessCallbacks
	{
	public:
		notQProcessCallbacks() {}
		virtual ~notQProcessCallbacks() {}
		virtual void startedSignal (string) {}
		virtual void errorSignal (int) {}
		virtual void processFinishedSignal (string) {}
		virtual void readyReadStandardOutputSignal (void) {}
		virtual void readyReadStandardErrorSignal (void) {}
	};

	/*!
	 * notQProcess is a simple replacement for the Qt class QProcess.
	 */
	class notQProcess
	{
	public:
		notQProcess();
		~notQProcess();
		/*!
		 * Write \arg input to the stdin of the process.
		 */
		void writeIn (string& input);
		/*!
		 * fork and exec the process.
		 */
		int start (const string& program, const list<string>& args);
		/*!
		 * Send a TERM signal to the process.
		 */
		void terminate (void);

		/*!
		 * poll to see if there is data on stderr or stdout
		 * and to see if the process has exited.
		 *
		 * This must be called on a scheduled basis. It checks
		 * for any stdout/stderr data and also checks whether
		 * the process is still running.
		 */
		void probeProcess (void);

		/*!
		 * Accessors
		 */
		//@{
		pid_t getPid (void) { return this->pid; }
		int getError (void) { return this->error; }
		void setError (int e) { this->error = e; }

		/*!
		 * Setter for the callbacks.
		 */
		void setCallbacks (notQProcessCallbacks * cb) { this->callbacks = cb; }
		//@}

		/*! 
		 * Slots
		 */
		//@{
		string readAllStandardOutput (void);
		string readAllStandardError (void);
		/*!
		 * Wait for the process to get itself going. Do this
		 * by looking at pid.  If no pid after a while,
		 * return false.
		 */
		bool waitForStarted (void);
		//@}
	private:
		/*!
		 * The name of the program to execute
		 */
		string progName;
		/*!
		 * The environment and arguments of the program to execute
		 */
		list<string> environment;
		/*!
		 * Holds a notQProcess error, defined above. NOTQPROCNOERROR, etc.
		 */
		int error;
		/*!
		 * Process ID of the program
		 */
		pid_t pid;
		/*!
		 * Set to true if the fact that the program has been
		 * started has been signalled using the callback
		 * callbacks->startedSignal
		 */
		bool signalledStart;
		/*!
		 * stdin parent to child
		 */
		int parentToChild[2];
		/*!
		 * stdout child to parent
		 */
		int childToParent[2];
		/*!
		 * stderr child to parent
		 */
		int childErrToParent[2];
		/*!
		 * Used in the poll() call in probeProcess()
		 */
		struct pollfd * p;
		/*!
		 * Pointer to a callback object
		 */
		notQProcessCallbacks * callbacks;
	};

	/*!
	 * A simple replacement for the Qt Class QTemporaryFile.
	 */
	class notQTemporaryFile
	{
	public:
		notQTemporaryFile();
		~notQTemporaryFile();
		/*!
		 * Open a file with a (not really) random name. The
		 * filename will be /tmp/notQtXXXXXX where XXXXXX will
		 * be the time in seconds since the unix epoch.
		 */
		void open (void);
		/*!
		 * Write \arg input to the temporary file.
		 */
		void write (string input);
		/*!
		 * Close the temporary file's stream.
		 */
		void close (void);
		/*!
		 * A getter for the file name of the temporary file
		 */
		string fileName (void);
		/*!
		 * Remove the temporary file
		 */
		void remove (void);

	private:
		/*!
		 * The file name of the temporary file
		 */
		string theFileName;
		/*!
		 * The file stream for the temporary file
		 */
		fstream f;
	};

	/*!
	 * A few useful utility functions.
	 */
	class notQtUtilities
	{
	public:
		notQtUtilities();
		~notQtUtilities();

		/*! The same (more or less) as Qt QString::simplified */
		static string simplify (string& input);
		/*!
		 * Split a string 'line' based on token, placing the portions in the vector rtn
		 */
		static void splitString (string& line, char token, vector<string>& rtn);
		/*!
		 * Split a string 'line' based on token, placing the portions in the list rtn
		 */
		static void splitString (string& line, char token, list<string>& rtn);
		/*!
		 * Run through input and replace any DOS newlines with unix newlines.
		 */
		static int ensureUnixNewlines (std::string& input);
	};

} // namespace
#endif