Kstars

ksfilereader.h
1/*
2 SPDX-FileCopyrightText: 2007 James B. Bowlin <bowlin@mindspring.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#pragma once
8
9#include <QFile>
10#include <QObject>
11#include <QTextStream>
12#include <climits>
13
14class QString;
15
16/**
17 * @class KSFileReader
18 * I totally rewrote this because the earlier scheme of reading all the lines of
19 * a file into a buffer before processing is actually extremely inefficient
20 * because it makes it impossible to interleave file reading and data processing
21 * which all modern computers can do. It also force large data files to be
22 * split up into many smaller files which made dealing with the data much more
23 * difficult and made the code to read in the data needlessly complicated. A
24 * simple subclassing of QTextStream fixed all of the above problems (IMO).
25 *
26 * I had added periodic progress reports based on line number to several parts
27 * of the code that read in large files. So I combined that duplicated code
28 * into one place which was naturally here. The progress code causes almost
29 * nothing extra to take place when reading a file that does not use it. The
30 * only thing extra is incrementing the line number. For files that you want to
31 * emit periodic progress reports, you call setProgress() once setting up the
32 * message to display and the intervals the message will be emitted. Then
33 * inside the loop you call showProgress(). This is an extra call in the read
34 * loop but it just does an integer compare and almost always returns. We could
35 * inline it to reduce the call overhead but I don't think it makes a bit of
36 * difference considering all of the processing that takes place when we read in
37 * a line from a data file.
38 *
39 * NOTE: We no longer close the file like the previous version did. I've
40 * changed the code where this was assumed.
41 *
42 * There are two ways to use this class. One is pass in a QFile& in the
43 * constructor which is included only for backward compatibility. The preferred
44 * way is to just instantiate KSFileReader with no parameters and then use the
45 * open( QString fname ) method to let this class handle the file opening which
46 * helps take unneeded complexity out of the calling classes. I didn't make a
47 * constructor with the filename in it because we need to be able to inform the
48 * caller of an error opening the file, hence the bool open(filename) method.
49 *
50 *
51 * -- James B. Bowlin
52 */
53
54class KSFileReader : public QObject, public QTextStream
55{
57
58 public:
59 /**
60 * @short this is the preferred constructor. You can then use
61 * the open() method to let this class open the file for you.
62 */
63 explicit KSFileReader(qint64 maxLen = 1024);
64
65 /**
66 * Constructor
67 * @param file is a previously opened (for reading) file.
68 * @param maxLen sets the maximum line length before wrapping. Setting
69 * this parameter should help efficiency. The little max-length.pl
70 * script will tell you the maximum line length of files.
71 */
72 explicit KSFileReader(QFile &file, qint64 maxLen = 1024);
73
74 /**
75 * @short opens the file fname from the QStandardPaths::AppLocalDataLocation directory and uses that
76 * file for the QTextStream.
77 *
78 * @param fname the name of the file to open
79 * @return returns true on success. Prints an error message and returns
80 * false on failure.
81 */
82 bool open(const QString &fname);
83
84 /**
85 * @short opens the file with full path fname and uses that
86 * file for the QTextStream. open() locates QStandardPaths::AppLocalDataLocation behind the scenes,
87 * so passing fname such that
88 * QString fname = KSPaths::locate(QStandardPaths::AppLocalDataLocation, "file_name" );
89 * is equivalent
90 *
91 * @param fname full path to directory + name of the file to open
92 * @return returns true on success. Prints an error message and returns
93 * false on failure.
94 */
95 bool openFullPath(const QString &fname);
96
97 /**
98 * @return true if we are not yet at the end of the file.
99 * (I added this to be compatible with existing code.)
100 */
101 bool hasMoreLines() const { return !QTextStream::atEnd(); }
102
103 /**
104 * @short increments the line number and returns the next line from the file as a QString.
105 */
107 {
108 m_curLine++;
109 return QTextStream::readLine(m_maxLen);
110 }
111
112 /** @short returns the current line number */
113 int lineNumber() const { return m_curLine; }
114
115 /**
116 * @short Prepares this instance to emit progress reports on how much
117 * of the file has been read (in percent).
118 * @param label the label
119 * @param lastLine the number of lines to be read
120 * @param numUpdates the number of progress reports to send
121 */
122 void setProgress(QString label, unsigned int lastLine, unsigned int numUpdates = 10);
123
124 /**
125 * @short emits progress reports when required and updates bookkeeping
126 * for when to send the next report. This might seem slow at first
127 * glance but almost all the time we are just doing an integer compare
128 * and returning. If you are worried about speed we can inline it.
129 * It could also safely be included in the readLine() method since
130 * m_targetLine is set to MAXUINT in the constructor.
131 */
132 void showProgress();
133
134 signals:
135 void progressText(const QString &message);
136
137 private:
138 QFile m_file;
139 qint64 m_maxLen;
140 unsigned int m_curLine { 0 };
141
142 unsigned int m_totalLines { 0 };
143 unsigned int m_targetLine { UINT_MAX };
144 unsigned int m_targetIncrement { 0 };
145 QString m_label;
146};
I totally rewrote this because the earlier scheme of reading all the lines of a file into a buffer be...
int lineNumber() const
returns the current line number
bool openFullPath(const QString &fname)
opens the file with full path fname and uses that file for the QTextStream.
void 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).
QString readLine()
increments the line number and returns the next line from the file as a QString.
KSFileReader(qint64 maxLen=1024)
this is the preferred constructor.
bool hasMoreLines() const
bool open(const QString &fname)
opens the file fname from the QStandardPaths::AppLocalDataLocation directory and uses that file for t...
void showProgress()
emits progress reports when required and updates bookkeeping for when to send the next report.
Q_OBJECTQ_OBJECT
bool atEnd() const const
QString readLine(qint64 maxlen)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:15:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.