[112] | 1 | /* smplayer, GUI front-end for mplayer.
|
---|
[188] | 2 | Copyright (C) 2006-2017 Ricardo Villalba <rvm@users.sourceforge.net>
|
---|
[112] | 3 |
|
---|
| 4 | This program is free software; you can redistribute it and/or modify
|
---|
| 5 | it under the terms of the GNU General Public License as published by
|
---|
| 6 | the Free Software Foundation; either version 2 of the License, or
|
---|
| 7 | (at your option) any later version.
|
---|
| 8 |
|
---|
| 9 | This program is distributed in the hope that it will be useful,
|
---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 12 | GNU General Public License for more details.
|
---|
| 13 |
|
---|
| 14 | You should have received a copy of the GNU General Public License
|
---|
| 15 | along with this program; if not, write to the Free Software
|
---|
| 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
| 17 | */
|
---|
| 18 |
|
---|
| 19 | #include "myprocess.h"
|
---|
[165] | 20 | #include <QDebug>
|
---|
[112] | 21 |
|
---|
| 22 | #ifdef Q_OS_WIN
|
---|
| 23 |
|
---|
| 24 | #if QT_VERSION < 0x040300
|
---|
| 25 | #define USE_TEMP_FILE 1
|
---|
| 26 | #else
|
---|
| 27 | #define USE_TEMP_FILE 0
|
---|
| 28 | #endif
|
---|
| 29 |
|
---|
| 30 | #else
|
---|
| 31 | #define USE_TEMP_FILE 0
|
---|
| 32 | #endif
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | MyProcess::MyProcess(QObject * parent) : QProcess(parent)
|
---|
| 36 | {
|
---|
| 37 | clearArguments();
|
---|
| 38 | setProcessChannelMode( QProcess::MergedChannels );
|
---|
| 39 |
|
---|
| 40 | #if USE_TEMP_FILE
|
---|
| 41 | temp_file.open(); // Create temporary file
|
---|
| 42 | QString filename = temp_file.fileName();
|
---|
| 43 | setStandardOutputFile( filename );
|
---|
| 44 | qDebug("MyProcess::MyProcess: temporary file: %s", filename.toUtf8().data());
|
---|
| 45 | temp_file.close();
|
---|
| 46 |
|
---|
| 47 | //connect(&temp_file, SIGNAL(readyRead()), this, SLOT(readTmpFile()) );
|
---|
| 48 | connect(&timer, SIGNAL(timeout()), this, SLOT(readTmpFile()) );
|
---|
| 49 | #else
|
---|
| 50 | connect(this, SIGNAL(readyReadStandardOutput()), this, SLOT(readStdOut()) );
|
---|
| 51 | #endif
|
---|
| 52 |
|
---|
| 53 | connect(this, SIGNAL(finished(int, QProcess::ExitStatus)),
|
---|
| 54 | this, SLOT(procFinished()) );
|
---|
| 55 |
|
---|
| 56 | // Test splitArguments
|
---|
| 57 | //QStringList l = MyProcess::splitArguments("-opt 1 hello \"56 67\" wssx -ios");
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | void MyProcess::clearArguments() {
|
---|
| 61 | program = "";
|
---|
| 62 | arg.clear();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | bool MyProcess::isRunning() {
|
---|
| 66 | return (state() == QProcess::Running);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | void MyProcess::addArgument(const QString & a) {
|
---|
| 70 | if (program.isEmpty()) {
|
---|
| 71 | program = a;
|
---|
| 72 | } else {
|
---|
| 73 | arg.append(a);
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | QStringList MyProcess::arguments() {
|
---|
| 78 | QStringList l = arg;
|
---|
| 79 | l.prepend(program);
|
---|
| 80 | return l;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | void MyProcess::start() {
|
---|
| 84 | remaining_output.clear();
|
---|
| 85 |
|
---|
| 86 | QProcess::start(program, arg);
|
---|
| 87 |
|
---|
| 88 | #if USE_TEMP_FILE
|
---|
| 89 | //bool r = temp_file.open(QIODevice::ReadOnly);
|
---|
| 90 | bool r = temp_file.open();
|
---|
| 91 | timer.start(50);
|
---|
| 92 | qDebug("MyProcess::start: r: %d", r);
|
---|
| 93 | #endif
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | void MyProcess::readStdOut() {
|
---|
| 97 | genericRead( readAllStandardOutput() );
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 | void MyProcess::readTmpFile() {
|
---|
| 102 | genericRead( temp_file.readAll() );
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | void MyProcess::genericRead(QByteArray buffer) {
|
---|
| 106 | QByteArray ba = remaining_output + buffer;
|
---|
| 107 | int start = 0;
|
---|
| 108 | int from_pos = 0;
|
---|
| 109 | int pos = canReadLine(ba, from_pos);
|
---|
| 110 |
|
---|
| 111 | //qDebug("MyProcess::read: pos: %d", pos);
|
---|
| 112 | while ( pos > -1 ) {
|
---|
| 113 | // Readline
|
---|
| 114 | //QByteArray line = ba.left(pos);
|
---|
| 115 | QByteArray line = ba.mid(start, pos-start);
|
---|
| 116 | //ba = ba.mid(pos+1);
|
---|
| 117 | from_pos = pos + 1;
|
---|
| 118 | #if defined(Q_OS_WIN) || defined(Q_OS_OS2)
|
---|
| 119 | if ((from_pos < ba.size()) && (ba.at(from_pos)=='\n')) from_pos++;
|
---|
| 120 | #endif
|
---|
| 121 | start = from_pos;
|
---|
| 122 |
|
---|
| 123 | emit lineAvailable(line);
|
---|
| 124 |
|
---|
| 125 | pos = canReadLine(ba, from_pos);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | remaining_output = ba.mid(from_pos);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | int MyProcess::canReadLine(const QByteArray & ba, int from) {
|
---|
| 132 | int pos1 = ba.indexOf('\n', from);
|
---|
| 133 | int pos2 = ba.indexOf('\r', from);
|
---|
| 134 |
|
---|
| 135 | //qDebug("MyProcess::canReadLine: pos2: %d", pos2);
|
---|
| 136 |
|
---|
| 137 | if ( (pos1 == -1) && (pos2 == -1) ) return -1;
|
---|
| 138 |
|
---|
| 139 | int pos = pos1;
|
---|
| 140 | if ( (pos1 != -1) && (pos2 != -1) ) {
|
---|
| 141 | /*
|
---|
| 142 | if (pos2 == (pos1+1)) pos = pos2; // \r\n
|
---|
| 143 | else
|
---|
| 144 | */
|
---|
| 145 | if (pos1 < pos2) pos = pos1; else pos = pos2;
|
---|
| 146 | } else {
|
---|
| 147 | if (pos1 == -1) pos = pos2;
|
---|
| 148 | else
|
---|
| 149 | if (pos2 == -1) pos = pos1;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | return pos;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /*!
|
---|
| 156 | Do some clean up, and be sure that all output has been read.
|
---|
| 157 | */
|
---|
| 158 | void MyProcess::procFinished() {
|
---|
| 159 | qDebug("MyProcess::procFinished");
|
---|
| 160 |
|
---|
| 161 | #if !USE_TEMP_FILE
|
---|
[165] | 162 | qDebug() << "MyProcess::procFinished: Bytes available: " << bytesAvailable();
|
---|
[112] | 163 | if ( bytesAvailable() > 0 ) readStdOut();
|
---|
| 164 | #else
|
---|
| 165 | timer.stop();
|
---|
| 166 |
|
---|
[165] | 167 | qDebug() << "MyProcess::procFinished: Bytes available: " << temp_file.bytesAvailable();
|
---|
[112] | 168 | if ( temp_file.bytesAvailable() > 0 ) readTmpFile();
|
---|
[165] | 169 | qDebug() << "MyProcess::procFinished: Bytes available:" << temp_file.bytesAvailable();
|
---|
[112] | 170 |
|
---|
| 171 | temp_file.close();
|
---|
| 172 | #endif
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | QStringList MyProcess::splitArguments(const QString & args) {
|
---|
| 176 | qDebug("MyProcess::splitArguments: '%s'", args.toUtf8().constData());
|
---|
| 177 |
|
---|
| 178 | QStringList l;
|
---|
| 179 |
|
---|
| 180 | bool opened_quote = false;
|
---|
| 181 | int init_pos = 0;
|
---|
| 182 | for (int n = 0; n < args.length(); n++) {
|
---|
| 183 | if ((args[n] == QChar(' ')) && (!opened_quote)) {
|
---|
| 184 | l.append(args.mid(init_pos, n - init_pos));
|
---|
| 185 | init_pos = n+1;
|
---|
| 186 | }
|
---|
| 187 | else
|
---|
| 188 | if (args[n] == QChar('\"')) opened_quote = !opened_quote;
|
---|
| 189 |
|
---|
| 190 | if (n == args.length()-1) {
|
---|
| 191 | l.append(args.mid(init_pos, (n - init_pos)+1));
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | for (int n = 0; n < l.count(); n++) {
|
---|
| 196 | qDebug("MyProcess::splitArguments: arg: %d '%s'", n, l[n].toUtf8().constData());
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | return l;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | #include "moc_myprocess.cpp"
|
---|