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
|
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc
** All rights reserved.
** For any questions to Digia, please use the contact form at
** http://qt.digia.com/
**
** This file is part of Qt Enterprise Embedded.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** the contact form at http://qt.digia.com/
**
****************************************************************************/
#include <QCoreApplication>
#include <QProcess>
#include <QDebug>
#include <QElapsedTimer>
#include <QSocketNotifier>
#include <QFile>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QStringList args = app.arguments();
args.removeFirst();
if (args.size() == 0) {
qDebug() << "Usage: Program to excute";
return 1;
}
QList<qint32> timings;
QElapsedTimer timer;
QProcess p;
p.setProcessChannelMode(QProcess::MergedChannels);
QSocketNotifier notifier(0, QSocketNotifier::Read);
QFile standardInput;
if (!standardInput.open(0, QFile::ReadOnly))
qFatal("open");
QObject::connect(¬ifier, &QSocketNotifier::activated, [&p, &standardInput](){ p.write(standardInput.readAll());});
QObject::connect(&p, (void (QProcess::*)(int, QProcess::ExitStatus))&QProcess::finished, [&timings, &timer](int exitCode, QProcess::ExitStatus exitStatus) {
timings += -timer.elapsed();
if (exitStatus != QProcess::NormalExit || exitCode != 0) {
qDebug() << "Something went wrong";
qApp->exit(1);
return;
}
qDebug() << "Doing calculations";
double factor = 100.0/abs(timings.last());
for (int i = 0; i < timings.size(); ++i) {
qint32 value = timings[i];
qint32 output = value * factor;
QString text;
if (i == timings.size()-1)
fprintf(stdout, "%d\n", value);
else {
if (output == 0 && value < 0)
fprintf(stdout, "-0\n");
else
fprintf(stdout, "%d\n", output);
}
}
qApp->exit(0);
});
QObject::connect(&p, &QProcess::readyRead, [&p, &timer, &timings]() {
while (p.canReadLine()) {
QByteArray line = p.readLine();
line.chop(1);
qDebug() << line;
if (line.startsWith("+"))
timings += -timer.elapsed();
else
timings += timer.elapsed();
}
});
p.start("/bin/sh", QStringList() << "-x" << args);
timer.start();
if (!p.waitForStarted())
qFatal("Failed to start");
return app.exec();
}
|