2
* bconsole.cpp - ByteStream wrapper for stdin/stdout
3
* Copyright (C) 2003 Justin Karneges
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
10
* This library 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 GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
using namespace CS_NAMESPACE;
27
#include<qsocketnotifier.h>
31
//----------------------------------------------------------------------------
33
//----------------------------------------------------------------------------
34
class BConsole::Private
39
QSocketNotifier *r, *w;
44
BConsole::BConsole(QObject *parent)
51
// set stdin/stdout to non-block
52
fcntl(0, F_SETFL, O_NONBLOCK);
53
fcntl(1, F_SETFL, O_NONBLOCK);
55
d->r = new QSocketNotifier(0, QSocketNotifier::Read);
56
connect(d->r, SIGNAL(activated(int)), SLOT(sn_read()));
67
bool BConsole::isOpen() const
69
return (!d->closing && !d->closed);
72
void BConsole::close()
74
if(d->closing || d->closed)
77
if(bytesToWrite() > 0)
85
void BConsole::sn_read()
88
int r = ::read(0, a.data(), a.size());
102
void BConsole::sn_write()
107
if(bytesToWrite() > 0)
109
if(bytesToWrite() == 0 && d->closing) {
112
delayedCloseFinished();
116
int BConsole::tryWrite()
118
// try a section of the write buffer
119
QByteArray a = takeWrite(1024, false);
122
int r = ::write(1, a.data(), a.size());
128
d->w = new QSocketNotifier(1, QSocketNotifier::Write);
129
connect(d->w, SIGNAL(activated(int)), SLOT(sn_write()));