~ubuntu-branches/ubuntu/breezy/psi/breezy

« back to all changes in this revision

Viewing changes to cutestuff/util/bconsole.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2004-06-15 00:10:41 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040615001041-enywb6pcpe4sjsw6
Tags: 0.9.2-1
* New upstream release
* Set KDEDIR for ./configure so kde specific files get installed
* Don't install libpsiwidgets.so. It got installed in /usr/share
  where it doesn't belong. May be included (at a better location)
  later.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * bconsole.cpp - ByteStream wrapper for stdin/stdout
 
3
 * Copyright (C) 2003  Justin Karneges
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 *
 
19
 */
 
20
 
 
21
#include"bconsole.h"
 
22
 
 
23
#ifdef CS_NAMESPACE
 
24
using namespace CS_NAMESPACE;
 
25
#endif
 
26
 
 
27
#include<qsocketnotifier.h>
 
28
#include<unistd.h>
 
29
#include<fcntl.h>
 
30
 
 
31
//----------------------------------------------------------------------------
 
32
// BConsole
 
33
//----------------------------------------------------------------------------
 
34
class BConsole::Private
 
35
{
 
36
public:
 
37
        Private() {}
 
38
 
 
39
        QSocketNotifier *r, *w;
 
40
        bool closing;
 
41
        bool closed;
 
42
};
 
43
 
 
44
BConsole::BConsole(QObject *parent)
 
45
:ByteStream(parent)
 
46
{
 
47
        d = new Private;
 
48
        d->closing = false;
 
49
        d->closed = false;
 
50
 
 
51
        // set stdin/stdout to non-block
 
52
        fcntl(0, F_SETFL, O_NONBLOCK);
 
53
        fcntl(1, F_SETFL, O_NONBLOCK);
 
54
 
 
55
        d->r = new QSocketNotifier(0, QSocketNotifier::Read);
 
56
        connect(d->r, SIGNAL(activated(int)), SLOT(sn_read()));
 
57
        d->w = 0;
 
58
}
 
59
 
 
60
BConsole::~BConsole()
 
61
{
 
62
        delete d->w;
 
63
        delete d->r;
 
64
        delete d;
 
65
}
 
66
 
 
67
bool BConsole::isOpen() const
 
68
{
 
69
        return (!d->closing && !d->closed);
 
70
}
 
71
 
 
72
void BConsole::close()
 
73
{
 
74
        if(d->closing || d->closed)
 
75
                return;
 
76
 
 
77
        if(bytesToWrite() > 0)
 
78
                d->closing = true;
 
79
        else {
 
80
                ::fclose(stdout);
 
81
                d->closed = true;
 
82
        }
 
83
}
 
84
 
 
85
void BConsole::sn_read()
 
86
{
 
87
        QByteArray a(1024);
 
88
        int r = ::read(0, a.data(), a.size());
 
89
        if(r < 0) {
 
90
                error(ErrRead);
 
91
        }
 
92
        else if(r == 0) {
 
93
                connectionClosed();
 
94
        }
 
95
        else {
 
96
                a.resize(r);
 
97
                appendRead(a);
 
98
                readyRead();
 
99
        }
 
100
}
 
101
 
 
102
void BConsole::sn_write()
 
103
{
 
104
        d->w->deleteLater();
 
105
        d->w = 0;
 
106
 
 
107
        if(bytesToWrite() > 0)
 
108
                tryWrite();
 
109
        if(bytesToWrite() == 0 && d->closing) {
 
110
                d->closing = false;
 
111
                d->closed = true;
 
112
                delayedCloseFinished();
 
113
        }
 
114
}
 
115
 
 
116
int BConsole::tryWrite()
 
117
{
 
118
        // try a section of the write buffer
 
119
        QByteArray a = takeWrite(1024, false);
 
120
 
 
121
        // write it
 
122
        int r = ::write(1, a.data(), a.size());
 
123
        if(r < 0) {
 
124
                error(ErrWrite);
 
125
                return -1;
 
126
        }
 
127
 
 
128
        d->w = new QSocketNotifier(1, QSocketNotifier::Write);
 
129
        connect(d->w, SIGNAL(activated(int)), SLOT(sn_write()));
 
130
 
 
131
        takeWrite(r);
 
132
        bytesWritten(r);
 
133
        return r;
 
134
}