38 TextFile::TextFile() {
57 TextFile::TextFile(
const QString &filename,
58 const char *openmode,
const char *extension) {
61 Open(filename, openmode, extension);
88 TextFile::TextFile(
const QString &filename,
const char *openmode,
89 std::vector<QString> &lines,
const int &maxLinesToReadWrite,
90 const bool skipComments) {
93 Open(filename, openmode);
95 GetFile(lines, maxLinesToReadWrite, skipComments);
98 PutFile(lines, maxLinesToReadWrite);
125 TextFile::TextFile(
const char *filename,
const char *openmode,
126 std::vector<QString> &lines,
const int &maxLinesToReadWrite,
127 const bool skipComments) {
128 TextFile(QString(filename), openmode, lines, maxLinesToReadWrite, skipComments);
156 TextFile::TextFile(
const QString &filename,
const char *openmode,
157 QString *lines,
const int &maxLinesToReadWrite,
158 const bool skipComments) {
161 Open(filename, openmode);
162 if(p_openmode == 1) {
163 GetFile(lines, maxLinesToReadWrite, skipComments);
166 PutFile(lines, maxLinesToReadWrite);
194 TextFile::TextFile(
const char *filename,
const char *openmode,
195 QString *lines,
const int &maxLinesToReadWrite,
196 const bool skipComments) {
197 TextFile(QString(filename), openmode, lines, maxLinesToReadWrite, skipComments);
202 TextFile::~TextFile() {
233 void TextFile::Open(
const QString &filename,
const char *openmode,
234 const char *extension) {
243 if(p_stream.is_open()) {
244 QString message =
"TextFile:Open:-> Already opened with this object: ["
245 + QString(openmode) +
"]:[" + p_filename +
"]";
254 filenameTmp.addExtension(extension);
255 p_filename = filenameTmp.expanded();
261 if(chkOpenmode ==
"input") {
264 else if(chkOpenmode ==
"output") {
267 else if(chkOpenmode ==
"overwrite") {
270 else if(chkOpenmode ==
"append") {
274 QString message =
"TextFile::-> Unknown openmode: (input, output, overwrite, append):["
275 + QString(openmode) +
"]:[" + p_filename +
"]";
280 if(p_openmode == 1) {
281 p_stream.open(p_filename.toLatin1().data(), fstream::in);
284 else if(p_openmode == 2) {
286 if(filenameTmp.fileExists() && QFileInfo(filenameTmp.toString()).size() > 0) {
287 QString message =
"TextFile:Open: -> Output file already exists ["
288 + QString(openmode) +
"]:[" + p_filename +
"]";
292 p_stream.open(p_filename.toLatin1().data(), fstream::in | fstream::out | fstream::trunc);
296 else if(p_openmode == 3) {
297 p_stream.open(p_filename.toLatin1().data(), fstream::in | fstream::out | fstream::trunc);
300 else if(p_openmode == 4) {
302 if(filenameTmp.fileExists()) {
303 p_stream.open(p_filename.toLatin1().data(), fstream::in | fstream::out | fstream::ate);
306 p_stream.open(p_filename.toLatin1().data(), fstream::in | fstream::out | fstream::trunc);
310 if(!p_stream.is_open()) {
311 QString message =
"TextFile:Open:-> Unable to open: ["
312 + QString(openmode) +
"]:[" + p_filename +
"]";
317 bool TextFile::OpenChk(
bool bailIfNotOpen) {
318 if(p_stream.is_open()) {
323 QString message =
"TextFile::-> File not open: [" + p_filename +
"]";
324 throw IException(IException::Programmer, message,
_FILEINFO_);
333 void TextFile::Rewind() {
338 p_stream.seekg(0, ios_base::beg);
342 void TextFile::Close() {
343 if(p_stream.is_open()) {
350 void TextFile::GetFile(std::vector<QString> &lines,
const int &maxLinesToRead,
351 const bool skipComments) {
355 while(GetLine(line, skipComments)) {
356 if(maxLinesToRead > 0) {
357 if(lineCount++ >= maxLinesToRead) {
361 lines.push_back(line);
366 void TextFile::GetFile(QString *lines,
const int &maxLinesToRead,
367 const bool skipComments) {
371 while(GetLine(line, skipComments)) {
372 if(maxLinesToRead > 0) {
373 if(lineCount > maxLinesToRead) {
377 else if(lines[lineCount] ==
"\0") {
380 lines[lineCount] = line;
386 void TextFile::PutFile(std::vector<QString> &lines,
const int &maxLinesToWrite) {
388 for(
int lineCount = 0; lineCount < (int) lines.size(); lineCount++) {
389 if(maxLinesToWrite > 0) {
390 if(lineCount > maxLinesToWrite) {
394 PutLine(lines[lineCount]);
399 void TextFile::PutFile(
const QString *lines,
const int &maxLinesToWrite) {
403 if(maxLinesToWrite > 0) {
404 if(lineCount > maxLinesToWrite) {
408 else if(lines[lineCount] ==
"\0") {
411 PutLine(lines[lineCount]);
427 bool TextFile::GetLine(QString &line,
bool skipComments) {
428 return (p_GetLine(line, skipComments));
440 bool TextFile::GetLine(
bool skipComments) {
442 return (p_GetLine(line, skipComments));
453 bool TextFile::GetLineNoFilter(QString &line) {
454 return (p_GetLine(line,
false));
463 bool TextFile::GetLineNoFilter() {
465 return (p_GetLine(line,
false));
479 bool TextFile::p_GetLine(QString &line,
bool chkComment) {
485 std::string lineStdString;
486 getline(p_stream, lineStdString);
487 line = lineStdString.c_str();
495 if(!p_stream.good()) {
497 QString message =
"TextFile:GetLine: -> Error reading text file: ["
504 if(p_commentString.length()) {
505 int locComment = line.indexOf(p_commentString);
506 if(locComment != -1) {
507 int afterWhiteSpace = line.indexOf(QRegExp(
"[^\\s]"));
508 if((locComment == 0) || (locComment == afterWhiteSpace)) {
509 return p_GetLine(line, chkComment);
524 void TextFile::PutLine(
const QString &line) {
525 PutLine(line.toLatin1().data());
538 void TextFile::PutLine(
const char *line) {
542 p_stream << line << p_newLineString;
544 if(!p_stream.good()) {
545 if(p_openmode != 1) {
546 QString message =
"TextFile:PutLine: -> Error writing text file: ["
552 "TextFile:PutLine: -> Attempt to write to INPUT - Read Only text file: ["
565 void TextFile::PutLineComment(
const QString &line) {
566 PutLine(p_commentString + line);
575 void TextFile::PutLineComment(
const char *line) {
576 PutLine(p_commentString + QString(line));
580 QString TextFile::GetComment() {
581 return(p_commentString);
593 void TextFile::SetComment(
const char *commentString) {
594 p_commentString = commentString;
598 QString TextFile::GetNewLine() {
599 return(p_newLineString);
609 void TextFile::SetNewLine(
const char *newLineString) {
610 p_newLineString = newLineString;
623 int TextFile::LineCount(
const int &maxLinesToRead) {
628 bool eofStat =
false;
634 streampos savePos = p_stream.tellg();
635 p_stream.seekg(0, ios_base::beg);
639 if(maxLinesToRead > 0) {
640 while((getline(p_stream, tmpLine)) && (lineCount <= maxLinesToRead)) {
645 while(getline(p_stream, tmpLine)) {
654 p_stream.seekg(savePos, ios_base::beg);
657 p_stream.seekg(0, ios_base::end);
668 streamsize TextFile::Size() {
673 bool eofStat =
false;
679 streampos savePos = p_stream.tellg();
680 p_stream.seekg(0, ios_base::end);
681 streamsize bytes = p_stream.tellg();
682 p_stream.seekg(savePos, ios_base::beg);
685 p_stream.seekg(0, ios_base::end);
File name manipulation and expansion.
#define _FILEINFO_
Macro for the filename and line number.
IString DownCase()
Converts all upper case letters in the object IString into lower case characters. ...
Provides access to sequential ASCII stream I/O.
Adds specific functionality to C++ strings.