summaryrefslogtreecommitdiffstats
path: root/ProcessUtilities.h
blob: 5de8065fb619a06fdfe375d33d048d70983d6eb2 (plain) (blame)
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
#ifndef PROCESSUTILITIES_H_
#define PROCESSUTILITIES_H_

#include <iostream>
#include <string>
#include <vector>

class ProcessWatcher;
class ProcessInfo;
class ProcessController
{
	public:
		virtual ~ProcessController() = 0;

		virtual void finished(int ret) = 0;
		virtual void standardOutput(const std::string& aString) = 0;
		virtual void standardError(const std::string& aString) = 0;
		virtual void failed() = 0;
		
		void add(ProcessWatcher* aWatch);
		void signalFinish(int ret);
	private:
		std::vector<ProcessWatcher*> Public;
		friend class ProcessInfo;
};

class LineProcessController : public ProcessController
{
	public:
		virtual void finished(int ret);
		virtual void standardOutput(const std::string& aString);
		virtual void standardError(const std::string& aString);

		virtual void standardOutputByLine(const std::string& aString) = 0;
		virtual void standardErrorByLine(const std::string& aString) = 0;
	private:
		std::string OutputSoFar;
		std::string ErrorSoFar;
};

class ProcessWatcher
{
	public:
		virtual ~ProcessWatcher() = 0;
		virtual void processTerminated(ProcessController* Which, int ret) = 0;
};

class TestController : public ProcessController
{
	public:
		TestController(const std::string& n)
		: N(n)
		{
		}

		void finished(int ret)
		{
			std::cout << N << " finished with " << ret << std::endl;
		}

		void standardError(const std::string& Out)
		{
			std::cout << N << " ERR: " << Out << std::endl;
		}

		void standardOutput(const std::string& Out)
		{
			std::cout << N << " : " << Out << std::endl;
		}

		void failed()
		{
			std::cout << N << " failed" << std::endl;
		}

		std::string N;
};


bool launchProcess(ProcessController* aController, 
	const std::string& Executable, const std::vector<std::string>& Args);
bool shellLaunchProcess(ProcessController* aController, 
	const std::string& Executable, const std::vector<std::string>& Args);

unsigned int waitForProcessEvent();

int syncExecuteProcess(const std::string& Executable, const std::vector<std::string>& Args,
	std::string &StdOut, std::string& StdErr);

#endif