summaryrefslogtreecommitdiffstats
path: root/ProcessUtilities.cc
blob: 3c9037597fa9015c2052cdd400e0dffaf38afaef (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
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
#include "ProcessUtilities.h"
#include "HostPlatform.h"
#if defined(HOST_IS_WINDOWS)
#include "ProcessUtilities.win.cc"
#elif defined(HOST_IS_POSIX)
#include "ProcessUtilities.posix.cc"
#endif
#include "FileSystemUtilities.h"

/* PROCESSWATHCER */

ProcessWatcher::~ProcessWatcher()
{
}


/* PROCESSCONTROLLER */

ProcessController::~ProcessController()
{
}

void ProcessController::add(ProcessWatcher* aWatch)
{
	Public.push_back(aWatch);
}

void ProcessController::signalFinish(int ret)
{
	for (unsigned int i=0; i<Public.size(); ++i)
		Public[i]->processTerminated(this,ret);
}

/* LINEPROCESSLINECONTROLLER */


void LineProcessController::finished(int ret)
{
	if (OutputSoFar.length())
		standardOutputByLine(OutputSoFar);
	OutputSoFar.clear();
	if (ErrorSoFar.length())
		standardErrorByLine(ErrorSoFar);
	ErrorSoFar.clear();
}

void LineProcessController::standardOutput(const std::string& aString)
{
	OutputSoFar += aString;
	std::string EOL(eolTextSequence());
	std::string::size_type idx = OutputSoFar.find(EOL);
	while (idx != std::string::npos)
	{
		standardOutputByLine(OutputSoFar.substr(0,idx));
		OutputSoFar.erase(0,idx+EOL.length());
		idx = OutputSoFar.find(EOL);
	}
}

void LineProcessController::standardError(const std::string& aString)
{
	ErrorSoFar += aString;
	std::string EOL(eolTextSequence());
	std::string::size_type idx = ErrorSoFar.find(EOL);
	while (idx != std::string::npos)
	{
		standardErrorByLine(ErrorSoFar.substr(0,idx));
		ErrorSoFar.erase(0,idx+EOL.length());
		idx = ErrorSoFar.find(EOL);
	}

}

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

		bool Ended;
		int ReturnCode;
		std::string StdOut;
		std::string StdErr;
};

TraceController::TraceController()
: Ended(false), ReturnCode(0)
{
}

void TraceController::finished(int ret)
{
	ReturnCode = ret;
	Ended = true;
}

void TraceController::standardOutput(const std::string& aString)
{
	StdOut += aString;
}

void TraceController::standardError(const std::string& aString)
{
	StdErr += aString;
}

void TraceController::failed()
{
}


int syncExecuteProcess(const std::string& Executable, const std::vector<std::string>& Args,
	std::string &StdOut, std::string& StdErr)
{
	TraceController Tracer;
	bool b = launchProcess(&Tracer,Executable,Args);
	if (!b) return -1;
	while (!Tracer.Ended)
		waitForProcessEvent();
	StdOut = Tracer.StdOut;
	StdErr = Tracer.StdErr;
	return Tracer.ReturnCode;
}