• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kstars

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

KDE's Doxygen guidelines are available online.

kstars

Skip menu "kstars"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal