Better reporting
[vng.git] / Vng.cpp
blob9e7dd29e5ea2218b614064f6e3177d0a05e3fbfc
1 /*
2 * This file is part of the vng project
3 * Copyright (C) 2008 Thomas Zander <tzander@trolltech.com>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "Vng.h"
20 #include <QProcess>
21 #include <QDebug>
23 using namespace Vng;
25 // fix shortcoming in Qts version for right now, thiago is said to work on a fix
26 // This fixes 2 issues that probably are the same bug;
27 // * readline can return -1 while there is still data to be read on the device.
28 // * readline can return a partial line when the internal (QIOBuffer) seems to have run out.
29 qint64 Vng::readLine(QIODevice *device, char *buffer, int maxLength)
31 int offset = 0;
32 while (maxLength - offset > 2) {
33 qint64 lineLength = device->readLine(buffer + offset, maxLength - offset);
34 if (lineLength < 1) {
35 QProcess *process = qobject_cast<QProcess*>(device);
36 if (process == 0 || process->state() != QProcess::Running)
37 return offset == 0 ? -1 : offset;
38 process->waitForReadyRead();
39 continue;
41 offset += lineLength;
42 if (buffer[offset-1] == '\n')
43 return offset;
45 return offset;