• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • KDevelop Platform Libraries
  • Sitemap
  • Contact Us
 

util

processlinemaker.cpp

00001 /* This file is part of the KDE project
00002    Copyright 2002 John Firebaugh <jfirebaugh@kde.org>
00003    Copyright 2007 Andreas Pakulat <apaku@gmx.de>
00004    Copyright 2007 Oswald Buddenhagen <ossi@kde.org>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019    Boston, MA 02110-1301, USA.
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"

util

Skip menu "util"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

KDevelop Platform Libraries

Skip menu "KDevelop Platform Libraries"
  • interfaces
  • language
  •   duchain
  •   editor
  • outputview
  • project
  • shell
  • sublime
  • util
  • vcs
Generated for KDevelop Platform Libraries by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal