Fix no newlines warnings. Patch by Peter Oberndorfer
[kdevelopdvcssupport.git] / util / processlinemaker.cpp
blob123c473ad6a22029160889a0224b29ef2525924c
1 /* This file is part of the KDE project
2 Copyright 2002 John Firebaugh <jfirebaugh@kde.org>
3 Copyright 2007 Andreas Pakulat <apaku@gmx.de>
4 Copyright 2007 Oswald Buddenhagen <ossi@kde.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
22 #include "processlinemaker.h"
24 #include <QtCore/QProcess>
25 #include <QtCore/QStringList>
26 #include <QtCore/QTimer>
28 namespace KDevelop
31 class ProcessLineMakerPrivate
33 public:
34 QByteArray stdoutbuf;
35 QByteArray stderrbuf;
36 ProcessLineMaker* p;
37 QProcess* m_proc;
39 ProcessLineMakerPrivate( ProcessLineMaker* maker )
40 : p(maker)
44 void slotReadyReadStdout()
46 stdoutbuf += m_proc->readAllStandardOutput();
47 processStdOut();
50 void processStdOut()
52 QStringList lineList;
53 int pos;
54 while ( (pos = stdoutbuf.indexOf('\n')) != -1) {
55 if (pos > 0 && stdoutbuf.at(pos - 1) == '\r')
56 lineList << QString::fromLocal8Bit(stdoutbuf, pos - 1);
57 else
58 lineList << QString::fromLocal8Bit(stdoutbuf, pos);
59 stdoutbuf.remove(0, pos+1);
61 emit p->receivedStdoutLines(lineList);
64 void slotReadyReadStderr()
66 stderrbuf += m_proc->readAllStandardError();
67 processStdErr();
70 void processStdErr()
72 QStringList lineList;
73 int pos;
74 while ( (pos = stderrbuf.indexOf('\n')) != -1) {
75 if (pos > 0 && stderrbuf.at(pos - 1) == '\r')
76 lineList << QString::fromLocal8Bit(stderrbuf, pos - 1);
77 else
78 lineList << QString::fromLocal8Bit(stderrbuf, pos);
79 stderrbuf.remove(0, pos+1);
81 emit p->receivedStderrLines(lineList);
86 ProcessLineMaker::ProcessLineMaker(QObject* parent)
87 : QObject(parent)
88 , d( new ProcessLineMakerPrivate( this ) )
92 ProcessLineMaker::ProcessLineMaker( QProcess* proc, QObject* parent )
93 : QObject(parent)
94 , d( new ProcessLineMakerPrivate( this ) )
96 d->m_proc = proc;
97 d->m_proc->setTextModeEnabled( true );
98 connect(proc, SIGNAL(readyReadStandardOutput()),
99 this, SLOT(slotReadyReadStdout()) );
100 connect(proc, SIGNAL(readyReadStandardError()),
101 this, SLOT(slotReadyReadStderr()) );
104 ProcessLineMaker::~ProcessLineMaker()
106 delete d;
109 void ProcessLineMaker::slotReceivedStdout( const QByteArray& buffer )
111 d->stdoutbuf += buffer;
112 d->processStdOut();
115 void ProcessLineMaker::slotReceivedStderr( const QByteArray& buffer )
117 d->stderrbuf += buffer;
118 d->processStdErr();
121 void ProcessLineMaker::discardBuffers( )
123 d->stderrbuf.truncate(0);
124 d->stdoutbuf.truncate(0);
127 void ProcessLineMaker::flushBuffers()
129 if (!d->stdoutbuf.isEmpty())
130 emit receivedStdoutLines(QStringList(QString::fromLocal8Bit(d->stdoutbuf)));
131 if (!d->stderrbuf.isEmpty())
132 emit receivedStderrLines(QStringList(QString::fromLocal8Bit(d->stderrbuf)));
133 discardBuffers();
138 #include "processlinemaker.moc"