• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDE3Support

  • kde3support
  • kdecore
k3procio.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 1997 David Sweet <dsweet@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*/
18
19#include "k3procio.h"
20
21#include <config.h>
22#include <stdio.h>
23
24#include <kdebug.h>
25#include <QtCore/QTextCodec>
26
27class KProcIOPrivate
28{
29public:
30 KProcIOPrivate( QTextCodec* c )
31 : codec( c ),
32 rbi( 0 ),
33 readsignalon( true ),
34 writeready( true ),
35 comm(K3Process::All)
36 {
37 }
38
39 QList<QByteArray *> outbuffer;
40 QByteArray recvbuffer;
41 QTextCodec *codec;
42 int rbi;
43 bool needreadsignal;
44 bool readsignalon;
45 bool writeready;
46 K3Process::Communication comm;
47};
48
49K3ProcIO::K3ProcIO ( QTextCodec *_codec)
50 : d( new KProcIOPrivate( _codec ) )
51{
52 if ( !d->codec ) {
53 d->codec = QTextCodec::codecForName( "ISO 8859-1" );
54 if ( !d->codec ) {
55 kError( 174 ) << "Can't create ISO 8859-1 codec!" << endl;
56 }
57 }
58}
59
60K3ProcIO::~K3ProcIO()
61{
62 qDeleteAll( d->outbuffer );
63 delete d;
64}
65
66void
67K3ProcIO::resetAll ()
68{
69 if (isRunning()) {
70 kill();
71 }
72
73 clearArguments();
74 d->rbi = 0;
75 d->readsignalon = true;
76 d->writeready = true;
77
78 disconnect (this, SIGNAL (receivedStdout(K3Process*,char*,int)),
79 this, SLOT (received(K3Process*,char*,int)));
80
81 disconnect (this, SIGNAL (receivedStderr(K3Process*,char*,int)),
82 this, SLOT (received(K3Process*,char*,int)));
83
84 disconnect (this, SIGNAL (wroteStdin(K3Process*)),
85 this, SLOT (sent(K3Process*)));
86
87 qDeleteAll( d->outbuffer );
88 d->outbuffer.clear();
89}
90
91void K3ProcIO::setComm (Communication comm)
92{
93 d->comm = comm;
94}
95
96bool K3ProcIO::start (RunMode runmode, bool includeStderr)
97{
98 connect (this, SIGNAL (receivedStdout(K3Process*,char*,int)),
99 this, SLOT (received(K3Process*,char*,int)));
100
101 if (includeStderr)
102 {
103 connect (this, SIGNAL (receivedStderr(K3Process*,char*,int)),
104 this, SLOT (received(K3Process*,char*,int)));
105 }
106
107 connect (this, SIGNAL (wroteStdin(K3Process*)),
108 this, SLOT (sent(K3Process*)));
109
110 return K3Process::start (runmode, d->comm);
111}
112
113bool K3ProcIO::writeStdin (const QString &line, bool appendnewline)
114{
115 return writeStdin( d->codec->fromUnicode( line ), appendnewline );
116}
117
118bool K3ProcIO::writeStdin (const QByteArray &line, bool appendnewline)
119{
120 QByteArray *qs = new QByteArray(line);
121
122 if (appendnewline)
123 {
124 *qs += '\n';
125 }
126
127 int l = qs->length();
128 if (!l)
129 {
130 delete qs;
131 return true;
132 }
133
134 QByteArray *b = (QByteArray *) qs;
135 b->truncate(l); // Strip trailing null
136
137 d->outbuffer.append(b);
138
139 if ( d->writeready ) {
140 d->writeready = false;
141 return K3Process::writeStdin( b->data(), b->size() );
142 }
143
144 return true;
145}
146
147bool K3ProcIO::writeStdin(const QByteArray &data)
148{
149 if (!data.size()) {
150 return true;
151 }
152 QByteArray *b = new QByteArray(data);
153 d->outbuffer.append(b);
154
155 if ( d->writeready ) {
156 d->writeready=false;
157 return K3Process::writeStdin( b->data(), b->size() );
158 }
159
160 return true;
161}
162
163void K3ProcIO::closeWhenDone()
164{
165 if (d->writeready) {
166 closeStdin();
167 return;
168 }
169 d->outbuffer.append(0);
170
171 return;
172}
173
174void K3ProcIO::sent(K3Process *)
175{
176 d->outbuffer.removeFirst();
177
178 if ( d->outbuffer.count() == 0 ) {
179 d->writeready = true;
180 } else {
181 QByteArray *b = d->outbuffer.first();
182 if (!b) {
183 closeStdin();
184 } else {
185 K3Process::writeStdin(b->data(), b->size());
186 }
187 }
188}
189
190void K3ProcIO::received (K3Process *, char *buffer, int buflen)
191{
192 d->recvbuffer += QByteArray(buffer, buflen);
193
194 controlledEmission();
195}
196
197void K3ProcIO::ackRead ()
198{
199 d->readsignalon = true;
200 if ( d->needreadsignal || d->recvbuffer.length() != 0 ) {
201 controlledEmission();
202 }
203}
204
205void K3ProcIO::controlledEmission ()
206{
207 if ( d->readsignalon ) {
208 d->needreadsignal = false;
209 d->readsignalon = false; //will stay off until read is acknowledged
210 emit readReady (this);
211 } else {
212 d->needreadsignal = true;
213 }
214}
215
216void K3ProcIO::enableReadSignals (bool enable)
217{
218 d->readsignalon = enable;
219
220 if ( enable && d->needreadsignal ) {
221 emit readReady(this);
222 }
223}
224
225int K3ProcIO::readln (QString &line, bool autoAck, bool *partial)
226{
227 int len;
228
229 if ( autoAck ) {
230 d->readsignalon=true;
231 }
232
233 //need to reduce the size of recvbuffer at some point...
234
235 len = d->recvbuffer.indexOf('\n', d->rbi) - d->rbi;
236
237 //kDebug(174) << "KPIO::readln" << endl;
238
239 //in case there's no '\n' at the end of the buffer
240 if ( ( len < 0 ) &&
241 ( d->rbi < d->recvbuffer.length() ) ) {
242 d->recvbuffer = d->recvbuffer.mid( d->rbi );
243 d->rbi = 0;
244 if (partial)
245 {
246 len = d->recvbuffer.length();
247 line = d->recvbuffer;
248 d->recvbuffer = "";
249 *partial = true;
250 return len;
251 }
252 return -1;
253 }
254
255 if (len>=0)
256 {
257 line = d->codec->toUnicode( d->recvbuffer.mid( d->rbi, len ) );
258 d->rbi += len + 1;
259 if (partial)
260 *partial = false;
261 return len;
262 }
263
264 d->recvbuffer = "";
265 d->rbi = 0;
266
267 //-1 on return signals "no more data" not error
268 return -1;
269
270}
271
272#include "k3procio.moc"
273
K3ProcIO::controlledEmission
void controlledEmission()
Definition: k3procio.cpp:205
K3ProcIO::~K3ProcIO
~K3ProcIO()
Destructor.
Definition: k3procio.cpp:60
K3ProcIO::sent
void sent(K3Process *)
Definition: k3procio.cpp:174
K3ProcIO::closeWhenDone
void closeWhenDone()
Closes stdin after all data has been send.
Definition: k3procio.cpp:163
K3ProcIO::readReady
void readReady(K3ProcIO *pio)
Emitted when the process is ready for reading.
K3ProcIO::setComm
void setComm(Communication comm)
Sets the communication mode to be passed to K3Process::start() by start().
Definition: k3procio.cpp:91
K3ProcIO::resetAll
void resetAll()
Reset the class.
Definition: k3procio.cpp:67
K3ProcIO::enableReadSignals
void enableReadSignals(bool enable)
Turns readReady() signals on and off.
Definition: k3procio.cpp:216
K3ProcIO::K3ProcIO
K3ProcIO(QTextCodec *codec=0)
Constructor.
Definition: k3procio.cpp:49
K3ProcIO::start
bool start(RunMode runmode=NotifyOnExit, bool includeStderr=false)
Starts the process.
Definition: k3procio.cpp:96
K3ProcIO::writeStdin
bool writeStdin(const QString &line, bool appendnewline=true)
Writes text to stdin of the process.
Definition: k3procio.cpp:113
K3ProcIO::ackRead
void ackRead()
Call this after you have finished processing a readReady() signal.
Definition: k3procio.cpp:197
K3ProcIO::received
void received(K3Process *proc, char *buffer, int buflen)
Definition: k3procio.cpp:190
K3ProcIO::readln
int readln(QString &line, bool autoAck=true, bool *partial=0)
Reads a line of text (up to and including '\n').
Definition: k3procio.cpp:225
K3Process
Definition: k3process.h:128
K3Process::wroteStdin
void wroteStdin(K3Process *proc)
Emitted after all the data that has been specified by a prior call to writeStdin() has actually been ...
K3Process::receivedStdout
void receivedStdout(K3Process *proc, char *buffer, int buflen)
Emitted, when output from the child process has been received on stdout.
K3Process::RunMode
RunMode
Run-modes for a child process.
Definition: k3process.h:165
K3Process::start
virtual bool start(RunMode runmode=NotifyOnExit, Communication comm=NoCommunication)
Starts the process.
Definition: k3process.cpp:239
K3Process::closeStdin
bool closeStdin()
Shuts down the Stdin communication link.
Definition: k3process.cpp:592
K3Process::receivedStderr
void receivedStderr(K3Process *proc, char *buffer, int buflen)
Emitted, when output from the child process has been received on stderr.
K3Process::isRunning
bool isRunning() const
Checks whether the process is running.
Definition: k3process.cpp:438
K3Process::kill
virtual bool kill(int signo=SIGTERM)
Stop the process (by sending it a signal).
Definition: k3process.cpp:429
K3Process::writeStdin
bool writeStdin(const char *buffer, int buflen)
Definition: k3process.cpp:560
K3Process::clearArguments
void clearArguments()
Clear a command line argument list that has been set by using operator<<.
Definition: k3process.cpp:234
QList
k3procio.h
kdebug.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDE3Support

Skip menu "KDE3Support"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal