⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vk_process.cpp

📁 Linux平台下的内核及程序调试器
💻 CPP
📖 第 1 页 / 共 5 页
字号:
   void VKProcess::init()      {         d = new VKProcessPrivate();         exitStat = 0;         exitNormal = false;      }   /* This private class resets the process variables, etc. so that it      can be used for another process to start. */   void VKProcess::reset()      {         delete d;         d = new VKProcessPrivate();         exitStat = 0;         exitNormal = false;         d->bufFDout.clear();         d->bufStdout.clear();         d->bufStderr.clear();      }   /* Returns the list of arguments that are set for the process.      Arguments can be specified with the constructor or with the      functions setArguments() and addArgument(). */   QStringList VKProcess::arguments() const      {         return _arguments;      }   /* Clears the list of arguments that are set for the process. */   void VKProcess::clearArguments()      {         _arguments.clear();      }   /* Sets args as the arguments for the process. The first element in      the list is the command to be executed. The other elements in the      list are the arguments to the command. Any previous arguments are      deleted.      VKProcess does not perform argument substitutions; for example, if      you specify "*" or "$DISPLAY", these values are passed to the      process literally. If you want to have the same behavior as the      shell provides, you must do the substitutions yourself;      i.e. instead of specifying a "*" you must specify the list of all      the filenames in the current directory, and instead of "$DISPLAY"      you must specify the value of the environment variable DISPLAY. */   void VKProcess::setArguments( const QStringList& args )      {         _arguments = args;      }   /* Adds arg to the end of the list of arguments.      The first element in the list of arguments is the command to be      executed; the following elements are the command's arguments. */   void VKProcess::addArgument( const QString& arg )      {         _arguments.append( arg );      }   /* Returns the working directory that was set with      setWorkingDirectory(), or the current directory if none has been      explicitly set. */   QDir VKProcess::workingDirectory() const      {         return workingDir;      }   /* Sets dir as the working directory for processes. This does not      affect running processes; only processes that are started      afterwards are affected.      Setting the working directory is especially useful for processes      that try to access files with relative paths. */   void VKProcess::setWorkingDirectory( const QDir& dir )      {         workingDir = dir;      }   /* Returns the communication required with the process, i.e. some      combination of the Communication flags. */   int VKProcess::communication() const      {         return comms;      }   /* If FDout/in overlaps with Stdout/err/in, FDout/in take precedence */   void VKProcess::reprioritiseComms()      {         /* If FDin enabled, take care of (dis/re)enabling stdin */         if (comms & FDin) {            if (comms & Stdin) {   /* Stdin enabled: disable it? */               if (filedesc_in == STDIN_FILENO) {                  comms &= ~Stdin;                  disabledStdin = true;               }            } else {               /* Stdin not enabled: re-enable it? */               if (disabledStdin && filedesc_in != STDIN_FILENO) {                  comms |= Stdin;                  disabledStdin = false;               }            }         }         /* If FDout enabled, take care of (dis/re)enabling stdout/stderr */         if (comms & FDout) {            if (comms & Stdout) {  /* Stdout enabled: disable it? */               if (filedesc_out == STDOUT_FILENO) {                  comms &= ~Stdout;                  disabledStdout = true;               }            } else {               /* Stdout not enabled: re-enable it? */               if (disabledStdout && filedesc_out != STDOUT_FILENO) {                  comms |= Stdout;                  disabledStdout = false;               }            }            if (comms & Stderr) {  /* Stderr enabled: disable it? */               if (filedesc_out == STDERR_FILENO) {                  comms &= ~Stderr;                  disabledStderr = true;               }            } else {               /* Stderr not enabled: re-enable it? */               if (disabledStderr && filedesc_out != STDERR_FILENO) {                  comms |= Stderr;                  disabledStderr = false;               }            }         }      }   /* Sets commFlags as the communication required with the process.      commFlags is a bitwise OR of the flags defined by the Communication      enum.  The default is { Stdin | Stdout | Stderr }.      NOTE: FDout|in take priority over Stdout/err/in, so the latter are      NOT enabled if there's an overlap of fd's.  However, their      disabling is recorded, and subsequent calls to setCommunication()      and setFDin/out() will reflect this.  See reprioritiseComms().      Returns actual communications set. */   int VKProcess::setCommunication( int commFlags )      {         comms = commFlags;         /* Reset disabling of std fd's */         disabledStdin  = false;         disabledStdout = false;         disabledStderr = false;         /* disable std fd's if overlap FDout/in */         reprioritiseComms();         return comms;      }   /* Sets input file descriptor      The default is 0      If fd == stdin, disable Stdin: FDin takes precedence         NOTE: FDin takes priority over Stdin, so the latter may be DISABLED      if there's an overlap of fd's.  See reprioritiseComms(). */   void VKProcess::setFDin( int fd )      {         vk_assert(fd >= 0 && fd < 1024);  // <0..1023>         filedesc_in = fd;         /* disable/enable std fd's wrt overlapping FDout/in */         reprioritiseComms();      }   /* Sets output file descriptor      The default is 1      If fd == stdout|stderr, disable Stdout|Stdin: FDout takes precedence      NOTE: FDout takes priority over Stdout/err, so one of the latter      may be DISABLED if there's an overlap of fd's.  See      reprioritiseComms(). */   void VKProcess::setFDout( int fd )      {         vk_assert(fd > 0 && fd < 1024);  // <1..1023>         filedesc_out = fd;         /* disable/enable std fd's wrt overlapping FDout/in */         reprioritiseComms();      }   int VKProcess::getFDin()      { return filedesc_in; }   int VKProcess::getFDout()      { return filedesc_out; }   /* Returns true if the process has exited normally; otherwise returns      false. This implies that this function returns false if the process      is still running. */   bool VKProcess::normalExit() const      {         // isRunning() has the side effect that it determines the exit status!         if ( isRunning() )            return false;         else            return exitNormal;      }   /* Returns the exit status of the process or 0 if the process is still      running. This function returns immediately and does not wait until      the process is finished.      If normalExit() is false (e.g. if the program was killed or      crashed), this function returns 0, so you should check the return      value of normalExit() before relying on this value. */   int VKProcess::exitStatus() const      {         // isRunning() has the side effect that it determines the exit status!         if ( isRunning() )            return 0;         else            return exitStat;      }   /* Reads the data that the process has written to FDout.  When new      data is written to standard output, the class emits the signal      readyReadFDout().      If there is no data to read, this function returns a QByteArray of      size 0: it does not wait until there is something to read. */   QByteArray VKProcess::readFDout()      {         if ( readFDoutCalled ) {            return QByteArray();         }         readFDoutCalled = true;         VKMembuf *buf = membufFDout();         readFDoutCalled = false;         return buf->readAll();      }   /* Reads the data that the process has written to standard output.      When new data is written to standard output, the class emits the      signal readyReadStdout().      If there is no data to read, this function returns a QByteArray of      size 0: it does not wait until there is something to read. */   QByteArray VKProcess::readStdout()      {         if ( readStdoutCalled ) {            return QByteArray();         }         readStdoutCalled = true;         VKMembuf *buf = membufStdout();         readStdoutCalled = false;         return buf->readAll();      }   /* Reads the data that the process has written to standard error.      When new data is written to standard error, the class emits the      signal readyReadStderr().      If there is no data to read, this function returns a QByteArray of      size 0: it does not wait until there is something to read. */   QByteArray VKProcess::readStderr()      {         if ( readStderrCalled ) {            return QByteArray();         }         readStderrCalled = true;         VKMembuf *buf = membufStderr();         readStderrCalled = false;         return buf->readAll();      }   /* Reads a line of text from FDout, excluding any trailing newline or      carriage return characters, and returns it. Returns QString::null      if canReadLineFDout() returns false.      By default, the text is interpreted to be in Latin-1 encoding. If      you need other codecs, you can set a different codec with      QTextCodec::setCodecForCStrings(). */   QString VKProcess::readLineFDout()      {         QByteArray a( 256 );         VKMembuf *buf = membufFDout();         if ( !buf->scanNewline( &a ) ) {            if ( !canReadLineFDout() )               return QString::null;            if ( !buf->scanNewline( &a ) )               return QString( buf->readAll() );         }         uint size = a.size();         buf->consumeBytes( size, 0 );         // get rid of terminating \n or \r\n         if ( size>0 && a.at( size - 1 ) == '\n' ) {            if ( size>1 && a.at( size - 2 ) == '\r' )               a.at( size - 2 ) = '\0';            else               a.at( size - 1 ) = '\0';         }         return QString( a );      }   /* Reads a line of text from standard output, excluding any trailing      newline or carriage return characters, and returns it. Returns      QString::null if canReadLineStdout() returns false.      By default, the text is interpreted to be in Latin-1 encoding. If      you need other codecs, you can set a different codec with      QTextCodec::setCodecForCStrings(). */   QString VKProcess::readLineStdout()      {         QByteArray a( 256 );         VKMembuf *buf = membufStdout();         if ( !buf->scanNewline( &a ) ) {            if ( !canReadLineStdout() )               return QString::null;                if ( !buf->scanNewline( &a ) )               return QString( buf->readAll() );         }         uint size = a.size();         buf->consumeBytes( size, 0 );         // get rid of terminating \n or \r\n         if ( size>0 && a.at( size - 1 ) == '\n' ) {            if ( size>1 && a.at( size - 2 ) == '\r' )               a.at( size - 2 ) = '\0';            else               a.at( size - 1 ) = '\0';         }         return QString( a );      }   /* Reads a line of text from standard error, excluding any trailing      newline or carriage return characters and returns it. Returns

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -