42 const char *openmode,
const char *extension) {
45 Open(filename, openmode, extension);
73 std::vector<QString> &lines,
const int &maxLinesToReadWrite,
74 const bool skipComments) {
77 Open(filename, openmode);
79 GetFile(lines, maxLinesToReadWrite, skipComments);
82 PutFile(lines, maxLinesToReadWrite);
110 std::vector<QString> &lines,
const int &maxLinesToReadWrite,
111 const bool skipComments) {
112 TextFile(QString(filename), openmode, lines, maxLinesToReadWrite, skipComments);
141 QString *lines,
const int &maxLinesToReadWrite,
142 const bool skipComments) {
145 Open(filename, openmode);
147 GetFile(lines, maxLinesToReadWrite, skipComments);
150 PutFile(lines, maxLinesToReadWrite);
179 QString *lines,
const int &maxLinesToReadWrite,
180 const bool skipComments) {
181 TextFile(QString(filename), openmode, lines, maxLinesToReadWrite, skipComments);
218 const char *extension) {
228 QString message =
"TextFile:Open:-> Already opened with this object: ["
229 + QString(openmode) +
"]:[" +
p_filename +
"]";
238 filenameTmp.addExtension(extension);
245 if(chkOpenmode ==
"input") {
248 else if(chkOpenmode ==
"output") {
251 else if(chkOpenmode ==
"overwrite") {
254 else if(chkOpenmode ==
"append") {
258 QString message =
"TextFile::-> Unknown openmode: (input, output, overwrite, append):["
259 + QString(openmode) +
"]:[" +
p_filename +
"]";
270 if(filenameTmp.fileExists() && QFileInfo(filenameTmp.toString()).size() > 0) {
271 QString message =
"TextFile:Open: -> Output file already exists ["
272 + QString(openmode) +
"]:[" +
p_filename +
"]";
276 p_stream.open(
p_filename.toLatin1().data(), fstream::in | fstream::out | fstream::trunc);
281 p_stream.open(
p_filename.toLatin1().data(), fstream::in | fstream::out | fstream::trunc);
286 if(filenameTmp.fileExists()) {
287 p_stream.open(
p_filename.toLatin1().data(), fstream::in | fstream::out | fstream::ate);
290 p_stream.open(
p_filename.toLatin1().data(), fstream::in | fstream::out | fstream::trunc);
295 QString message =
"TextFile:Open:-> Unable to open: ["
296 + QString(openmode) +
"]:[" +
p_filename +
"]";
301 bool TextFile::OpenChk(
bool bailIfNotOpen) {
307 QString message =
"TextFile::-> File not open: [" +
p_filename +
"]";
334 void TextFile::GetFile(std::vector<QString> &lines,
const int &maxLinesToRead,
335 const bool skipComments) {
339 while(
GetLine(line, skipComments)) {
340 if(maxLinesToRead > 0) {
341 if(lineCount++ >= maxLinesToRead) {
345 lines.push_back(line);
350 void TextFile::GetFile(QString *lines,
const int &maxLinesToRead,
351 const bool skipComments) {
355 while(
GetLine(line, skipComments)) {
356 if(maxLinesToRead > 0) {
357 if(lineCount > maxLinesToRead) {
361 else if(lines[lineCount] ==
"\0") {
364 lines[lineCount] = line;
370 void TextFile::PutFile(std::vector<QString> &lines,
const int &maxLinesToWrite) {
372 for(
int lineCount = 0; lineCount < (int) lines.size(); lineCount++) {
373 if(maxLinesToWrite > 0) {
374 if(lineCount > maxLinesToWrite) {
383 void TextFile::PutFile(
const QString *lines,
const int &maxLinesToWrite) {
387 if(maxLinesToWrite > 0) {
388 if(lineCount > maxLinesToWrite) {
392 else if(lines[lineCount] ==
"\0") {
469 std::string lineStdString;
471 line = lineStdString.c_str();
481 QString message =
"TextFile:GetLine: -> Error reading text file: ["
490 if(locComment != -1) {
491 int afterWhiteSpace = line.indexOf(QRegExp(
"[^\\s]"));
492 if((locComment == 0) || (locComment == afterWhiteSpace)) {
509 PutLine(line.toLatin1().data());
530 QString message =
"TextFile:PutLine: -> Error writing text file: ["
536 "TextFile:PutLine: -> Attempt to write to INPUT - Read Only text file: ["
564 QString TextFile::GetComment() {
582 QString TextFile::GetNewLine() {
612 bool eofStat =
false;
618 streampos savePos =
p_stream.tellg();
623 if(maxLinesToRead > 0) {
624 while((getline(
p_stream, tmpLine)) && (lineCount <= maxLinesToRead)) {
638 p_stream.seekg(savePos, ios_base::beg);
657 bool eofStat =
false;
663 streampos savePos =
p_stream.tellg();
665 streamsize bytes =
p_stream.tellg();
666 p_stream.seekg(savePos, ios_base::beg);
File name manipulation and expansion.
@ Programmer
This error is for when a programmer made an API call that was illegal.
@ Io
A type of error that occurred when performing an actual I/O operation.
Adds specific functionality to C++ strings.
IString DownCase()
Converts all upper case letters in the object IString into lower case characters.
bool GetLine(QString &line, const bool skipComments=true)
Gets next line from file.
TextFile()
Constructs an empty TextFile object.
void Open(const QString &filename, const char *openmode="input", const char *extension="")
Opens a text file.
QString p_newLineString
'newline' string used by PutLine and PutLineComment
void SetComment(const char *commentString="#")
Sets the 'comment' string.
QString p_commentString
'comment' string used by GetLine and PutLineComment
std::fstream p_stream
File stream handle.
int LineCount(const int &maxLinesToRead=0)
Counts number of lines in file.
int p_openmode
openmode of file: Input, Output, Overwrite, Append
void PutLine(const QString &line)
Writes string to file and appends a 'newline' string.
std::streamsize Size()
Counts number of bytes in file.
bool GetLineNoFilter()
Gets next NON-COMMENT line from file.
~TextFile()
Closes file (if still open). Destroys the TextFile object.
void Close()
Closes file. Called automatically by TextFile destructor.
void Rewind()
Sets Read / Write pointer to begining of opened file.
QString p_filename
FileName of the opened file.
bool p_GetLine(QString &line, bool chkComment)
Gets next line from file.
void PutLineComment(const QString &line)
Writes string to file, prepends a 'comment' string and appends a 'newline' string.
void SetNewLine(const char *newLineString="\n")
Sets the 'newline' string.
This is free and unencumbered software released into the public domain.
Namespace for the standard library.