#include <console_reader.h>
Public Member Functions | |
console_reader (QObject *parent=0) | |
virtual | ~console_reader () |
Protected Member Functions | |
virtual void | interpretate_console_output (QStringList &stringList)=0 |
virtual QString | QByteArray_to_QString (QByteArray &m_byteArray) |
Protected Attributes | |
QString::SplitBehavior | internal_splitBehavior |
KProcess | m_process |
Private Slots | |
void | read_console_output () |
Private Attributes | |
QString | m_uncomplete_line |
This class is useful when you want to read and interpretate the output of a command line program, and espacialy when you want to write a graphical frontend to a command line program.
This class has a protected member m_process of type KProcess which is used to start the external program. When you inherit from this class, you have to add some methods to set up and start this process. In m_process, stderr and stdout are merged. Don't read the output from m_process yourself. Instead, implement interpretate_console_output(QStringList& stringList) to process the output of m_process line-by-line.
Attention: It is expected that no \0x00
is contained in the output of m_process.
Definition at line 46 of file console_reader.h.
console_reader::console_reader | ( | QObject * | parent = 0 |
) |
Constructor of the class.
parent | Sets the parent of this object. |
Definition at line 23 of file console_reader.cpp.
References internal_splitBehavior, m_process, and read_console_output().
console_reader::~console_reader | ( | ) | [virtual] |
Destructor of the class.
Calling the destructor when the process of m_process is still running kills of course this process. (It could maybe take very much time to just send a terminate signal to the process and than wait until the process terminates voluntarily, so this desctructor sends directly a kill signal.)
Of course, it's nicer when you make sure that the process is terminated before you delete an instance of this class.
Definition at line 34 of file console_reader.cpp.
References m_process.
virtual void console_reader::interpretate_console_output | ( | QStringList & | stringList | ) | [protected, pure virtual] |
This abstract method is called when one or more new lines were written by m_process. It doesn't matter if the line breaks are done by CR+LF or by CR or by LF.
It is made sure that this method is called always with complete lines - that means with lines which are terminated by a line break. This method will never be called with with uncomplete lines.
stringList | This parameter holds a QStringList, and each QString in this list will contain exactly one line of the output. |
Implemented in get_streamripper_version, and ripping.
Referenced by read_console_output().
QString console_reader::QByteArray_to_QString | ( | QByteArray & | m_byteArray | ) | [protected, virtual] |
This function is used internally to convert the output of m_process (a QByteArray) to a QString before splitting it into individual lines and providing it to interpretate_console_output(QStringList& stringList). To do so, you need information about the encoding. This standard implementation just uses QString::fromLocal8Bit() to convert. Reimplement this function if you want to handle the encoding differently.
m_byteArray | The QByteArray that has to be converted to QString. |
Definition at line 40 of file console_reader.cpp.
Referenced by read_console_output().
void console_reader::read_console_output | ( | ) | [private, slot] |
This private slot is connected to the signal readReady() from m_process and reads the output of m_process. This slot makes sure that the output is made available line-by-line to interpretate_console_output(QStringList& stringList).
If the last line of the output of m_process isn't terminated by a line break when this slot is called, this line is buffered internally in m_uncomplete_line, and when later the m_process has completed the line, interpretate_console_output(QStringList& stringList) is called with the complete line.
You can use QByteArray_to_QString(QByteArray& m_byteArray) and internal_splitBehavior to fine-tune the conversion from QByteArray to nicely separated lines.
Definition at line 45 of file console_reader.cpp.
References internal_splitBehavior, interpretate_console_output(), m_process, m_uncomplete_line, and QByteArray_to_QString().
Referenced by console_reader().
QString::SplitBehavior console_reader::internal_splitBehavior [protected] |
This member is used to determinate the handling of empty lines. By default, it is set to QString::KeepEmptyParts and the QStringList passed to interpretate_console_output(QStringList& stringList) contains the output "as is".
You can also set this member to QString::SkipEmptyParts and empty lines will be oppressed.
Definition at line 111 of file console_reader.h.
Referenced by console_reader(), read_console_output(), and ripping::ripping().
KProcess console_reader::m_process [protected] |
Use this member to start the external program. You can access and modify it like you want. However, stderr and stdout are merged - leave this at it is. And don't read data from the output - Instead, implement interpretate_console_output(QStringList& stringList).
Definition at line 116 of file console_reader.h.
Referenced by streamripper_base::abortStreamripper(), console_reader(), get_streamripper_version::get_streamripper_version(), read_console_output(), ripping::ripping(), ripping::shutDown(), streamripper_base::startStreamripper(), ripping::startStreamripper(), ~console_reader(), and get_stream_info::~get_stream_info().
QString console_reader::m_uncomplete_line [private] |
This private member is used by read_console_output() to buffer the lines that m_process has started to write to the console but which aren't yet finished (means: which aren't terminated by a line break). They are buffered until m_process terminates them by a line break.
Definition at line 123 of file console_reader.h.
Referenced by read_console_output().