From af644c020106e7096d4fe9c648f25eede98c972f Mon Sep 17 00:00:00 2001 From: Michele Calgaro Date: Sun, 15 Dec 2024 22:06:44 +0900 Subject: Rename class KProcIO to TDEProcIO Signed-off-by: Michele Calgaro --- tdecore/CMakeLists.txt | 4 +- tdecore/MAINTAINERS | 2 +- tdecore/Makefile.am | 4 +- tdecore/README.exec | 4 +- tdecore/kdebug.areas | 2 +- tdecore/kprocio.cpp | 276 ---------------------------------------- tdecore/kprocio.h | 217 ------------------------------- tdecore/tdeprocess.h | 2 +- tdecore/tdeprocio.cpp | 276 ++++++++++++++++++++++++++++++++++++++++ tdecore/tdeprocio.h | 216 +++++++++++++++++++++++++++++++ tdecore/tests/CMakeLists.txt | 2 +- tdecore/tests/Makefile.am | 6 +- tdecore/tests/kprociotest.cpp | 65 ---------- tdecore/tests/kprociotest.h | 42 ------ tdecore/tests/tdeprociotest.cpp | 65 ++++++++++ tdecore/tests/tdeprociotest.h | 42 ++++++ 16 files changed, 612 insertions(+), 613 deletions(-) delete mode 100644 tdecore/kprocio.cpp delete mode 100644 tdecore/kprocio.h create mode 100644 tdecore/tdeprocio.cpp create mode 100644 tdecore/tdeprocio.h delete mode 100644 tdecore/tests/kprociotest.cpp delete mode 100644 tdecore/tests/kprociotest.h create mode 100644 tdecore/tests/tdeprociotest.cpp create mode 100644 tdecore/tests/tdeprociotest.h (limited to 'tdecore') diff --git a/tdecore/CMakeLists.txt b/tdecore/CMakeLists.txt index f56a92aca..021d650ae 100644 --- a/tdecore/CMakeLists.txt +++ b/tdecore/CMakeLists.txt @@ -64,7 +64,7 @@ install( FILES krandomsequence.h knotifyclient.h kiconeffect.h kaudioplayer.h kdcoppropertyproxy.h netwm.h tdeaccelmanager.h netwm_def.h kpixmapprovider.h kunload.h kstaticdeleter.h kextsock.h - kextendedsocket.h ksockaddr.h kprocio.h kasyncio.h kbufferedio.h + kextendedsocket.h ksockaddr.h tdeprocio.h kasyncio.h kbufferedio.h kurldrag.h kmimesourcefactory.h kmdcodec.h ksocks.h tdesycoca.h tdesycocaentry.h tdesycocatype.h kxmessages.h tdestartupinfo.h klargefile.h tdemultipledrag.h kgenericfactory.h kgenericfactory.tcc @@ -121,7 +121,7 @@ set( ${target}_SRCS krandomsequence.cpp kstringhandler.cpp kcompletion.cpp tdecmdlineargs.cpp tdeaboutdata.cpp kcompletionbase.cpp knotifyclient.cpp kaudioplayer.cpp kdcoppropertyproxy.cpp ksockaddr.cpp - kextsock.cpp netsupp.cpp kprocio.cpp kbufferedio.cpp + kextsock.cpp netsupp.cpp tdeprocio.cpp kbufferedio.cpp kpixmapprovider.cpp kurldrag.cpp kmdcodec.cpp ksocks.cpp fakes.c vsnprintf.c tdesycoca.cpp tdesycocadict.cpp tdesycoca.skel tdesycocafactory.cpp kxmessages.cpp tdestartupinfo.cpp diff --git a/tdecore/MAINTAINERS b/tdecore/MAINTAINERS index 1bc8f7a8c..3b4bae7cf 100644 --- a/tdecore/MAINTAINERS +++ b/tdecore/MAINTAINERS @@ -50,7 +50,7 @@ kpalette.cpp Waldo Bastian kpixmapprovider.cpp Carsten Pfeiffer tdeprocctrl.cpp Waldo Bastian tdeprocess.cpp Waldo Bastian -kprocio.cpp Waldo Bastian +tdeprocio.cpp Waldo Bastian krandomsequence.cpp Waldo Bastian kregexp.cpp krfcdate.cpp Waldo Bastian diff --git a/tdecore/Makefile.am b/tdecore/Makefile.am index d30506507..35aa6f6f3 100644 --- a/tdecore/Makefile.am +++ b/tdecore/Makefile.am @@ -51,7 +51,7 @@ include_HEADERS = tdeconfig.h tdeconfigskeleton.h \ krandomsequence.h knotifyclient.h kiconeffect.h \ kaudioplayer.h kdcoppropertyproxy.h netwm.h tdeaccelmanager.h \ netwm_def.h kpixmapprovider.h kunload.h kstaticdeleter.h \ - kextsock.h kextendedsocket.h ksockaddr.h kprocio.h kasyncio.h \ + kextsock.h kextendedsocket.h ksockaddr.h tdeprocio.h kasyncio.h \ kbufferedio.h kurldrag.h kmimesourcefactory.h kmdcodec.h ksocks.h \ tdesycoca.h tdesycocaentry.h tdesycocatype.h kxmessages.h tdestartupinfo.h \ klargefile.h tdemultipledrag.h kgenericfactory.h kgenericfactory.tcc \ @@ -105,7 +105,7 @@ libtdecore_la_SOURCES = libintl.cpp tdeapplication.cpp \ ksavefile.cpp krandomsequence.cpp kstringhandler.cpp kcompletion.cpp \ tdecmdlineargs.cpp tdeaboutdata.cpp kcompletionbase.cpp knotifyclient.cpp \ kaudioplayer.cpp kdcoppropertyproxy.cpp \ - ksockaddr.cpp kextsock.cpp netsupp.cpp kprocio.cpp kbufferedio.cpp \ + ksockaddr.cpp kextsock.cpp netsupp.cpp tdeprocio.cpp kbufferedio.cpp \ kpixmapprovider.cpp kurldrag.cpp \ kmdcodec.cpp ksocks.cpp fakes.c vsnprintf.c \ tdesycoca.cpp tdesycocadict.cpp tdesycocafactory.cpp tdesycoca.skel \ diff --git a/tdecore/README.exec b/tdecore/README.exec index d47c808e9..6ddf1aef4 100644 --- a/tdecore/README.exec +++ b/tdecore/README.exec @@ -16,9 +16,9 @@ child of your process, e.g. because you want to catch stdout/stderr or need to send it data via stdin. You should never use this to start other KDE applications unless your application is called kgdb :-) If you need to send/receive text like data to/from the process, you are probably better -off with KProcIO +off with TDEProcIO -3. KProcIO +3. TDEProcIO Like TDEProcess. Unlike TDEProcess, this class actually makes it easy to send data to and receive data from the process. diff --git a/tdecore/kdebug.areas b/tdecore/kdebug.areas index 24c87f524..e71455362 100644 --- a/tdecore/kdebug.areas +++ b/tdecore/kdebug.areas @@ -18,7 +18,7 @@ 171 tdecore (KSocks) 172 tdecore (TDEStartupInfo) 173 tdecore (TDELocale) -174 tdecore (KProcIO) +174 tdecore (TDEProcIO) 175 tdecore (TDEProcess) 176 tdecore (KWin) 177 tdecore (TDEConfigSkeleton) diff --git a/tdecore/kprocio.cpp b/tdecore/kprocio.cpp deleted file mode 100644 index aac14a09e..000000000 --- a/tdecore/kprocio.cpp +++ /dev/null @@ -1,276 +0,0 @@ -/* This file is part of the KDE libraries - Copyright (C) 1997 David Sweet - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License version 2 as published by the Free Software Foundation. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -// $Id$ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include - -#include "kprocio.h" - -#include -#include - -class KProcIOPrivate { -public: - KProcIOPrivate() : comm(TDEProcess::All) {} - TDEProcess::Communication comm; -}; - -KProcIO::KProcIO ( TQTextCodec *_codec) - : codec(_codec), d(new KProcIOPrivate) -{ - rbi=0; - readsignalon=writeready=true; - outbuffer.setAutoDelete(true); - - if (!codec) - { - codec = TQTextCodec::codecForName("ISO 8859-1"); - if (!codec) - { - kdError(174) << "Can't create ISO 8859-1 codec!" << endl; - } - } -} - -KProcIO::~KProcIO() -{ - delete d; -} - -void -KProcIO::resetAll () -{ - if (isRunning()) - kill(); - - clearArguments(); - rbi=0; - readsignalon=writeready=true; - - disconnect (this, TQ_SIGNAL (receivedStdout (TDEProcess *, char *, int)), - this, TQ_SLOT (received (TDEProcess *, char *, int))); - - disconnect (this, TQ_SIGNAL (receivedStderr (TDEProcess *, char *, int)), - this, TQ_SLOT (received (TDEProcess *, char *, int))); - - disconnect (this, TQ_SIGNAL (wroteStdin(TDEProcess *)), - this, TQ_SLOT (sent (TDEProcess *))); - - outbuffer.clear(); - -} - -void KProcIO::setComm (Communication comm) -{ - d->comm = comm; -} - -bool KProcIO::start (RunMode runmode, bool includeStderr) -{ - connect (this, TQ_SIGNAL (receivedStdout (TDEProcess *, char *, int)), - this, TQ_SLOT (received (TDEProcess *, char *, int))); - - if (includeStderr) - { - connect (this, TQ_SIGNAL (receivedStderr (TDEProcess *, char *, int)), - this, TQ_SLOT (received (TDEProcess *, char *, int))); - } - - connect (this, TQ_SIGNAL (wroteStdin(TDEProcess *)), - this, TQ_SLOT (sent (TDEProcess *))); - - return TDEProcess::start (runmode, d->comm); -} - -bool KProcIO::writeStdin (const TQString &line, bool appendnewline) -{ - return writeStdin(TQCString(codec->fromUnicode(line)), appendnewline); -} - -bool KProcIO::writeStdin (const TQCString &line, bool appendnewline) -{ - TQCString *qs = new TQCString(line); - - if (appendnewline) - { - *qs += '\n'; - } - - int l = qs->length(); - if (!l) - { - delete qs; - return true; - } - - TQByteArray *b = (TQByteArray *) qs; - b->truncate(l); // Strip trailing null - - outbuffer.append(b); - - if (writeready) - { - writeready=false; - return TDEProcess::writeStdin( b->data(), b->size() ); - } - return true; -} - -bool KProcIO::writeStdin(const TQByteArray &data) -{ - if (!data.size()) - return true; - TQByteArray *b = new TQByteArray(data); - outbuffer.append(b); - - if (writeready) - { - writeready=false; - return TDEProcess::writeStdin( b->data(), b->size() ); - } - return true; -} - -void KProcIO::closeWhenDone() -{ - if (writeready) - { - closeStdin(); - return; - } - outbuffer.append(0); - - return; -} - -void KProcIO::sent(TDEProcess *) -{ - outbuffer.removeFirst(); - - if (outbuffer.count()==0) - { - writeready=true; - } - else - { - TQByteArray *b = outbuffer.first(); - if (!b) - { - closeStdin(); - } - else - { - TDEProcess::writeStdin(b->data(), b->size()); - } - } - -} - -void KProcIO::received (TDEProcess *, char *buffer, int buflen) -{ - recvbuffer += TQCString(buffer, buflen+1); - - controlledEmission(); -} - -void KProcIO::ackRead () -{ - readsignalon=true; - if (needreadsignal || recvbuffer.length()!=0) - controlledEmission(); -} - -void KProcIO::controlledEmission () -{ - if (readsignalon) - { - needreadsignal=false; - readsignalon=false; //will stay off until read is acknowledged - emit readReady (this); - } - else - { - needreadsignal=true; - } -} - -void KProcIO::enableReadSignals (bool enable) -{ - readsignalon=enable; - - if (enable && needreadsignal) - emit readReady (this); -} - -int KProcIO::readln (TQString &line, bool autoAck, bool *partial) -{ - int len; - - if (autoAck) - readsignalon=true; - - //need to reduce the size of recvbuffer at some point... - - len=recvbuffer.find ('\n',rbi)-rbi; - - //kdDebug(174) << "KPIO::readln" << endl; - - //in case there's no '\n' at the end of the buffer - if ((len<0) && - ((unsigned int)rbi=0) - { - line = codec->toUnicode(recvbuffer.mid(rbi,len), len); - rbi += len+1; - if (partial) - *partial = false; - return len; - } - - recvbuffer=""; - rbi=0; - - //-1 on return signals "no more data" not error - return -1; - -} - -void KProcIO::virtual_hook( int id, void* data ) -{ TDEProcess::virtual_hook( id, data ); } - -#include "kprocio.moc" - diff --git a/tdecore/kprocio.h b/tdecore/kprocio.h deleted file mode 100644 index 02a3257d5..000000000 --- a/tdecore/kprocio.h +++ /dev/null @@ -1,217 +0,0 @@ -/* This file is part of the KDE libraries - Copyright (C) 1997 David Sweet - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License version 2 as published by the Free Software Foundation. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ -#ifndef KPROCIO_H_ -#define KPROCIO_H_ - -#include -#include -#include -#include "tdelibs_export.h" - -class KProcIOPrivate; -class TQTextCodec; - -/** - * KProcIO - * - * This class provides a slightly simpler interface to the communication - * functions provided by TDEProcess. The simplifications are: - * @li The buffer for a write is copied to an internal KProcIO - * buffer and maintained/freed appropriately. There is no need - * to be concerned with wroteStdin() signals _at_all_. - * @li readln() reads a line of data and buffers any leftovers. - * @li Conversion from/to unicode. - * - * Basically, KProcIO gives you buffered I/O similar to fgets()/fputs(). - * - * Aside from these, and the fact that start() takes different - * parameters, use this class just like TDEProcess. - * - * @author David Sweet - * @short A slightly simpler interface to TDEProcess - **/ - - -class TDECORE_EXPORT KProcIO : public TDEProcess -{ - TQ_OBJECT - -public: - /** - * Constructor - */ - KProcIO ( TQTextCodec *codec = 0 ); - - /** - * Destructor - */ - ~KProcIO(); - - /** - * Sets the communication mode to be passed to TDEProcess::start() - * by start(). The default communication mode is TDEProcess::All. - * You probably want to use this function in conjunction with - * TDEProcess::setUsePty(). - * @param comm the communication mode - */ - void setComm (Communication comm); - - /** - * Starts the process. It will fail in the following cases: - * @li The process is already running. - * @li The command line argument list is empty. - * @li The starting of the process failed (could not fork). - * @li The executable was not found. - * - * @param runmode For a detailed description of the - * various run modes, have a look at the - * general description of the TDEProcess class. - * @param includeStderr If true, data from both stdout and stderr is - * listened to. If false, only stdout is listened to. - * @return true on success, false on error. - **/ - bool start (RunMode runmode = NotifyOnExit, bool includeStderr = false); - - /** - * Writes text to stdin of the process. - * @param line Text to write. - * @param appendnewline if true, a newline '\\n' is appended. - * @return true if successful, false otherwise - **/ - bool writeStdin(const TQString &line, bool appendnewline=true); - - /** - * Writes text to stdin of the process. - * @param line Text to write. - * @param appendnewline if true, a newline '\\n' is appended. - * @return true if successful, false otherwise - **/ - bool writeStdin(const TQCString &line, bool appendnewline); - - /** - * Writes data to stdin of the process. - * @param data Data to write. - * @return true if successful, false otherwise - **/ - bool writeStdin(const TQByteArray &data); - - //I like fputs better -- it's the same as writeStdin - //inline - /** - * This function just calls writeStdin(). - * - * @param line Text to write. - * @param AppendNewLine if true, a newline '\\n' is appended. - * @return true if successful, false otherwise - * @deprecated - **/ - TDE_DEPRECATED bool fputs (const TQString &line, bool AppendNewLine=true) - { return writeStdin(line, AppendNewLine); } - - /** - * Closes stdin after all data has been send. - */ - void closeWhenDone(); - - /** - * Reads a line of text (up to and including '\\n'). - * - * Use readln() in response to a readReady() signal. - * You may use it multiple times if more than one line of data is - * available. - * Be sure to use ackRead() when you have finished processing the - * readReady() signal. This informs KProcIO that you are ready for - * another readReady() signal. - * - * readln() never blocks. - * - * autoAck==true makes these functions call ackRead() for you. - * - * @param line is used to store the line that was read. - * @param autoAck when true, ackRead() is called for you. - * @param partial when provided the line is returned - * even if it does not contain a '\\n'. *partial will be set to - * false if the line contains a '\\n' and false otherwise. - * @return the number of characters read, or -1 if no data is available. - **/ - int readln (TQString &line, bool autoAck=true, bool *partial=0); - - /** - * This function calls readln(). - * @param line is used to store the line that was read. - * @param autoAck when true, ackRead() is called for you. - * @return the number of characters read, or -1 if no data is available. - * @deprecated use readln. Note that it has an inverted autoAck default, - * though. - **/ - TDE_DEPRECATED int fgets (TQString &line, bool autoAck=false) - { return readln (line, autoAck); } - - /** - * Reset the class. Doesn't kill the process. - **/ - void resetAll (); - - /** - * Call this after you have finished processing a readReady() - * signal. This call need not be made in the slot that was signalled - * by readReady(). You won't receive any more readReady() signals - * until you acknowledge with ackRead(). This prevents your slot - * from being reentered while you are still processing the current - * data. If this doesn't matter, then call ackRead() right away in - * your readReady()-processing slot. - **/ - void ackRead (); - - /** - * Turns readReady() signals on and off. - * You can turn this off at will and not worry about losing any data. - * (as long as you turn it back on at some point...) - * @param enable true to turn the signals on, false to turn them off - */ - void enableReadSignals (bool enable); - -signals: - /** - * Emitted when the process is ready for reading. - * @param pio the process that emitted the signal - * @see enableReadSignals() - */ - void readReady(KProcIO *pio); - -protected: - TQPtrList outbuffer; - TQCString recvbuffer; - TQTextCodec *codec; - int rbi; - bool needreadsignal, readsignalon, writeready; - - void controlledEmission (); - -protected slots: - void received (TDEProcess *proc, char *buffer, int buflen); - void sent (TDEProcess *); - -protected: - virtual void virtual_hook( int id, void* data ); -private: - KProcIOPrivate *d; -}; - -#endif // KPROCIO_H_ - diff --git a/tdecore/tdeprocess.h b/tdecore/tdeprocess.h index 658571ce7..a6329f147 100644 --- a/tdecore/tdeprocess.h +++ b/tdecore/tdeprocess.h @@ -47,7 +47,7 @@ class KPty; * This class allows a KDE application to start child processes without having * to worry about UN*X signal handling issues and zombie process reaping. * - * @see KProcIO + * @see TDEProcIO * * Basically, this class distinguishes three different ways of running * child processes: diff --git a/tdecore/tdeprocio.cpp b/tdecore/tdeprocio.cpp new file mode 100644 index 000000000..e614148c5 --- /dev/null +++ b/tdecore/tdeprocio.cpp @@ -0,0 +1,276 @@ +/* This file is part of the KDE libraries + Copyright (C) 1997 David Sweet + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +// $Id$ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#include "tdeprocio.h" + +#include +#include + +class TDEProcIOPrivate { +public: + TDEProcIOPrivate() : comm(TDEProcess::All) {} + TDEProcess::Communication comm; +}; + +TDEProcIO::TDEProcIO ( TQTextCodec *_codec) + : codec(_codec), d(new TDEProcIOPrivate) +{ + rbi=0; + readsignalon=writeready=true; + outbuffer.setAutoDelete(true); + + if (!codec) + { + codec = TQTextCodec::codecForName("ISO 8859-1"); + if (!codec) + { + kdError(174) << "Can't create ISO 8859-1 codec!" << endl; + } + } +} + +TDEProcIO::~TDEProcIO() +{ + delete d; +} + +void +TDEProcIO::resetAll () +{ + if (isRunning()) + kill(); + + clearArguments(); + rbi=0; + readsignalon=writeready=true; + + disconnect (this, TQ_SIGNAL (receivedStdout (TDEProcess *, char *, int)), + this, TQ_SLOT (received (TDEProcess *, char *, int))); + + disconnect (this, TQ_SIGNAL (receivedStderr (TDEProcess *, char *, int)), + this, TQ_SLOT (received (TDEProcess *, char *, int))); + + disconnect (this, TQ_SIGNAL (wroteStdin(TDEProcess *)), + this, TQ_SLOT (sent (TDEProcess *))); + + outbuffer.clear(); + +} + +void TDEProcIO::setComm (Communication comm) +{ + d->comm = comm; +} + +bool TDEProcIO::start (RunMode runmode, bool includeStderr) +{ + connect (this, TQ_SIGNAL (receivedStdout (TDEProcess *, char *, int)), + this, TQ_SLOT (received (TDEProcess *, char *, int))); + + if (includeStderr) + { + connect (this, TQ_SIGNAL (receivedStderr (TDEProcess *, char *, int)), + this, TQ_SLOT (received (TDEProcess *, char *, int))); + } + + connect (this, TQ_SIGNAL (wroteStdin(TDEProcess *)), + this, TQ_SLOT (sent (TDEProcess *))); + + return TDEProcess::start (runmode, d->comm); +} + +bool TDEProcIO::writeStdin (const TQString &line, bool appendnewline) +{ + return writeStdin(TQCString(codec->fromUnicode(line)), appendnewline); +} + +bool TDEProcIO::writeStdin (const TQCString &line, bool appendnewline) +{ + TQCString *qs = new TQCString(line); + + if (appendnewline) + { + *qs += '\n'; + } + + int l = qs->length(); + if (!l) + { + delete qs; + return true; + } + + TQByteArray *b = (TQByteArray *) qs; + b->truncate(l); // Strip trailing null + + outbuffer.append(b); + + if (writeready) + { + writeready=false; + return TDEProcess::writeStdin( b->data(), b->size() ); + } + return true; +} + +bool TDEProcIO::writeStdin(const TQByteArray &data) +{ + if (!data.size()) + return true; + TQByteArray *b = new TQByteArray(data); + outbuffer.append(b); + + if (writeready) + { + writeready=false; + return TDEProcess::writeStdin( b->data(), b->size() ); + } + return true; +} + +void TDEProcIO::closeWhenDone() +{ + if (writeready) + { + closeStdin(); + return; + } + outbuffer.append(0); + + return; +} + +void TDEProcIO::sent(TDEProcess *) +{ + outbuffer.removeFirst(); + + if (outbuffer.count()==0) + { + writeready=true; + } + else + { + TQByteArray *b = outbuffer.first(); + if (!b) + { + closeStdin(); + } + else + { + TDEProcess::writeStdin(b->data(), b->size()); + } + } + +} + +void TDEProcIO::received (TDEProcess *, char *buffer, int buflen) +{ + recvbuffer += TQCString(buffer, buflen+1); + + controlledEmission(); +} + +void TDEProcIO::ackRead () +{ + readsignalon=true; + if (needreadsignal || recvbuffer.length()!=0) + controlledEmission(); +} + +void TDEProcIO::controlledEmission () +{ + if (readsignalon) + { + needreadsignal=false; + readsignalon=false; //will stay off until read is acknowledged + emit readReady (this); + } + else + { + needreadsignal=true; + } +} + +void TDEProcIO::enableReadSignals (bool enable) +{ + readsignalon=enable; + + if (enable && needreadsignal) + emit readReady (this); +} + +int TDEProcIO::readln (TQString &line, bool autoAck, bool *partial) +{ + int len; + + if (autoAck) + readsignalon=true; + + //need to reduce the size of recvbuffer at some point... + + len=recvbuffer.find ('\n',rbi)-rbi; + + //kdDebug(174) << "KPIO::readln" << endl; + + //in case there's no '\n' at the end of the buffer + if ((len<0) && + ((unsigned int)rbi=0) + { + line = codec->toUnicode(recvbuffer.mid(rbi,len), len); + rbi += len+1; + if (partial) + *partial = false; + return len; + } + + recvbuffer=""; + rbi=0; + + //-1 on return signals "no more data" not error + return -1; + +} + +void TDEProcIO::virtual_hook( int id, void* data ) +{ TDEProcess::virtual_hook( id, data ); } + +#include "tdeprocio.moc" + diff --git a/tdecore/tdeprocio.h b/tdecore/tdeprocio.h new file mode 100644 index 000000000..d7394b8b1 --- /dev/null +++ b/tdecore/tdeprocio.h @@ -0,0 +1,216 @@ +/* This file is part of the TDE libraries + Copyright (C) 1997 David Sweet + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ +#ifndef TDEPROCIO_H_ +#define TDEPROCIO_H_ + +#include +#include +#include +#include "tdelibs_export.h" + +class TDEProcIOPrivate; +class TQTextCodec; + +/** + * TDEProcIO + * + * This class provides a slightly simpler interface to the communication + * functions provided by TDEProcess. The simplifications are: + * @li The buffer for a write is copied to an internal TDEProcIO + * buffer and maintained/freed appropriately. There is no need + * to be concerned with wroteStdin() signals _at_all_. + * @li readln() reads a line of data and buffers any leftovers. + * @li Conversion from/to unicode. + * + * Basically, TDEProcIO gives you buffered I/O similar to fgets()/fputs(). + * + * Aside from these, and the fact that start() takes different + * parameters, use this class just like TDEProcess. + * + * @author David Sweet + * @short A slightly simpler interface to TDEProcess + **/ + + +class TDECORE_EXPORT TDEProcIO : public TDEProcess +{ + TQ_OBJECT + +public: + /** + * Constructor + */ + TDEProcIO ( TQTextCodec *codec = 0 ); + + /** + * Destructor + */ + ~TDEProcIO(); + + /** + * Sets the communication mode to be passed to TDEProcess::start() + * by start(). The default communication mode is TDEProcess::All. + * You probably want to use this function in conjunction with + * TDEProcess::setUsePty(). + * @param comm the communication mode + */ + void setComm (Communication comm); + + /** + * Starts the process. It will fail in the following cases: + * @li The process is already running. + * @li The command line argument list is empty. + * @li The starting of the process failed (could not fork). + * @li The executable was not found. + * + * @param runmode For a detailed description of the + * various run modes, have a look at the + * general description of the TDEProcess class. + * @param includeStderr If true, data from both stdout and stderr is + * listened to. If false, only stdout is listened to. + * @return true on success, false on error. + **/ + bool start (RunMode runmode = NotifyOnExit, bool includeStderr = false); + + /** + * Writes text to stdin of the process. + * @param line Text to write. + * @param appendnewline if true, a newline '\\n' is appended. + * @return true if successful, false otherwise + **/ + bool writeStdin(const TQString &line, bool appendnewline=true); + + /** + * Writes text to stdin of the process. + * @param line Text to write. + * @param appendnewline if true, a newline '\\n' is appended. + * @return true if successful, false otherwise + **/ + bool writeStdin(const TQCString &line, bool appendnewline); + + /** + * Writes data to stdin of the process. + * @param data Data to write. + * @return true if successful, false otherwise + **/ + bool writeStdin(const TQByteArray &data); + + //I like fputs better -- it's the same as writeStdin + //inline + /** + * This function just calls writeStdin(). + * + * @param line Text to write. + * @param AppendNewLine if true, a newline '\\n' is appended. + * @return true if successful, false otherwise + * @deprecated + **/ + TDE_DEPRECATED bool fputs (const TQString &line, bool AppendNewLine=true) + { return writeStdin(line, AppendNewLine); } + + /** + * Closes stdin after all data has been send. + */ + void closeWhenDone(); + + /** + * Reads a line of text (up to and including '\\n'). + * + * Use readln() in response to a readReady() signal. + * You may use it multiple times if more than one line of data is + * available. + * Be sure to use ackRead() when you have finished processing the + * readReady() signal. This informs TDEProcIO that you are ready for + * another readReady() signal. + * + * readln() never blocks. + * + * autoAck==true makes these functions call ackRead() for you. + * + * @param line is used to store the line that was read. + * @param autoAck when true, ackRead() is called for you. + * @param partial when provided the line is returned + * even if it does not contain a '\\n'. *partial will be set to + * false if the line contains a '\\n' and false otherwise. + * @return the number of characters read, or -1 if no data is available. + **/ + int readln (TQString &line, bool autoAck=true, bool *partial=0); + + /** + * This function calls readln(). + * @param line is used to store the line that was read. + * @param autoAck when true, ackRead() is called for you. + * @return the number of characters read, or -1 if no data is available. + * @deprecated use readln. Note that it has an inverted autoAck default, + * though. + **/ + TDE_DEPRECATED int fgets (TQString &line, bool autoAck=false) + { return readln (line, autoAck); } + + /** + * Reset the class. Doesn't kill the process. + **/ + void resetAll (); + + /** + * Call this after you have finished processing a readReady() + * signal. This call need not be made in the slot that was signalled + * by readReady(). You won't receive any more readReady() signals + * until you acknowledge with ackRead(). This prevents your slot + * from being reentered while you are still processing the current + * data. If this doesn't matter, then call ackRead() right away in + * your readReady()-processing slot. + **/ + void ackRead (); + + /** + * Turns readReady() signals on and off. + * You can turn this off at will and not worry about losing any data. + * (as long as you turn it back on at some point...) + * @param enable true to turn the signals on, false to turn them off + */ + void enableReadSignals (bool enable); + +signals: + /** + * Emitted when the process is ready for reading. + * @param pio the process that emitted the signal + * @see enableReadSignals() + */ + void readReady(TDEProcIO *pio); + +protected: + TQPtrList outbuffer; + TQCString recvbuffer; + TQTextCodec *codec; + int rbi; + bool needreadsignal, readsignalon, writeready; + + void controlledEmission (); + +protected slots: + void received (TDEProcess *proc, char *buffer, int buflen); + void sent (TDEProcess *); + +protected: + virtual void virtual_hook( int id, void* data ); +private: + TDEProcIOPrivate *d; +}; + +#endif diff --git a/tdecore/tests/CMakeLists.txt b/tdecore/tests/CMakeLists.txt index b0c1fb36b..e79dbfdeb 100644 --- a/tdecore/tests/CMakeLists.txt +++ b/tdecore/tests/CMakeLists.txt @@ -36,7 +36,7 @@ set( CHECKS ksocktest kstringhandlertest kcmdlineargstest tdeapptest kmemtest dcopkonqtest kipctest cplusplustest kiconloadertest kresolvertest kmdcodectest knotifytest ksortablevaluelisttest krfcdatetest testqtargs - kprociotest kcharsetstest kcalendartest kmacroexpandertest kshelltest + tdeprociotest kcharsetstest kcalendartest kmacroexpandertest kshelltest kxerrorhandlertest startserviceby kglobaltest ktimezonestest ) diff --git a/tdecore/tests/Makefile.am b/tdecore/tests/Makefile.am index c7b1f7d0e..cbf12cb2c 100644 --- a/tdecore/tests/Makefile.am +++ b/tdecore/tests/Makefile.am @@ -26,14 +26,14 @@ check_PROGRAMS = tdeconfigtestgui klocaletest tdeprocesstest ksimpleconfigtest \ kdebugtest ksocktest kstringhandlertest kcmdlineargstest tdeapptest \ kmemtest kidlservertest kidlclienttest dcopkonqtest kipctest \ cplusplustest kiconloadertest kresolvertest kmdcodectest knotifytest \ - ksortablevaluelisttest krfcdatetest testqtargs kprociotest \ + ksortablevaluelisttest krfcdatetest testqtargs tdeprociotest \ kcharsetstest kcalendartest kmacroexpandertest kshelltest \ kxerrorhandlertest startserviceby tdestdacceltest kglobaltest ktimezonestest TESTS = kurltest tdestdacceltest noinst_HEADERS = klocaletest.h tdeprocesstest.h KIDLTest.h \ - kipctest.h kprociotest.h + kipctest.h tdeprociotest.h METASOURCES = AUTO @@ -68,7 +68,7 @@ knotifytest_SOURCES = knotifytest.cpp ksortablevaluelisttest_SOURCES = ksortablevaluelisttest.cpp krfcdatetest_SOURCES = krfcdatetest.cpp testqtargs_SOURCES = testqtargs.cpp -kprociotest_SOURCES = kprociotest.cpp +tdeprociotest_SOURCES = tdeprociotest.cpp kcharsetstest_SOURCES = kcharsetstest.cpp kcalendartest_SOURCES = kcalendartest.cpp kmacroexpandertest_SOURCES = kmacroexpandertest.cpp diff --git a/tdecore/tests/kprociotest.cpp b/tdecore/tests/kprociotest.cpp deleted file mode 100644 index 81522f394..000000000 --- a/tdecore/tests/kprociotest.cpp +++ /dev/null @@ -1,65 +0,0 @@ -// -// MAIN -- a little demo of the capabilities of the "TDEProcess" class -// -// version 0.2, Aug 2nd 1997 -// $Id$ -// -// (C) Christian Czezatke -// e9025461@student.tuwien.ac.at -// - - -#include "tdeprocess.h" - -#include -#include -#include - -#include - -#include "kprociotest.h" - - - -// -// A nice input for "sort"... ;- ) -// -static const char txt[] = "hat\nder\nalte\nhexenmeister\nsich\ndoch\neinmal\nwegbegeben\n\ -und\nnun\nsollen\nseine\ngeister\nsich\nnach\nmeinem\nwillen\nregen\nseine\nwort\nund\n\ -werke\nmerkt\nich\nund\nden\nbrauch\nund\nmit\ngeistesstaerke\ntu\nich\nwunder\nauch\n"; - - -int main(int argc, char *argv[]) -{ - Dummy dummy; - TDEApplication app(argc, argv, TQCString("kprociotest")); - - printf("Welcome to the KProcIO Demo Application!\n"); - - - KProcIO p; - - p << "rev"; - - p.connect(&p, TQ_SIGNAL(processExited(TDEProcess*)), &dummy, TQ_SLOT(printMessage(TDEProcess*))); - p.connect(&p, TQ_SIGNAL(readReady(KProcIO*)), &dummy, TQ_SLOT(gotOutput(KProcIO*))); - - bool b; - - b = p.start(); - printf("Start returns %s\n", b ? "true" : "false"); - - b = p.fputs("Hello World!"); - printf("fputs returns %s\n", b ? "true" : "false"); - - b = p.fputs("This is a test. It should come out in reverse (esrever)"); - printf("fputs returns %s\n", b ? "true" : "false"); - - p.closeWhenDone(); - - printf("Entering man Qt event loop -- press to abort\n"); - app.exec(); - - return 0; -} -#include "kprociotest.moc" diff --git a/tdecore/tests/kprociotest.h b/tdecore/tests/kprociotest.h deleted file mode 100644 index 78b5ad926..000000000 --- a/tdecore/tests/kprociotest.h +++ /dev/null @@ -1,42 +0,0 @@ -// -// DUMMY -- A dummy class with a slot to demonstrate TDEProcess signals -// -// version 0.2, Aug 2nd 1997 -// -// (C) Christian Czezatke -// e9025461@student.tuwien.ac.at -// - - -#ifndef __DUMMY_H__ -#define __DUMMY_H__ - -#include -#include -#include "kprocio.h" - -class Dummy : public TQObject -{ - TQ_OBJECT - - public slots: - void printMessage(TDEProcess *proc) - { - printf("Process %d exited!\n", (int)proc->getPid()); - } - - void gotOutput(KProcIO*proc) - { - TQString line; - while(true) { - int result = proc->readln(line); - if (result == -1) return; - printf("OUTPUT>> [%d] '%s'\n", result, line.latin1()); - } - } - -}; - -#endif - - diff --git a/tdecore/tests/tdeprociotest.cpp b/tdecore/tests/tdeprociotest.cpp new file mode 100644 index 000000000..16c9b14b6 --- /dev/null +++ b/tdecore/tests/tdeprociotest.cpp @@ -0,0 +1,65 @@ +// +// MAIN -- a little demo of the capabilities of the "TDEProcess" class +// +// version 0.2, Aug 2nd 1997 +// $Id$ +// +// (C) Christian Czezatke +// e9025461@student.tuwien.ac.at +// + + +#include "tdeprocess.h" + +#include +#include +#include + +#include + +#include "tdeprociotest.h" + + + +// +// A nice input for "sort"... ;- ) +// +static const char txt[] = "hat\nder\nalte\nhexenmeister\nsich\ndoch\neinmal\nwegbegeben\n\ +und\nnun\nsollen\nseine\ngeister\nsich\nnach\nmeinem\nwillen\nregen\nseine\nwort\nund\n\ +werke\nmerkt\nich\nund\nden\nbrauch\nund\nmit\ngeistesstaerke\ntu\nich\nwunder\nauch\n"; + + +int main(int argc, char *argv[]) +{ + Dummy dummy; + TDEApplication app(argc, argv, TQCString("tdeprociotest")); + + printf("Welcome to the TDEProcIO Demo Application!\n"); + + + TDEProcIO p; + + p << "rev"; + + p.connect(&p, TQ_SIGNAL(processExited(TDEProcess*)), &dummy, TQ_SLOT(printMessage(TDEProcess*))); + p.connect(&p, TQ_SIGNAL(readReady(TDEProcIO*)), &dummy, TQ_SLOT(gotOutput(TDEProcIO*))); + + bool b; + + b = p.start(); + printf("Start returns %s\n", b ? "true" : "false"); + + b = p.fputs("Hello World!"); + printf("fputs returns %s\n", b ? "true" : "false"); + + b = p.fputs("This is a test. It should come out in reverse (esrever)"); + printf("fputs returns %s\n", b ? "true" : "false"); + + p.closeWhenDone(); + + printf("Entering man Qt event loop -- press to abort\n"); + app.exec(); + + return 0; +} +#include "tdeprociotest.moc" diff --git a/tdecore/tests/tdeprociotest.h b/tdecore/tests/tdeprociotest.h new file mode 100644 index 000000000..194a26504 --- /dev/null +++ b/tdecore/tests/tdeprociotest.h @@ -0,0 +1,42 @@ +// +// DUMMY -- A dummy class with a slot to demonstrate TDEProcess signals +// +// version 0.2, Aug 2nd 1997 +// +// (C) Christian Czezatke +// e9025461@student.tuwien.ac.at +// + + +#ifndef __DUMMY_H__ +#define __DUMMY_H__ + +#include +#include +#include "tdeprocio.h" + +class Dummy : public TQObject +{ + TQ_OBJECT + + public slots: + void printMessage(TDEProcess *proc) + { + printf("Process %d exited!\n", (int)proc->getPid()); + } + + void gotOutput(TDEProcIO*proc) + { + TQString line; + while(true) { + int result = proc->readln(line); + if (result == -1) return; + printf("OUTPUT>> [%d] '%s'\n", result, line.latin1()); + } + } + +}; + +#endif + + -- cgit v1.2.1