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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
/* smplayer, GUI front-end for mplayer.
Copyright (C) 2006-2018 Ricardo Villalba <rvm@users.sourceforge.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "myprocess.h"
#include <QDebug>
#ifdef Q_OS_WIN
#if QT_VERSION < 0x040300
#define USE_TEMP_FILE 1
#else
#define USE_TEMP_FILE 0
#endif
#else
#define USE_TEMP_FILE 0
#endif
MyProcess::MyProcess(QObject * parent) : QProcess(parent)
{
clearArguments();
setProcessChannelMode( QProcess::MergedChannels );
#if USE_TEMP_FILE
temp_file.open(); // Create temporary file
QString filename = temp_file.fileName();
setStandardOutputFile( filename );
qDebug("MyProcess::MyProcess: temporary file: %s", filename.toUtf8().data());
temp_file.close();
//connect(&temp_file, SIGNAL(readyRead()), this, SLOT(readTmpFile()) );
connect(&timer, SIGNAL(timeout()), this, SLOT(readTmpFile()) );
#else
connect(this, SIGNAL(readyReadStandardOutput()), this, SLOT(readStdOut()) );
#endif
connect(this, SIGNAL(finished(int, QProcess::ExitStatus)),
this, SLOT(procFinished()) );
// Test splitArguments
//QStringList l = MyProcess::splitArguments("-opt 1 hello \"56 67\" wssx -ios");
}
void MyProcess::clearArguments() {
program = "";
arg.clear();
}
bool MyProcess::isRunning() {
return (state() == QProcess::Running);
}
void MyProcess::addArgument(const QString & a) {
if (program.isEmpty()) {
program = a;
} else {
arg.append(a);
}
}
QStringList MyProcess::arguments() {
QStringList l = arg;
l.prepend(program);
return l;
}
void MyProcess::start() {
remaining_output.clear();
QProcess::start(program, arg);
#if USE_TEMP_FILE
//bool r = temp_file.open(QIODevice::ReadOnly);
bool r = temp_file.open();
timer.start(50);
qDebug("MyProcess::start: r: %d", r);
#endif
}
void MyProcess::readStdOut() {
genericRead( readAllStandardOutput() );
}
void MyProcess::readTmpFile() {
genericRead( temp_file.readAll() );
}
void MyProcess::genericRead(QByteArray buffer) {
QByteArray ba = remaining_output + buffer;
int start = 0;
int from_pos = 0;
int pos = canReadLine(ba, from_pos);
//qDebug("MyProcess::read: pos: %d", pos);
while ( pos > -1 ) {
// Readline
//QByteArray line = ba.left(pos);
QByteArray line = ba.mid(start, pos-start);
//ba = ba.mid(pos+1);
from_pos = pos + 1;
#if defined(Q_OS_WIN) || defined(Q_OS_OS2)
if ((from_pos < ba.size()) && (ba.at(from_pos)=='\n')) from_pos++;
#endif
start = from_pos;
emit lineAvailable(line);
pos = canReadLine(ba, from_pos);
}
remaining_output = ba.mid(from_pos);
}
int MyProcess::canReadLine(const QByteArray & ba, int from) {
int pos1 = ba.indexOf('\n', from);
int pos2 = ba.indexOf('\r', from);
//qDebug("MyProcess::canReadLine: pos2: %d", pos2);
if ( (pos1 == -1) && (pos2 == -1) ) return -1;
int pos = pos1;
if ( (pos1 != -1) && (pos2 != -1) ) {
/*
if (pos2 == (pos1+1)) pos = pos2; // \r\n
else
*/
if (pos1 < pos2) pos = pos1; else pos = pos2;
} else {
if (pos1 == -1) pos = pos2;
else
if (pos2 == -1) pos = pos1;
}
return pos;
}
/*!
Do some clean up, and be sure that all output has been read.
*/
void MyProcess::procFinished() {
qDebug("MyProcess::procFinished");
#if !USE_TEMP_FILE
qDebug() << "MyProcess::procFinished: Bytes available: " << bytesAvailable();
if ( bytesAvailable() > 0 ) readStdOut();
#else
timer.stop();
qDebug() << "MyProcess::procFinished: Bytes available: " << temp_file.bytesAvailable();
if ( temp_file.bytesAvailable() > 0 ) readTmpFile();
qDebug() << "MyProcess::procFinished: Bytes available:" << temp_file.bytesAvailable();
temp_file.close();
#endif
}
QStringList MyProcess::splitArguments(const QString & args) {
qDebug("MyProcess::splitArguments: '%s'", args.toUtf8().constData());
QStringList l;
bool opened_quote = false;
int init_pos = 0;
for (int n = 0; n < args.length(); n++) {
if ((args[n] == QChar(' ')) && (!opened_quote)) {
l.append(args.mid(init_pos, n - init_pos));
init_pos = n+1;
}
else
if (args[n] == QChar('\"')) opened_quote = !opened_quote;
if (n == args.length()-1) {
l.append(args.mid(init_pos, (n - init_pos)+1));
}
}
for (int n = 0; n < l.count(); n++) {
qDebug("MyProcess::splitArguments: arg: %d '%s'", n, l[n].toUtf8().constData());
}
return l;
}
#include "moc_myprocess.cpp"
|