[922] | 1 | #include <QFile>
|
---|
| 2 | #include <QDebug>
|
---|
| 3 | #include <QProcess>
|
---|
| 4 |
|
---|
| 5 | int main (int argc, char *argv[])
|
---|
| 6 | {
|
---|
| 7 | if (argc < 4)
|
---|
| 8 | {
|
---|
| 9 | qDebug("Usage: %s <in_file> <out_file> <child> [<child_arguments>]\n\n"
|
---|
| 10 | "Sends <in_file> to stdin of <child> and puts its stdout to"
|
---|
| 11 | "<out_file>.", argv [0]);
|
---|
| 12 | return 0;
|
---|
| 13 | }
|
---|
| 14 |
|
---|
| 15 | QByteArray data;
|
---|
| 16 |
|
---|
| 17 | QFile qfIn (argv [1]);
|
---|
| 18 | if (qfIn.open (QIODevice::ReadOnly))
|
---|
| 19 | {
|
---|
| 20 | data = qfIn.readAll();
|
---|
| 21 | qfIn.close();
|
---|
| 22 | qDebug() << "Read" << data.size() << "bytes from" << argv [1];
|
---|
| 23 | }
|
---|
| 24 | else
|
---|
| 25 | {
|
---|
| 26 | qDebug() << "Couldn't open" << argv [1] << "for reading";
|
---|
| 27 | return 1;
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | qint64 bytes;
|
---|
| 31 | QProcess proc;
|
---|
| 32 |
|
---|
| 33 | #if 0
|
---|
| 34 | proc.setProcessChannelMode (QProcess::ForwardedChannels);
|
---|
| 35 | #endif
|
---|
| 36 |
|
---|
| 37 | QStringList args;
|
---|
| 38 | for (int i = 4; i < argc; ++ i)
|
---|
| 39 | args << argv [i];
|
---|
| 40 |
|
---|
| 41 | proc.start (argv [3], args, QIODevice::ReadWrite);
|
---|
| 42 |
|
---|
| 43 | if (!proc.waitForStarted())
|
---|
| 44 | {
|
---|
| 45 | qDebug() << "Failed to start" << argv [3] <<":" << proc.errorString();
|
---|
| 46 | return 1;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | qDebug() << "Started" << argv [3];
|
---|
| 50 |
|
---|
| 51 | bytes = proc.write (data);
|
---|
| 52 | qDebug() << "Wrote" << bytes << "bytes to child process";
|
---|
| 53 |
|
---|
| 54 | proc.closeWriteChannel();
|
---|
| 55 | qDebug() << "Waiting for child termination...";
|
---|
| 56 |
|
---|
| 57 | if (!proc.waitForFinished())
|
---|
| 58 | {
|
---|
| 59 | qDebug() << "Child ended abnormally:" << proc.errorString();
|
---|
| 60 | }
|
---|
| 61 | else
|
---|
| 62 | {
|
---|
| 63 | qDebug() << "Child ended normally";
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | data = proc.readAllStandardOutput();
|
---|
| 67 | qDebug() << "Read" << data.size() << "bytes from child process";
|
---|
| 68 |
|
---|
| 69 | QFile qfOut (argv [2]);
|
---|
| 70 | if (qfOut.open (QIODevice::WriteOnly))
|
---|
| 71 | {
|
---|
| 72 | bytes = qfOut.write(data);
|
---|
| 73 | qfOut.close();
|
---|
| 74 | qDebug() << "Wrote" << bytes << "bytes to" << argv [2];
|
---|
| 75 | }
|
---|
| 76 | else
|
---|
| 77 | {
|
---|
| 78 | qDebug() << "Couldn't open" << argv [2] << "for writing";
|
---|
| 79 | return 1;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | qDebug() << "Done.";
|
---|
| 83 |
|
---|
| 84 | return 0;
|
---|
| 85 | }
|
---|