7 #include "IException.h"
8 #include "IException.h"
22 TextFile::TextFile() {
41 TextFile::TextFile(
const QString &filename,
42 const char *openmode,
const char *extension) {
45 Open(filename, openmode, extension);
72 TextFile::TextFile(
const QString &filename,
const char *openmode,
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);
109 TextFile::TextFile(
const char *filename,
const char *openmode,
110 std::vector<QString> &lines,
const int &maxLinesToReadWrite,
111 const bool skipComments) {
112 TextFile(QString(filename), openmode, lines, maxLinesToReadWrite, skipComments);
140 TextFile::TextFile(
const QString &filename,
const char *openmode,
141 QString *lines,
const int &maxLinesToReadWrite,
142 const bool skipComments) {
145 Open(filename, openmode);
146 if(p_openmode == 1) {
147 GetFile(lines, maxLinesToReadWrite, skipComments);
150 PutFile(lines, maxLinesToReadWrite);
178 TextFile::TextFile(
const char *filename,
const char *openmode,
179 QString *lines,
const int &maxLinesToReadWrite,
180 const bool skipComments) {
181 TextFile(QString(filename), openmode, lines, maxLinesToReadWrite, skipComments);
186 TextFile::~TextFile() {
217 void TextFile::Open(
const QString &filename,
const char *openmode,
218 const char *extension) {
227 if(p_stream.is_open()) {
228 QString message =
"TextFile:Open:-> Already opened with this object: ["
229 + QString(openmode) +
"]:[" + p_filename +
"]";
230 throw IException(IException::Programmer, message, _FILEINFO_);
239 p_filename = filenameTmp.
expanded();
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 +
"]";
260 throw IException(IException::Programmer, message, _FILEINFO_);
264 if(p_openmode == 1) {
265 p_stream.open(p_filename.toLatin1().data(), fstream::in);
268 else if(p_openmode == 2) {
271 QString message =
"TextFile:Open: -> Output file already exists ["
272 + QString(openmode) +
"]:[" + p_filename +
"]";
273 throw IException(IException::Io, message, _FILEINFO_);
276 p_stream.open(p_filename.toLatin1().data(), fstream::in | fstream::out | fstream::trunc);
280 else if(p_openmode == 3) {
281 p_stream.open(p_filename.toLatin1().data(), fstream::in | fstream::out | fstream::trunc);
284 else if(p_openmode == 4) {
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);
294 if(!p_stream.is_open()) {
295 QString message =
"TextFile:Open:-> Unable to open: ["
296 + QString(openmode) +
"]:[" + p_filename +
"]";
297 throw IException(IException::Io, message, _FILEINFO_);
301 bool TextFile::OpenChk(
bool bailIfNotOpen) {
302 if(p_stream.is_open()) {
307 QString message =
"TextFile::-> File not open: [" + p_filename +
"]";
308 throw IException(IException::Programmer, message, _FILEINFO_);
317 void TextFile::Rewind() {
322 p_stream.seekg(0, ios_base::beg);
326 void TextFile::Close() {
327 if(p_stream.is_open()) {
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) {
378 PutLine(lines[lineCount]);
383 void TextFile::PutFile(
const QString *lines,
const int &maxLinesToWrite) {
387 if(maxLinesToWrite > 0) {
388 if(lineCount > maxLinesToWrite) {
392 else if(lines[lineCount] ==
"\0") {
395 PutLine(lines[lineCount]);
411 bool TextFile::GetLine(QString &line,
bool skipComments) {
412 return (p_GetLine(line, skipComments));
424 bool TextFile::GetLine(
bool skipComments) {
426 return (p_GetLine(line, skipComments));
437 bool TextFile::GetLineNoFilter(QString &line) {
438 return (p_GetLine(line,
false));
447 bool TextFile::GetLineNoFilter() {
449 return (p_GetLine(line,
false));
463 bool TextFile::p_GetLine(QString &line,
bool chkComment) {
469 std::string lineStdString;
470 getline(p_stream, lineStdString);
471 line = lineStdString.c_str();
479 if(!p_stream.good()) {
481 QString message =
"TextFile:GetLine: -> Error reading text file: ["
483 throw IException(IException::Io, message, _FILEINFO_);
488 if(p_commentString.length()) {
489 int locComment = line.indexOf(p_commentString);
490 if(locComment != -1) {
491 int afterWhiteSpace = line.indexOf(QRegExp(
"[^\\s]"));
492 if((locComment == 0) || (locComment == afterWhiteSpace)) {
493 return p_GetLine(line, chkComment);
508 void TextFile::PutLine(
const QString &line) {
509 PutLine(line.toLatin1().data());
522 void TextFile::PutLine(
const char *line) {
526 p_stream << line << p_newLineString;
528 if(!p_stream.good()) {
529 if(p_openmode != 1) {
530 QString message =
"TextFile:PutLine: -> Error writing text file: ["
532 throw IException(IException::Io, message, _FILEINFO_);
536 "TextFile:PutLine: -> Attempt to write to INPUT - Read Only text file: ["
538 throw IException(IException::Programmer, message, _FILEINFO_);
549 void TextFile::PutLineComment(
const QString &line) {
550 PutLine(p_commentString + line);
559 void TextFile::PutLineComment(
const char *line) {
560 PutLine(p_commentString + QString(line));
564 QString TextFile::GetComment() {
565 return(p_commentString);
577 void TextFile::SetComment(
const char *commentString) {
578 p_commentString = commentString;
582 QString TextFile::GetNewLine() {
583 return(p_newLineString);
593 void TextFile::SetNewLine(
const char *newLineString) {
594 p_newLineString = newLineString;
607 int TextFile::LineCount(
const int &maxLinesToRead) {
612 bool eofStat =
false;
618 streampos savePos = p_stream.tellg();
619 p_stream.seekg(0, ios_base::beg);
623 if(maxLinesToRead > 0) {
624 while((getline(p_stream, tmpLine)) && (lineCount <= maxLinesToRead)) {
629 while(getline(p_stream, tmpLine)) {
638 p_stream.seekg(savePos, ios_base::beg);
641 p_stream.seekg(0, ios_base::end);
652 streamsize TextFile::Size() {
657 bool eofStat =
false;
663 streampos savePos = p_stream.tellg();
664 p_stream.seekg(0, ios_base::end);
665 streamsize bytes = p_stream.tellg();
666 p_stream.seekg(savePos, ios_base::beg);
669 p_stream.seekg(0, ios_base::end);