Isis 3 Programmer Reference
Isis::TextFile Class Reference

Provides access to sequential ASCII stream I/O. More...

#include <TextFile.h>

Inheritance diagram for Isis::TextFile:
Inheritance graph
Collaboration diagram for Isis::TextFile:
Collaboration graph

Public Member Functions

 TextFile ()
 Constructs an empty TextFile object.
 
 TextFile (const QString &filename, const char *openmode="input", const char *extension="")
 Constructs a TextFile object and opens the specified file (including path).
 
 TextFile (const char *filename, const char *openmode, std::vector< QString > &lines, const int &maxLinesToReadWrite=0, const bool skipComments=true)
 Constructs a TextFile object and opens the specified file (including path), reads or writes file, leaves file open for further use of the rest of the methods.
 
 TextFile (const QString &filename, const char *openmode, std::vector< QString > &lines, const int &maxLinesToReadWrite=0, const bool skipComments=true)
 Constructs a TextFile object and opens the specified file (including path), reads or writes file, leaves file open for further use of the rest of the methods.
 
 TextFile (const char *filename, const char *openmode, QString *lines, const int &maxLinesToReadWrite, const bool skipComments=true)
 Constructs a TextFile object and opens the specified file (including path), reads or writes file, leaves file open for further use of the rest of the methods.
 
 TextFile (const QString &filename, const char *openmode, QString *lines, const int &maxLinesToReadWrite, const bool skipComments=true)
 Constructs a TextFile object and opens the specified file (including path), reads or writes file, leaves file open for further use of the rest of the methods.
 
 ~TextFile ()
 Closes file (if still open). Destroys the TextFile object.
 
void Open (const QString &filename, const char *openmode="input", const char *extension="")
 Opens a text file.
 
void Open (const char *filename, const char *openmode="input", const char *extension="")
 
bool OpenChk (bool bailIfNotOpen=false)
 
void Rewind ()
 Sets Read / Write pointer to begining of opened file.
 
void Close ()
 Closes file. Called automatically by TextFile destructor.
 
void GetFile (std::vector< QString > &lines, const int &maxLinesToRead=0, const bool skipComments=true)
 
void GetFile (QString *lines, const int &maxLinesToRead, const bool skipComments=true)
 
void PutFile (std::vector< QString > &lines, const int &maxLinesToWrite=0)
 
void PutFile (const QString *lines, const int &maxLinesToWrite)
 
bool GetLine (QString &line, const bool skipComments=true)
 Gets next line from file.
 
bool GetLineNoFilter (QString &line)
 Gets next NON-COMMENT line from file.
 
bool GetLine (const bool skipComments=true)
 Gets next line from file.
 
bool GetLineNoFilter ()
 Gets next NON-COMMENT line from file.
 
void PutLine (const QString &line)
 Writes string to file and appends a 'newline' string.
 
void PutLine (const char *line="")
 Writes char string to file and appends a 'newline' string.
 
void PutLineComment (const QString &line)
 Writes string to file, prepends a 'comment' string and appends a 'newline' string.
 
void PutLineComment (const char *line="")
 Writes char string to file, prepends a 'comment' string and appends a 'newline' string.
 
QString GetComment ()
 
QString GetNewLine ()
 
void SetComment (const char *commentString="#")
 Sets the 'comment' string.
 
void SetNewLine (const char *newLineString="\n")
 Sets the 'newline' string.
 
int LineCount (const int &maxLinesToRead=0)
 Counts number of lines in file.
 
std::streamsize Size ()
 Counts number of bytes in file.
 

Protected Member Functions

bool p_GetLine (QString &line, bool chkComment)
 Gets next line from file.
 

Protected Attributes

std::fstream p_stream
 File stream handle.
 
int p_openmode
 openmode of file: Input, Output, Overwrite, Append
 
QString p_filename
 FileName of the opened file.
 
QString p_commentString
 'comment' string used by GetLine and PutLineComment
 
QString p_newLineString
 'newline' string used by PutLine and PutLineComment
 

Detailed Description

Provides access to sequential ASCII stream I/O.

Provides access to sequential ASCII stream I/O. Checks for errors in opening, reading, and writing ASCII files. Can check for 'comment lines' while reading a text file.

Author
2003-08-29 Glenn Bennett
History

2005-02-16 Elizabeth Ribelin - Modified file to support Doxygen documentation

2010-03-30 Mackenzie Boyd - Modified open method append option to create the file if it doesn't exist, changed file existence check to use FileName class.

2011-08-08 Jeannie Backer - Modified unitTest to use $temporary variable instead of /tmp directory

Todo
2005-02-16 Glenn Bennett - add coded and implementation examples, and finish documentation

Definition at line 38 of file TextFile.h.

Constructor & Destructor Documentation

◆ TextFile() [1/6]

Isis::TextFile::TextFile ( )

Constructs an empty TextFile object.

Definition at line 22 of file TextFile.cpp.

Referenced by TextFile(), and TextFile().

◆ TextFile() [2/6]

Isis::TextFile::TextFile ( const QString & filename,
const char * openmode = "input",
const char * extension = "" )

Constructs a TextFile object and opens the specified file (including path).

Parameters
filenameFileName (including path) to be opened by TextFile object.
openmodeOpen Mode of file opened by TextFile object. InputOpens file for Input: Read Only, Fails if file does not exist OutputOpens file for Output: Read / Write - Creates file, Fails if file exists. OverwriteOpens file for Output: Read / Write - Creates file, Truncates if file exists. AppendOpens file for Append: Read / Write- Creates file, Appends if file exists. Defaults to "input"
extensionExtension to be added to filename (added only if not already on filename). Defaults to ""

Definition at line 41 of file TextFile.cpp.

References Open(), SetComment(), and SetNewLine().

◆ TextFile() [3/6]

Isis::TextFile::TextFile ( const char * filename,
const char * openmode,
std::vector< QString > & lines,
const int & maxLinesToReadWrite = 0,
const bool skipComments = true )

Constructs a TextFile object and opens the specified file (including path), reads or writes file, leaves file open for further use of the rest of the methods.

Parameters
filenameFileName (including path) to be opened by TextFile object.
openmodeOpen Mode of file opened by TextFile object. InputOpens file for Input: Read Only, Fails if file does not exist OutputOpens file for Output: Read / Write - Creates file, Fails if file exists. OverwriteOpens file for Output: Read / Write - Creates file, Truncates if file exists. AppendOpens file for Append: Read / Write - Creates file, Appends if file exists. Defaults to "input"
lines
maxLinesToReadWriteLimits the maximum lines read or written; 0=read entire file or write entire vectorDefaults to 0
skipCommentsControls whether comments lines are filtered; true=filter, false=return any line read from fileDefaults to true

Definition at line 109 of file TextFile.cpp.

References TextFile().

◆ TextFile() [4/6]

Isis::TextFile::TextFile ( const QString & filename,
const char * openmode,
std::vector< QString > & lines,
const int & maxLinesToReadWrite = 0,
const bool skipComments = true )

Constructs a TextFile object and opens the specified file (including path), reads or writes file, leaves file open for further use of the rest of the methods.

Parameters
filenameFileName (including path) to be opened by TextFile object.
openmodeOpen Mode of file opened by TextFile object. InputOpens file for Input: Read Only, Fails if file does not exist OutputOpens file for Output: Read / Write - Creates file, Fails if file exists. OverwriteOpens file for Output: Read / Write - Creates file, Truncates if file exists. AppendOpens file for Append: Read / Write - Creates file, Appends if file exists. Defaults to "input"
linesIString Vector to fill if reading or put to file if writing
maxLinesToReadWriteLimits the maximum lines read or written; 0=read entire file or write entire vectorDefaults to 0
skipCommentsControls whether comments lines are filtered; true=filter, false=return any line read from fileDefaults to true

Definition at line 72 of file TextFile.cpp.

References Open(), p_openmode, SetComment(), and SetNewLine().

◆ TextFile() [5/6]

Isis::TextFile::TextFile ( const char * filename,
const char * openmode,
QString * lines,
const int & maxLinesToReadWrite,
const bool skipComments = true )

Constructs a TextFile object and opens the specified file (including path), reads or writes file, leaves file open for further use of the rest of the methods.

Parameters
filenameFileName (including path) to be opened by TextFile object.
openmodeOpen Mode of file opened by TextFile object. InputOpens file for Input: Read Only, Fails if file does not exist OutputOpens file for Output: Read / Write - Creates file, Fails if file exists. OverwriteOpens file for Output: Read / Write - Creates file, Truncates if file exists. AppendOpens file for Append: Read / Write - Creates file, Appends if file exists. Defaults to "input"
lines
maxLinesToReadWriteLimits the maximum lines read or written; 0=read entire file or write entire vectorDefaults to 0
skipCommentsControls whether comments lines are filtered; true=filter, false=return any line read from fileDefaults to true

Definition at line 178 of file TextFile.cpp.

References TextFile().

◆ TextFile() [6/6]

Isis::TextFile::TextFile ( const QString & filename,
const char * openmode,
QString * lines,
const int & maxLinesToReadWrite,
const bool skipComments = true )

Constructs a TextFile object and opens the specified file (including path), reads or writes file, leaves file open for further use of the rest of the methods.

Parameters
filenameFileName (including path) to be opened by TextFile object.
openmodeOpen Mode of file opened by TextFile object. InputOpens file for Input: Read Only, Fails if file does not exist OutputOpens file for Output: Read / Write - Creates file, Fails if file exists. OverwriteOpens file for Output: Read / Write - Creates file, Truncates if file exists. AppendOpens file for Append: Read / Write - Creates file, Appends if file exists. Defaults to "input"
lines
maxLinesToReadWriteLimits the maximum lines read or written; 0=read entire file or write entire vectorDefaults to 0
skipCommentsControls whether comments lines are filtered; true=filter, false=return any line read from fileDefaults to true

Definition at line 140 of file TextFile.cpp.

References Open(), p_openmode, SetComment(), and SetNewLine().

◆ ~TextFile()

Isis::TextFile::~TextFile ( )

Closes file (if still open). Destroys the TextFile object.

Definition at line 186 of file TextFile.cpp.

References Close().

Member Function Documentation

◆ Close()

void Isis::TextFile::Close ( )

Closes file. Called automatically by TextFile destructor.

Definition at line 326 of file TextFile.cpp.

References p_stream.

Referenced by Isis::CisscalFile::~CisscalFile(), and ~TextFile().

◆ GetComment()

QString Isis::TextFile::GetComment ( )

Definition at line 564 of file TextFile.cpp.

◆ GetFile() [1/2]

void Isis::TextFile::GetFile ( QString * lines,
const int & maxLinesToRead,
const bool skipComments = true )

Definition at line 350 of file TextFile.cpp.

◆ GetFile() [2/2]

void Isis::TextFile::GetFile ( std::vector< QString > & lines,
const int & maxLinesToRead = 0,
const bool skipComments = true )

Definition at line 334 of file TextFile.cpp.

◆ GetLine() [1/2]

bool Isis::TextFile::GetLine ( const bool skipComments = true)

Gets next line from file.

Returns True if read a line, False if End Of File.

Parameters
skipCommentsControls whether comments lines are filtered; true=filter, false=return any line read from fileDefaults to true
Returns
bool

Definition at line 424 of file TextFile.cpp.

References p_GetLine().

◆ GetLine() [2/2]

bool Isis::TextFile::GetLine ( QString & line,
const bool skipComments = true )

Gets next line from file.

Returns True if read a line, False if End Of File.

Parameters
lineLine read from file, with newline removed.
skipCommentsControls whether comments lines are filtered; true=filter, false=return any line read from fileDefaults to true
Returns
bool

Definition at line 411 of file TextFile.cpp.

References p_GetLine().

◆ GetLineNoFilter() [1/2]

bool Isis::TextFile::GetLineNoFilter ( )

Gets next NON-COMMENT line from file.

Returns True if read a line, False if End Of File. See SetComment method.

Returns
bool

Definition at line 447 of file TextFile.cpp.

References p_GetLine().

◆ GetLineNoFilter() [2/2]

bool Isis::TextFile::GetLineNoFilter ( QString & line)

Gets next NON-COMMENT line from file.

Returns True if read a line, False if End Of File. See SetComment method.

Parameters
lineLine read from file, with newline removed.
Returns
bool

Definition at line 437 of file TextFile.cpp.

References p_GetLine().

◆ GetNewLine()

QString Isis::TextFile::GetNewLine ( )

Definition at line 582 of file TextFile.cpp.

◆ LineCount()

int Isis::TextFile::LineCount ( const int & maxLinesToRead = 0)

Counts number of lines in file.

Parameters
maxLinesToReadLimits lines counted - intended for large files. 0=count all lines. n=count up to and including n lines; if file longer than n lines, returns n+1. Defaults to 0
Returns
streamsize

Definition at line 607 of file TextFile.cpp.

References p_stream.

◆ Open()

void Isis::TextFile::Open ( const QString & filename,
const char * openmode = "input",
const char * extension = "" )

Opens a text file.

Parameters
filenameFileName (including path) to be opened.
openmodeOpen Mode of file to be opened. There are four options, "input", "output", "overwrite", and "append". "input" opens with in fstream option, "output" opens with in, out, and trunc fstream options and verifies the file does not exist, "overwrite" opens with in, out, and trunc fstream options, and "append" opens with in, out, and ate(at end) fstream options, "append" will open with in and out options if the file does not exist.
extensionExtension to be added to filename (added only if not already on filename). Defaults to ""
Exceptions
Isis::IException::Programmer
Isis::IException::Io- output file already exists
Isis::IException::Io- unable to open file

Definition at line 217 of file TextFile.cpp.

References Isis::IString::DownCase(), Isis::IException::Io, p_filename, p_openmode, p_stream, and Isis::IException::Programmer.

Referenced by Isis::UserInterface::loadBatchList(), TextFile(), TextFile(), and TextFile().

◆ OpenChk()

bool Isis::TextFile::OpenChk ( bool bailIfNotOpen = false)

Definition at line 301 of file TextFile.cpp.

◆ p_GetLine()

bool Isis::TextFile::p_GetLine ( QString & line,
bool chkComment )
protected

Gets next line from file.

Returns True if read a line, False if End Of File.

Parameters
lineLine read from file, with newline removed.
chkCommentTrue=Skip Comment Lines, False=Return All Lines.
Returns
bool
Exceptions
Isis::IException::Io- error reading text file

Definition at line 463 of file TextFile.cpp.

References Isis::IException::Io, p_commentString, p_filename, p_GetLine(), and p_stream.

Referenced by GetLine(), GetLine(), GetLineNoFilter(), GetLineNoFilter(), and p_GetLine().

◆ PutFile() [1/2]

void Isis::TextFile::PutFile ( const QString * lines,
const int & maxLinesToWrite )

Definition at line 383 of file TextFile.cpp.

◆ PutFile() [2/2]

void Isis::TextFile::PutFile ( std::vector< QString > & lines,
const int & maxLinesToWrite = 0 )

Definition at line 370 of file TextFile.cpp.

◆ PutLine() [1/2]

void Isis::TextFile::PutLine ( const char * line = "")

Writes char string to file and appends a 'newline' string.

See SetNewLine method.

Parameters
lineChar string to be written to file.Defaults to ""
Exceptions
Isis::IException::Io- error writing text to file
Isis::IException::Programmer- input is read only text file, cannot write to file

Definition at line 522 of file TextFile.cpp.

References Isis::IException::Io, p_filename, p_newLineString, p_openmode, p_stream, and Isis::IException::Programmer.

◆ PutLine() [2/2]

void Isis::TextFile::PutLine ( const QString & line)

Writes string to file and appends a 'newline' string.

See SetNewLine method.

Parameters
lineIString to be written to file. Defaults to ""

Definition at line 508 of file TextFile.cpp.

References PutLine().

Referenced by PutLine(), PutLineComment(), and PutLineComment().

◆ PutLineComment() [1/2]

void Isis::TextFile::PutLineComment ( const char * line = "")

Writes char string to file, prepends a 'comment' string and appends a 'newline' string.

See SetComment method and SetNewLine method.

Parameters
lineChar string to be written to file.Defaults to ""

Definition at line 559 of file TextFile.cpp.

References p_commentString, and PutLine().

◆ PutLineComment() [2/2]

void Isis::TextFile::PutLineComment ( const QString & line)

Writes string to file, prepends a 'comment' string and appends a 'newline' string.

See SetComment method and SetNewLine method.

Parameters
lineIString to be written to file.Defaults to ""

Definition at line 549 of file TextFile.cpp.

References p_commentString, and PutLine().

◆ Rewind()

void Isis::TextFile::Rewind ( )

Sets Read / Write pointer to begining of opened file.

Definition at line 317 of file TextFile.cpp.

References p_stream.

◆ SetComment()

void Isis::TextFile::SetComment ( const char * commentString = "#")

Sets the 'comment' string.

Default = '#' See ReadFilter method. Skips lines that begin with this string. See PutLineComment method. Prepends 'comment' string to output line.

Parameters
commentStringThe string of characters to be used for 'comment' lines. Defaults to "#"

Definition at line 577 of file TextFile.cpp.

References p_commentString.

Referenced by TextFile(), TextFile(), and TextFile().

◆ SetNewLine()

void Isis::TextFile::SetNewLine ( const char * newLineString = "\n")

Sets the 'newline' string.

Default = '
'. See PutLine and PutLineComment methods. Appends 'newline' string to output line.

Parameters
newLineStringThe string of characters to be used for 'comment' lines. Defaults to "\n"

Definition at line 593 of file TextFile.cpp.

References p_newLineString.

Referenced by TextFile(), TextFile(), and TextFile().

◆ Size()

streamsize Isis::TextFile::Size ( )

Counts number of bytes in file.

Returns
streamsize

Definition at line 652 of file TextFile.cpp.

References p_stream.

Member Data Documentation

◆ p_commentString

QString Isis::TextFile::p_commentString
protected

'comment' string used by GetLine and PutLineComment

Definition at line 45 of file TextFile.h.

Referenced by p_GetLine(), PutLineComment(), PutLineComment(), and SetComment().

◆ p_filename

QString Isis::TextFile::p_filename
protected

FileName of the opened file.

Definition at line 44 of file TextFile.h.

Referenced by Open(), Isis::CisscalFile::p_GetLine(), p_GetLine(), and PutLine().

◆ p_newLineString

QString Isis::TextFile::p_newLineString
protected

'newline' string used by PutLine and PutLineComment

Definition at line 47 of file TextFile.h.

Referenced by PutLine(), and SetNewLine().

◆ p_openmode

int Isis::TextFile::p_openmode
protected

openmode of file: Input, Output, Overwrite, Append

Definition at line 42 of file TextFile.h.

Referenced by Open(), PutLine(), TextFile(), and TextFile().

◆ p_stream

std::fstream Isis::TextFile::p_stream
protected

File stream handle.

Definition at line 41 of file TextFile.h.

Referenced by Close(), LineCount(), Open(), Isis::CisscalFile::p_GetLine(), p_GetLine(), PutLine(), Rewind(), and Size().


The documentation for this class was generated from the following files: