Isis 3 Programmer Reference
TextFile.h
1 #ifndef TextFile_h
2 #define TextFile_h
3 
8 /* SPDX-License-Identifier: CC0-1.0 */
9 #include <fstream>
10 #include <vector>
11 
12 #include <QString>
13 
14 namespace Isis {
38  class TextFile {
39 
40  protected:
41  std::fstream p_stream;
42  int p_openmode;
44  QString p_filename;
45  QString p_commentString;
47  QString p_newLineString;
50  bool p_GetLine(QString &line, bool chkComment);
51 
52  public:
53  // Constructors and destructors
54  TextFile();
55 
56  // implied open file
57  TextFile(const QString &filename, const char *openmode = "input",
58  const char *extension = "");
59 
60  // implied getFile/putFile
61  TextFile(const char *filename, const char *openmode,
62  std::vector<QString> &lines,
63  const int &maxLinesToReadWrite = 0,
64  const bool skipComments = true);
65  TextFile(const QString &filename,
66  const char *openmode, std::vector<QString> &lines,
67  const int &maxLinesToReadWrite = 0,
68  const bool skipComments = true);
69  TextFile(const char *filename,
70  const char *openmode, QString *lines,
71  const int &maxLinesToReadWrite, const bool skipComments = true);
72  TextFile(const QString &filename,
73  const char *openmode, QString *lines,
74  const int &maxLinesToReadWrite, const bool skipComments = true);
75 
76  ~TextFile();
77 
78  void Open(const QString &filename, const char *openmode = "input",
79  const char *extension = "");
80  void Open(const char *filename, const char *openmode = "input",
81  const char *extension = "");
82 
83  bool OpenChk(bool bailIfNotOpen = false);
84 
85  void Rewind();
86  void Close();
87 
88  void GetFile(std::vector<QString> &lines,
89  const int &maxLinesToRead = 0,
90  const bool skipComments = true);
91  void GetFile(QString *lines, const int &maxLinesToRead,
92  const bool skipComments = true);
93 
94  void PutFile(std::vector<QString> &lines,
95  const int &maxLinesToWrite = 0);
96  void PutFile(const QString *lines, const int &maxLinesToWrite);
97 
98  bool GetLine(QString &line, const bool skipComments = true);
99  bool GetLineNoFilter(QString &line);
100 
101  bool GetLine(const bool skipComments = true);
102  bool GetLineNoFilter();
103 
104  void PutLine(const QString &line);
105  void PutLine(const char *line = "");
106 
107  void PutLineComment(const QString &line);
108  void PutLineComment(const char *line = "");
109 
110  QString GetComment();
111  QString GetNewLine();
112 
113  void SetComment(const char *commentString = "#");
114  void SetNewLine(const char *newLineString = "\n");
115 
116  int LineCount(const int &maxLinesToRead = 0);
117  std::streamsize Size();
118  };
119 };
120 
121 #endif
Isis::TextFile::Size
std::streamsize Size()
Counts number of bytes in file.
Definition: TextFile.cpp:652
Isis::TextFile::~TextFile
~TextFile()
Closes file (if still open). Destroys the TextFile object.
Definition: TextFile.cpp:186
Isis::TextFile::Close
void Close()
Closes file. Called automatically by TextFile destructor.
Definition: TextFile.cpp:326
Isis::TextFile::p_GetLine
bool p_GetLine(QString &line, bool chkComment)
Gets next line from file.
Definition: TextFile.cpp:463
Isis::TextFile::p_newLineString
QString p_newLineString
'newline' string used by PutLine and PutLineComment
Definition: TextFile.h:47
Isis::TextFile::TextFile
TextFile()
Constructs an empty TextFile object.
Definition: TextFile.cpp:22
Isis::TextFile
Provides access to sequential ASCII stream I/O.
Definition: TextFile.h:38
Isis::TextFile::GetLine
bool GetLine(QString &line, const bool skipComments=true)
Gets next line from file.
Definition: TextFile.cpp:411
Isis::TextFile::p_openmode
int p_openmode
openmode of file: Input, Output, Overwrite, Append
Definition: TextFile.h:42
Isis::TextFile::SetNewLine
void SetNewLine(const char *newLineString="\n")
Sets the 'newline' string.
Definition: TextFile.cpp:593
Isis::TextFile::Open
void Open(const QString &filename, const char *openmode="input", const char *extension="")
Opens a text file.
Definition: TextFile.cpp:217
Isis::TextFile::LineCount
int LineCount(const int &maxLinesToRead=0)
Counts number of lines in file.
Definition: TextFile.cpp:607
Isis::TextFile::Rewind
void Rewind()
Sets Read / Write pointer to begining of opened file.
Definition: TextFile.cpp:317
Isis::TextFile::p_commentString
QString p_commentString
'comment' string used by GetLine and PutLineComment
Definition: TextFile.h:45
Isis::TextFile::SetComment
void SetComment(const char *commentString="#")
Sets the 'comment' string.
Definition: TextFile.cpp:577
Isis::TextFile::GetLineNoFilter
bool GetLineNoFilter()
Gets next NON-COMMENT line from file.
Definition: TextFile.cpp:447
Isis::TextFile::PutLine
void PutLine(const QString &line)
Writes string to file and appends a 'newline' string.
Definition: TextFile.cpp:508
Isis::TextFile::p_filename
QString p_filename
FileName of the opened file.
Definition: TextFile.h:44
Isis::TextFile::p_stream
std::fstream p_stream
File stream handle.
Definition: TextFile.h:41
Isis::TextFile::PutLineComment
void PutLineComment(const QString &line)
Writes string to file, prepends a 'comment' string and appends a 'newline' string.
Definition: TextFile.cpp:549
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16