2d9677254fa073d197f449edefa167d89b55e3ae
[quassel.git] / src / contrib / libqxt-2007-10-24 / src / core / qxtstdio.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
4 **
5 ** This file is part of the QxtCore module of the Qt eXTension library
6 **
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
9 ** IBM.
10 **
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
15 **
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
20 **
21 ** <http://libqxt.sourceforge.net>  <foundation@libqxt.org>
22 **
23 ****************************************************************************/
24 #include "qxtstdio.h"
25 #include <cstdio>
26 #include <QSocketNotifier>
27
28 /**
29 \class QxtStdio QxtStdio
30
31 \ingroup QxtCore
32
33 \brief QIODevice support for stdin and stdout
34
35 including readyRead() signal
36 note that when using this class, the buffers for stdin/stdout will be disabled, and NOT reenabled on destruction
37
38 perfect as a counter part for QProcess
39 */
40
41 QxtStdio::QxtStdio(QObject * parent):QIODevice(parent)
42 {
43     setvbuf ( stdin , NULL , _IONBF , 0 );
44     setvbuf ( stdout , NULL , _IONBF , 0 );
45
46     setOpenMode (QIODevice::ReadWrite);
47     notify = new QSocketNotifier (
48
49 #ifdef Q_CC_MSVC
50                  _fileno(stdin)
51 #else
52                  fileno(stdin)
53 #endif
54
55                  ,QSocketNotifier::Read,this );
56     connect(notify, SIGNAL(activated(int)),this,SLOT(activated(int)));
57 }
58
59 qint64 QxtStdio::readData ( char * data, qint64 maxSize )
60 {
61     qint64 i=0;
62     for (;i<maxSize;i++)
63     {
64         if (inbuffer.isEmpty())
65             break;
66         (*data++)=inbuffer.dequeue();
67     }
68     return i;
69 }
70 qint64 QxtStdio::writeData ( const char * data, qint64 maxSize )
71 {
72     qint64 i=0;
73     for (;i<maxSize;i++)
74     {
75         char c=*data++;
76         putchar(c);
77     }
78 //      emit(bytesWritten (i)); ///FIXME: acording to the docs this may not be recoursive. how do i prevent that?
79     return i;
80 }
81
82
83 bool QxtStdio::isSequential () const
84 {
85     return true;
86 }
87
88
89 qint64 QxtStdio::bytesAvailable () const
90 {
91     return inbuffer.count();
92 }
93
94 void QxtStdio::activated(int )
95 {
96     inbuffer.enqueue(getchar());
97     emit(readyRead ());
98 }
99
100