KSFileReader

Search for usage in LXR

KSFileReader Class Reference

#include <ksfilereader.h>

Inheritance diagram for KSFileReader:

Signals

void progressText (const QString &message)
 

Public Member Functions

 KSFileReader (QFile &file, qint64 maxLen=1024)
 
 KSFileReader (qint64 maxLen=1024)
 
bool hasMoreLines () const
 
int lineNumber () const
 
bool open (const QString &fname)
 
bool openFullPath (const QString &fname)
 
QString readLine ()
 
void setProgress (QString label, unsigned int lastLine, unsigned int numUpdates=10)
 
void showProgress ()
 
- Public Member Functions inherited from QObject
 QObject (QObject *parent)
 
bool blockSignals (bool block)
 
const QObjectListchildren () const const
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const
 
void deleteLater ()
 
void destroyed (QObject *obj)
 
bool disconnect (const char *signal, const QObject *receiver, const char *method) const const
 
bool disconnect (const QObject *receiver, const char *method) const const
 
void dumpObjectInfo ()
 
void dumpObjectInfo () const const
 
void dumpObjectTree ()
 
void dumpObjectTree () const const
 
QList< QByteArraydynamicPropertyNames () const const
 
virtual bool event (QEvent *e)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
findChild (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegExp &regExp, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QString &name, Qt::FindChildOptions options) const const
 
bool inherits (const char *className) const const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const const
 
bool isWindowType () const const
 
void killTimer (int id)
 
virtual const QMetaObjectmetaObject () const const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const const
 
void objectNameChanged (const QString &objectName)
 
QObjectparent () const const
 
QVariant property (const char *name) const const
 
 Q_CLASSINFO (Name, Value)
 
 Q_DISABLE_COPY (Class)
 
 Q_DISABLE_COPY_MOVE (Class)
 
 Q_DISABLE_MOVE (Class)
 
 Q_EMIT Q_EMIT
 
 Q_ENUM (...)
 
 Q_ENUM_NS (...)
 
 Q_ENUMS (...)
 
 Q_FLAG (...)
 
 Q_FLAG_NS (...)
 
 Q_FLAGS (...)
 
 Q_GADGET Q_GADGET
 
 Q_INTERFACES (...)
 
 Q_INVOKABLE Q_INVOKABLE
 
 Q_NAMESPACE Q_NAMESPACE
 
 Q_NAMESPACE_EXPORT (EXPORT_MACRO)
 
 Q_OBJECT Q_OBJECT
 
 Q_PROPERTY (...)
 
 Q_REVISION Q_REVISION
 
 Q_SET_OBJECT_NAME (Object)
 
 Q_SIGNAL Q_SIGNAL
 
 Q_SIGNALS Q_SIGNALS
 
 Q_SLOT Q_SLOT
 
 Q_SLOTS Q_SLOTS
 
qFindChild (const QObject *obj, const QString &name)
 
QList< T > qFindChildren (const QObject *obj, const QRegExp &regExp)
 
QList< T > qFindChildren (const QObject *obj, const QString &name)
 
qobject_cast (const QObject *object)
 
qobject_cast (QObject *object)
 
 QT_NO_NARROWING_CONVERSIONS_IN_CONNECT QT_NO_NARROWING_CONVERSIONS_IN_CONNECT
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const char *name, const QVariant &value)
 
bool signalsBlocked () const const
 
int startTimer (int interval, Qt::TimerType timerType)
 
int startTimer (std::chrono::milliseconds time, Qt::TimerType timerType)
 
QThreadthread () const const
 
- Public Member Functions inherited from QTextStream
 QTextStream (const QByteArray &array, QIODevice::OpenMode openMode)
 
 QTextStream (FILE *fileHandle, QIODevice::OpenMode openMode)
 
 QTextStream (QByteArray *array, QIODevice::OpenMode openMode)
 
 QTextStream (QIODevice *device)
 
 QTextStream (QString *string, QIODevice::OpenMode openMode)
 
bool atEnd () const const
 
bool autoDetectUnicode () const const
 
QTextStreambin (QTextStream &s)
 
QTextStreambom (QTextStream &s)
 
QTextStreamcenter (QTextStream &s)
 
QTextCodeccodec () const const
 
QTextStreamdec (QTextStream &s)
 
QIODevicedevice () const const
 
QTextStreamendl (QTextStream &s)
 
QTextStream::FieldAlignment fieldAlignment () const const
 
int fieldWidth () const const
 
QTextStreamfixed (QTextStream &s)
 
void flush ()
 
QTextStreamflush (QTextStream &s)
 
QTextStreamforcepoint (QTextStream &s)
 
QTextStreamforcesign (QTextStream &s)
 
bool generateByteOrderMark () const const
 
QTextStreamhex (QTextStream &s)
 
int integerBase () const const
 
QTextStreamleft (QTextStream &s)
 
QLocale locale () const const
 
QTextStreamlowercasebase (QTextStream &s)
 
QTextStreamlowercasedigits (QTextStream &s)
 
QTextStreamnoforcepoint (QTextStream &s)
 
QTextStreamnoforcesign (QTextStream &s)
 
QTextStreamnoshowbase (QTextStream &s)
 
QTextStream::NumberFlags numberFlags () const const
 
QTextStreamoct (QTextStream &s)
 
QTextStreamoperator<< (char c)
 
QTextStreamoperator<< (const char *string)
 
QTextStreamoperator<< (const QByteArray &array)
 
QTextStreamoperator<< (const QString &string)
 
QTextStreamoperator<< (const QStringRef &string)
 
QTextStreamoperator<< (const void *ptr)
 
QTextStreamoperator<< (double f)
 
QTextStreamoperator<< (float f)
 
QTextStreamoperator<< (int i)
 
QTextStreamoperator<< (long i)
 
QTextStreamoperator<< (QChar c)
 
QTextStreamoperator<< (QLatin1String string)
 
QTextStreamoperator<< (qlonglong i)
 
QTextStreamoperator<< (QStringView string)
 
QTextStreamoperator<< (qulonglong i)
 
QTextStreamoperator<< (short i)
 
QTextStreamoperator<< (unsigned int i)
 
QTextStreamoperator<< (unsigned long i)
 
QTextStreamoperator<< (unsigned short i)
 
QTextStreamoperator>> (char &c)
 
QTextStreamoperator>> (char *c)
 
QTextStreamoperator>> (double &f)
 
QTextStreamoperator>> (float &f)
 
QTextStreamoperator>> (int &i)
 
QTextStreamoperator>> (long &i)
 
QTextStreamoperator>> (QByteArray &array)
 
QTextStreamoperator>> (QChar &c)
 
QTextStreamoperator>> (qlonglong &i)
 
QTextStreamoperator>> (QString &str)
 
QTextStreamoperator>> (qulonglong &i)
 
QTextStreamoperator>> (short &i)
 
QTextStreamoperator>> (unsigned int &i)
 
QTextStreamoperator>> (unsigned long &i)
 
QTextStreamoperator>> (unsigned short &i)
 
QChar padChar () const const
 
qint64 pos () const const
 
QTextStreamManipulator qSetFieldWidth (int width)
 
QTextStreamManipulator qSetPadChar (QChar ch)
 
QTextStreamManipulator qSetRealNumberPrecision (int precision)
 
QString read (qint64 maxlen)
 
QString readAll ()
 
QString readLine (qint64 maxlen)
 
bool readLineInto (QString *line, qint64 maxlen)
 
QTextStream::RealNumberNotation realNumberNotation () const const
 
int realNumberPrecision () const const
 
void reset ()
 
QTextStreamreset (QTextStream &s)
 
void resetStatus ()
 
QTextStreamright (QTextStream &s)
 
QTextStreamscientific (QTextStream &s)
 
bool seek (qint64 pos)
 
void setAutoDetectUnicode (bool enabled)
 
void setCodec (const char *codecName)
 
void setCodec (QTextCodec *codec)
 
void setDevice (QIODevice *device)
 
void setFieldAlignment (QTextStream::FieldAlignment mode)
 
void setFieldWidth (int width)
 
void setGenerateByteOrderMark (bool generate)
 
void setIntegerBase (int base)
 
void setLocale (const QLocale &locale)
 
void setNumberFlags (QTextStream::NumberFlags flags)
 
void setPadChar (QChar ch)
 
void setRealNumberNotation (QTextStream::RealNumberNotation notation)
 
void setRealNumberPrecision (int precision)
 
void setStatus (QTextStream::Status status)
 
void setString (QString *string, QIODevice::OpenMode openMode)
 
QTextStreamshowbase (QTextStream &s)
 
void skipWhiteSpace ()
 
QTextStream::Status status () const const
 
QStringstring () const const
 
QTextStreamuppercasebase (QTextStream &s)
 
QTextStreamuppercasedigits (QTextStream &s)
 
QTextStreamws (QTextStream &s)
 

Additional Inherited Members

- Public Types inherited from QTextStream
enum  FieldAlignment
 
enum  NumberFlag
 
enum  RealNumberNotation
 
enum  Status
 
- Properties inherited from QObject
 objectName
 
- Static Public Member Functions inherited from QObject
QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, Functor functor)
 
bool disconnect (const QMetaObject::Connection &connection)
 
bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method)
 
bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
 
bool disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
 
QString tr (const char *sourceText, const char *disambiguation, int n)
 
QString trUtf8 (const char *sourceText, const char *disambiguation, int n)
 
- Public Attributes inherited from QObject
typedef QObjectList
 
- Public Attributes inherited from QTextStream
 AlignAccountingStyle
 
 AlignCenter
 
 AlignLeft
 
 AlignRight
 
 FixedNotation
 
 ForcePoint
 
 ForceSign
 
typedef NumberFlags
 
 Ok
 
 ReadCorruptData
 
 ReadPastEnd
 
 ScientificNotation
 
 ShowBase
 
 SmartNotation
 
 UppercaseBase
 
 UppercaseDigits
 
 WriteFailed
 
- Protected Member Functions inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
bool isSignalConnected (const QMetaMethod &signal) const const
 
int receivers (const char *signal) const const
 
QObjectsender () const const
 
int senderSignalIndex () const const
 
virtual void timerEvent (QTimerEvent *event)
 

Detailed Description

I totally rewrote this because the earlier scheme of reading all the lines of a file into a buffer before processing is actually extremely inefficient because it makes it impossible to interleave file reading and data processing which all modern computers can do. It also force large data files to be split up into many smaller files which made dealing with the data much more difficult and made the code to read in the data needlessly complicated. A simple subclassing of QTextStream fixed all of the above problems (IMO).

I had added periodic progress reports based on line number to several parts of the code that read in large files. So I combined that duplicated code into one place which was naturally here. The progress code causes almost nothing extra to take place when reading a file that does not use it. The only thing extra is incrementing the line number. For files that you want to emit periodic progress reports, you call setProgress() once setting up the message to display and the intervals the message will be emitted. Then inside the loop you call showProgress(). This is an extra call in the read loop but it just does an integer compare and almost always returns. We could inline it to reduce the call overhead but I don't think it makes a bit of difference considering all of the processing that takes place when we read in a line from a data file.

NOTE: We no longer close the file like the previous version did. I've changed the code where this was assumed.

There are two ways to use this class. One is pass in a QFile& in the constructor which is included only for backward compatibility. The preferred way is to just instantiate KSFileReader with no parameters and then use the open( QString fname ) method to let this class handle the file opening which helps take unneeded complexity out of the calling classes. I didn't make a constructor with the filename in it because we need to be able to inform the caller of an error opening the file, hence the bool open(filename) method.

– James B. Bowlin

Definition at line 53 of file ksfilereader.h.

Constructor & Destructor Documentation

◆ KSFileReader() [1/2]

KSFileReader::KSFileReader ( qint64  maxLen = 1024)
explicit

this is the preferred constructor.

You can then use the open() method to let this class open the file for you.

Definition at line 19 of file ksfilereader.cpp.

◆ KSFileReader() [2/2]

KSFileReader::KSFileReader ( QFile file,
qint64  maxLen = 1024 
)
explicit

Constructor.

Parameters
fileis a previously opened (for reading) file.
maxLensets the maximum line length before wrapping. Setting this parameter should help efficiency. The little max-length.pl script will tell you the maximum line length of files.

Definition at line 23 of file ksfilereader.cpp.

Member Function Documentation

◆ hasMoreLines()

bool KSFileReader::hasMoreLines ( ) const
inline
Returns
true if we are not yet at the end of the file. (I added this to be compatible with existing code.)

Definition at line 100 of file ksfilereader.h.

◆ lineNumber()

int KSFileReader::lineNumber ( ) const
inline

returns the current line number

Definition at line 112 of file ksfilereader.h.

◆ open()

bool KSFileReader::open ( const QString fname)

opens the file fname from the QStandardPaths::AppLocalDataLocation directory and uses that file for the QTextStream.

Parameters
fnamethe name of the file to open
Returns
returns true on success. Prints an error message and returns false on failure.

Definition at line 31 of file ksfilereader.cpp.

◆ openFullPath()

bool KSFileReader::openFullPath ( const QString fname)

opens the file with full path fname and uses that file for the QTextStream.

open() locates QStandardPaths::AppLocalDataLocation behind the scenes, so passing fname such that QString fname = KSPaths::locate(QStandardPaths::AppLocalDataLocation, "file_name" ); is equivalent

Parameters
fnamefull path to directory + name of the file to open
Returns
returns true on success. Prints an error message and returns false on failure.

Definition at line 43 of file ksfilereader.cpp.

◆ readLine()

QString KSFileReader::readLine ( )
inline

increments the line number and returns the next line from the file as a QString.

Definition at line 105 of file ksfilereader.h.

◆ setProgress()

void KSFileReader::setProgress ( QString  label,
unsigned int  lastLine,
unsigned int  numUpdates = 10 
)

Prepares this instance to emit progress reports on how much of the file has been read (in percent).

Parameters
labelthe label
lastLinethe number of lines to be read
numUpdatesthe number of progress reports to send

Definition at line 56 of file ksfilereader.cpp.

◆ showProgress()

void KSFileReader::showProgress ( )

emits progress reports when required and updates bookkeeping for when to send the next report.

This might seem slow at first glance but almost all the time we are just doing an integer compare and returning. If you are worried about speed we can inline it. It could also safely be included in the readLine() method since m_targetLine is set to MAXUINT in the constructor.

Definition at line 68 of file ksfilereader.cpp.


The documentation for this class was generated from the following files:
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Aug 18 2022 03:56:46 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.