util
processlinemaker.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "processlinemaker.h"
00023
00024 #include <QtCore/QProcess>
00025 #include <QtCore/QStringList>
00026 #include <QtCore/QTimer>
00027
00028 namespace KDevelop
00029 {
00030
00031 class ProcessLineMakerPrivate
00032 {
00033 public:
00034 QByteArray stdoutbuf;
00035 QByteArray stderrbuf;
00036 ProcessLineMaker* p;
00037 QProcess* m_proc;
00038
00039 ProcessLineMakerPrivate( ProcessLineMaker* maker )
00040 : p(maker)
00041 {
00042 }
00043
00044 void slotReadyReadStdout()
00045 {
00046 stdoutbuf += m_proc->readAllStandardOutput();
00047 processStdOut();
00048 }
00049
00050 void processStdOut()
00051 {
00052 QStringList lineList;
00053 int pos;
00054 while ( (pos = stdoutbuf.indexOf('\n')) != -1) {
00055 if (pos > 0 && stdoutbuf.at(pos - 1) == '\r')
00056 lineList << QString::fromLocal8Bit(stdoutbuf, pos - 1);
00057 else
00058 lineList << QString::fromLocal8Bit(stdoutbuf, pos);
00059 stdoutbuf.remove(0, pos+1);
00060 }
00061 emit p->receivedStdoutLines(lineList);
00062 }
00063
00064 void slotReadyReadStderr()
00065 {
00066 stderrbuf += m_proc->readAllStandardError();
00067 processStdErr();
00068 }
00069
00070 void processStdErr()
00071 {
00072 QStringList lineList;
00073 int pos;
00074 while ( (pos = stderrbuf.indexOf('\n')) != -1) {
00075 if (pos > 0 && stderrbuf.at(pos - 1) == '\r')
00076 lineList << QString::fromLocal8Bit(stderrbuf, pos - 1);
00077 else
00078 lineList << QString::fromLocal8Bit(stderrbuf, pos);
00079 stderrbuf.remove(0, pos+1);
00080 }
00081 emit p->receivedStderrLines(lineList);
00082 }
00083
00084 };
00085
00086 ProcessLineMaker::ProcessLineMaker(QObject* parent)
00087 : QObject(parent)
00088 , d( new ProcessLineMakerPrivate( this ) )
00089 {
00090 }
00091
00092 ProcessLineMaker::ProcessLineMaker( QProcess* proc, QObject* parent )
00093 : QObject(parent)
00094 , d( new ProcessLineMakerPrivate( this ) )
00095 {
00096 d->m_proc = proc;
00097 d->m_proc->setTextModeEnabled( true );
00098 connect(proc, SIGNAL(readyReadStandardOutput()),
00099 this, SLOT(slotReadyReadStdout()) );
00100 connect(proc, SIGNAL(readyReadStandardError()),
00101 this, SLOT(slotReadyReadStderr()) );
00102 }
00103
00104 ProcessLineMaker::~ProcessLineMaker()
00105 {
00106 delete d;
00107 }
00108
00109 void ProcessLineMaker::slotReceivedStdout( const QByteArray& buffer )
00110 {
00111 d->stdoutbuf += buffer;
00112 d->processStdOut();
00113 }
00114
00115 void ProcessLineMaker::slotReceivedStderr( const QByteArray& buffer )
00116 {
00117 d->stderrbuf += buffer;
00118 d->processStdErr();
00119 }
00120
00121 void ProcessLineMaker::discardBuffers( )
00122 {
00123 d->stderrbuf.truncate(0);
00124 d->stdoutbuf.truncate(0);
00125 }
00126
00127 void ProcessLineMaker::flushBuffers()
00128 {
00129 if (!d->stdoutbuf.isEmpty())
00130 emit receivedStdoutLines(QStringList(QString::fromLocal8Bit(d->stdoutbuf)));
00131 if (!d->stderrbuf.isEmpty())
00132 emit receivedStderrLines(QStringList(QString::fromLocal8Bit(d->stderrbuf)));
00133 discardBuffers();
00134 }
00135
00136 }
00137
00138 #include "processlinemaker.moc"