• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
tdeprocio.cpp
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 // $Id$
20 
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 
25 #include <stdio.h>
26 
27 #include "tdeprocio.h"
28 
29 #include <kdebug.h>
30 #include <tqtextcodec.h>
31 
32 class TDEProcIOPrivate {
33 public:
34  TDEProcIOPrivate() : comm(TDEProcess::All) {}
35  TDEProcess::Communication comm;
36 };
37 
38 TDEProcIO::TDEProcIO ( TQTextCodec *_codec)
39  : codec(_codec), d(new TDEProcIOPrivate)
40 {
41  rbi=0;
42  readsignalon=writeready=true;
43  outbuffer.setAutoDelete(true);
44 
45  if (!codec)
46  {
47  codec = TQTextCodec::codecForName("ISO 8859-1");
48  if (!codec)
49  {
50  kdError(174) << "Can't create ISO 8859-1 codec!" << endl;
51  }
52  }
53 }
54 
55 TDEProcIO::~TDEProcIO()
56 {
57  delete d;
58 }
59 
60 void
61 TDEProcIO::resetAll ()
62 {
63  if (isRunning())
64  kill();
65 
66  clearArguments();
67  rbi=0;
68  readsignalon=writeready=true;
69 
70  disconnect (this, TQ_SIGNAL (receivedStdout (TDEProcess *, char *, int)),
71  this, TQ_SLOT (received (TDEProcess *, char *, int)));
72 
73  disconnect (this, TQ_SIGNAL (receivedStderr (TDEProcess *, char *, int)),
74  this, TQ_SLOT (received (TDEProcess *, char *, int)));
75 
76  disconnect (this, TQ_SIGNAL (wroteStdin(TDEProcess *)),
77  this, TQ_SLOT (sent (TDEProcess *)));
78 
79  outbuffer.clear();
80 
81 }
82 
83 void TDEProcIO::setComm (Communication comm)
84 {
85  d->comm = comm;
86 }
87 
88 bool TDEProcIO::start (RunMode runmode, bool includeStderr)
89 {
90  connect (this, TQ_SIGNAL (receivedStdout (TDEProcess *, char *, int)),
91  this, TQ_SLOT (received (TDEProcess *, char *, int)));
92 
93  if (includeStderr)
94  {
95  connect (this, TQ_SIGNAL (receivedStderr (TDEProcess *, char *, int)),
96  this, TQ_SLOT (received (TDEProcess *, char *, int)));
97  }
98 
99  connect (this, TQ_SIGNAL (wroteStdin(TDEProcess *)),
100  this, TQ_SLOT (sent (TDEProcess *)));
101 
102  return TDEProcess::start (runmode, d->comm);
103 }
104 
105 bool TDEProcIO::writeStdin (const TQString &line, bool appendnewline)
106 {
107  return writeStdin(TQCString(codec->fromUnicode(line)), appendnewline);
108 }
109 
110 bool TDEProcIO::writeStdin (const TQCString &line, bool appendnewline)
111 {
112  TQCString *qs = new TQCString(line);
113 
114  if (appendnewline)
115  {
116  *qs += '\n';
117  }
118 
119  int l = qs->length();
120  if (!l)
121  {
122  delete qs;
123  return true;
124  }
125 
126  TQByteArray *b = (TQByteArray *) qs;
127  b->truncate(l); // Strip trailing null
128 
129  outbuffer.append(b);
130 
131  if (writeready)
132  {
133  writeready=false;
134  return TDEProcess::writeStdin( b->data(), b->size() );
135  }
136  return true;
137 }
138 
139 bool TDEProcIO::writeStdin(const TQByteArray &data)
140 {
141  if (!data.size())
142  return true;
143  TQByteArray *b = new TQByteArray(data);
144  outbuffer.append(b);
145 
146  if (writeready)
147  {
148  writeready=false;
149  return TDEProcess::writeStdin( b->data(), b->size() );
150  }
151  return true;
152 }
153 
154 void TDEProcIO::closeWhenDone()
155 {
156  if (writeready)
157  {
158  closeStdin();
159  return;
160  }
161  outbuffer.append(0);
162 
163  return;
164 }
165 
166 void TDEProcIO::sent(TDEProcess *)
167 {
168  outbuffer.removeFirst();
169 
170  if (outbuffer.count()==0)
171  {
172  writeready=true;
173  }
174  else
175  {
176  TQByteArray *b = outbuffer.first();
177  if (!b)
178  {
179  closeStdin();
180  }
181  else
182  {
183  TDEProcess::writeStdin(b->data(), b->size());
184  }
185  }
186 
187 }
188 
189 void TDEProcIO::received (TDEProcess *, char *buffer, int buflen)
190 {
191  recvbuffer += TQCString(buffer, buflen+1);
192 
193  controlledEmission();
194 }
195 
196 void TDEProcIO::ackRead ()
197 {
198  readsignalon=true;
199  if (needreadsignal || recvbuffer.length()!=0)
200  controlledEmission();
201 }
202 
203 void TDEProcIO::controlledEmission ()
204 {
205  if (readsignalon)
206  {
207  needreadsignal=false;
208  readsignalon=false; //will stay off until read is acknowledged
209  emit readReady (this);
210  }
211  else
212  {
213  needreadsignal=true;
214  }
215 }
216 
217 void TDEProcIO::enableReadSignals (bool enable)
218 {
219  readsignalon=enable;
220 
221  if (enable && needreadsignal)
222  emit readReady (this);
223 }
224 
225 int TDEProcIO::readln (TQString &line, bool autoAck, bool *partial)
226 {
227  int len;
228 
229  if (autoAck)
230  readsignalon=true;
231 
232  //need to reduce the size of recvbuffer at some point...
233 
234  len=recvbuffer.find ('\n',rbi)-rbi;
235 
236  //kdDebug(174) << "KPIO::readln" << endl;
237 
238  //in case there's no '\n' at the end of the buffer
239  if ((len<0) &&
240  ((unsigned int)rbi<recvbuffer.length()))
241  {
242  recvbuffer=recvbuffer.mid (rbi);
243  rbi=0;
244  if (partial)
245  {
246  len = recvbuffer.length();
247  line = recvbuffer;
248  recvbuffer = "";
249  *partial = true;
250  return len;
251  }
252  return -1;
253  }
254 
255  if (len>=0)
256  {
257  line = codec->toUnicode(recvbuffer.mid(rbi,len), len);
258  rbi += len+1;
259  if (partial)
260  *partial = false;
261  return len;
262  }
263 
264  recvbuffer="";
265  rbi=0;
266 
267  //-1 on return signals "no more data" not error
268  return -1;
269 
270 }
271 
272 void TDEProcIO::virtual_hook( int id, void* data )
273 { TDEProcess::virtual_hook( id, data ); }
274 
275 #include "tdeprocio.moc"
276 
TDEProcIO::closeWhenDone
void closeWhenDone()
Closes stdin after all data has been send.
Definition: tdeprocio.cpp:154
TDEProcIO::resetAll
void resetAll()
Reset the class.
Definition: tdeprocio.cpp:61
TDEProcIO::readln
int readln(TQString &line, bool autoAck=true, bool *partial=0)
Reads a line of text (up to and including '\n').
Definition: tdeprocio.cpp:225
TDEProcIO::enableReadSignals
void enableReadSignals(bool enable)
Turns readReady() signals on and off.
Definition: tdeprocio.cpp:217
TDEProcIO::setComm
void setComm(Communication comm)
Sets the communication mode to be passed to TDEProcess::start() by start().
Definition: tdeprocio.cpp:83
TDEProcIO::readReady
void readReady(TDEProcIO *pio)
Emitted when the process is ready for reading.
TDEProcIO::writeStdin
bool writeStdin(const TQString &line, bool appendnewline=true)
Writes text to stdin of the process.
Definition: tdeprocio.cpp:105
TDEProcIO::TDEProcIO
TDEProcIO(TQTextCodec *codec=0)
Constructor.
Definition: tdeprocio.cpp:38
TDEProcIO::start
bool start(RunMode runmode=NotifyOnExit, bool includeStderr=false)
Starts the process.
Definition: tdeprocio.cpp:88
TDEProcIO::~TDEProcIO
~TDEProcIO()
Destructor.
Definition: tdeprocio.cpp:55
TDEProcIO::ackRead
void ackRead()
Call this after you have finished processing a readReady() signal.
Definition: tdeprocio.cpp:196
TDEProcess
Child process invocation, monitoring and control.
Definition: tdeprocess.h:131
TDEProcess::isRunning
bool isRunning() const
Checks whether the process is running.
Definition: tdeprocess.cpp:499
TDEProcess::start
virtual bool start(RunMode runmode=NotifyOnExit, Communication comm=NoCommunication)
Starts the process.
Definition: tdeprocess.cpp:293
TDEProcess::clearArguments
void clearArguments()
Clear a command line argument list that has been set by using operator<<.
Definition: tdeprocess.cpp:288
TDEProcess::kill
virtual bool kill(int signo=SIGTERM)
Stop the process (by sending it a signal).
Definition: tdeprocess.cpp:488
TDEProcess::RunMode
RunMode
Run-modes for a child process.
Definition: tdeprocess.h:169
TDEProcess::Communication
Communication
Modes in which the communication channel can be opened.
Definition: tdeprocess.h:157
TDEProcess::writeStdin
bool writeStdin(const char *buffer, int buflen)
Definition: tdeprocess.cpp:623
TDEProcess::receivedStderr
void receivedStderr(TDEProcess *proc, char *buffer, int buflen)
Emitted, when output from the child process has been received on stderr.
TDEProcess::wroteStdin
void wroteStdin(TDEProcess *proc)
Emitted after all the data that has been specified by a prior call to writeStdin() has actually been ...
TDEProcess::closeStdin
bool closeStdin()
Shuts down the Stdin communication link.
Definition: tdeprocess.cpp:655
TDEProcess::receivedStdout
void receivedStdout(TDEProcess *proc, char *buffer, int buflen)
Emitted, when output from the child process has been received on stdout.
endl
kndbgstream & endl(kndbgstream &s)
Does nothing.
Definition: kdebug.h:583

tdecore

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

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.1
This website is maintained by Timothy Pearson.