Back to index

texmacs  1.0.7.15
QTMPipeLink.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : qt_pipe_link.cpp
00004 * DESCRIPTION: QT TeXmacs links
00005 * COPYRIGHT  : (C) 2009 David MICHEL
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "tm_link.hpp"
00013 #include "QTMPipeLink.hpp"
00014 #include <QByteArray>
00015 
00016 static string
00017 debug_io_string (QByteArray s) {
00018   int i, n= s.size ();
00019   string r;
00020   for (i=0; i<n; i++) {
00021     unsigned char c= (unsigned char) s[i];
00022     if (c == DATA_BEGIN) r << "[BEGIN]";
00023     else if (c == DATA_END) r << "[END]";
00024     else if (c == DATA_COMMAND) r << "[COMMAND]";
00025     else if (c == DATA_ESCAPE) r << "[ESCAPE]";
00026     else r << s[i];
00027   }
00028   return r;
00029 }
00030 
00031 void
00032 QTMPipeLink::readErrOut () {
00033   feedBuf (QProcess::StandardError);
00034   feedBuf (QProcess::StandardOutput);
00035 }
00036 
00037 QTMPipeLink::QTMPipeLink (string cmd2) : cmd (cmd2), outbuf (""), errbuf ("") {}
00038 
00039 QTMPipeLink::~QTMPipeLink () {
00040   killProcess ();
00041 }
00042 
00043 bool
00044 QTMPipeLink::launchCmd () {
00045   if (state () != QProcess::NotRunning) killProcess ();
00046   char* _cmd = as_charp (cmd);
00047   QProcess::start (_cmd);
00048   tm_delete_array (_cmd);
00049   bool r= waitForStarted ();
00050   if (r) {
00051     connect (this, SIGNAL(readyReadStandardOutput ()), SLOT(readErrOut ()));
00052     connect (this, SIGNAL(readyReadStandardError ()), SLOT(readErrOut ()));
00053   }
00054   return r;
00055 }
00056 
00057 int
00058 QTMPipeLink::writeStdin (string s) {
00059   char* _s= as_charp (s);
00060   if (DEBUG_IO) cout << "[INPUT]" << debug_io_string (_s);
00061   int err= QIODevice::write (_s, N(s));
00062   tm_delete_array (_s);
00063   return err;
00064 }
00065 
00066 void
00067 QTMPipeLink::feedBuf (ProcessChannel channel) {
00068   setReadChannel (channel);
00069   QByteArray tempout = QIODevice::readAll ();
00070   if (channel == QProcess::StandardOutput) outbuf << tempout.constData ();
00071   else errbuf << tempout.constData ();
00072   if (DEBUG_IO)
00073     cout << "[OUTPUT " << channel << "]" << debug_io_string (tempout.constData ()) << "\n";
00074 }
00075 
00076 bool
00077 QTMPipeLink::listenChannel (ProcessChannel channel, int msecs) {
00078   setReadChannel (channel);
00079   return waitForReadyRead (msecs);
00080 }
00081 
00082 void
00083 QTMPipeLink::killProcess () {
00084   disconnect (SIGNAL(readyReadStandardOutput ()), this, SLOT(readErrOut ()));
00085   disconnect (SIGNAL(readyReadStandardError ()), this, SLOT(readErrOut ()));
00086 #if defined(__MINGW__) || defined(__MINGW32__)
00087   close ();
00088 #else
00089   terminate ();
00090   if (! waitForFinished ()) kill ();
00091 #endif
00092 }
00093