STORMM Source Documentation
Loading...
Searching...
No Matches
stormm::testing::TextFile Class Reference

Structure for translating a text file into a compact, rapidly parsable vector of characters in CPU RAM. The struct contains two nested struct definitions, for a Reader and a Writer. Private objects of these structs can then be accessed with eponymous data() getter functions, returning the appropriate kind of access depending on the const-ness of the TextFile object itself. More...

#include <textfile.h>

Public Member Functions

 ~TextFile ()=default
 Default destructor.
 
std::string getFileName () const
 Get the name of the original file.
 
int getLineCount () const
 Get the line count of a text file after converting it to a character vector in memory.
 
int getLineLimits (int index) const
 Get one of the line limits of a text file converted to a character vector in memory.
 
int getLineLength (int index) const
 Get the length of a line. A bounds check will be applied to the line index number.
 
int getLongestLineLength () const
 Get the length of the longest line in the file.
 
size_t getTextSize () const
 Get the number of text characters in the object's buffer (this will not count implicit carriage returns between lines).
 
char getChar (int index) const
 Get one character of a text file after converting it to a character vector in memory.
 
std::string getLineAsString (int line_index) const
 Get a line from the file as a string.
 
const char * getTextPointer (int index) const
 Get a char pointer to a specific index in the object.
 
const char * getLinePointer (int line_index) const
 Get a char pointer to a specific line in the object.
 
const TextFileReader data () const
 Get an abstract of a text file's CPU-RAM representation, for ease of use.
 
std::string extractString (int line_number, int start_pos=0, int string_length=-1) const
 Extract a string based on a line number, starting position, and length.
 
char4 extractChar4 (int line_number, int start_pos, int string_length) const
 Extract a char4 tuple based on a line number, starting position, and length.
 
std::string toString (TextEnds line_endings=TextEnds::NEWLINE) const
 Convert all content to a string, with the option of making line endings carriage returns or simple spaces (fused line endings, which could create combined words out of the last word on one line and the first word on another, are not accepted).
 
 TextFile ()
 Constructor for taking an ascii file or a very long, formatted string and transforming it into a std::vector of characters with line limits recorded.
 
 TextFile (const std::string &file_name, TextOrigin source=TextOrigin::DISK, const std::string &content=std::string(""), const std::string &caller=std::string(""))
 
 TextFile (const char *content, const size_t length, const std::string &caller=std::string(""))
 
 TextFile (const TextFile &original)=default
 The default copy and move constructors as well as assignment operators are acceptable.
 
 TextFile (TextFile &&original)=default
 
TextFileoperator= (const TextFile &other)=default
 
TextFileoperator= (TextFile &&other)=default
 
void write (std::ofstream *foutp) const
 Write the contents of the object to disk.
 
void write (const std::string &new_filename, PrintSituation expectation=PrintSituation::OPEN_NEW) const
 
void write (PrintSituation expectation=PrintSituation::OPEN_NEW) const
 

Detailed Description

Structure for translating a text file into a compact, rapidly parsable vector of characters in CPU RAM. The struct contains two nested struct definitions, for a Reader and a Writer. Private objects of these structs can then be accessed with eponymous data() getter functions, returning the appropriate kind of access depending on the const-ness of the TextFile object itself.

Constructor & Destructor Documentation

◆ TextFile()

stormm::parse::TextFile::TextFile ( )

Constructor for taking an ascii file or a very long, formatted string and transforming it into a std::vector of characters with line limits recorded.

Overloaded:

  • Construct an empty object after taking no arguments
  • Construct a complete object from a named file (throws an exception if the file does not exist)
Parameters
file_nameName of the input file
sourceOrigin of the text–disk or RAM
contentContent for the TextFile, and perhaps later an ASCII text file, to hold
caller(Optional) name of the calling function

Member Function Documentation

◆ extractChar4()

char4 stormm::parse::TextFile::extractChar4 ( int line_number,
int start_pos,
int string_length ) const

Extract a char4 tuple based on a line number, starting position, and length.

Parameters
line_numberThe line number where the text shall be found
start_posStarting position on the line to begin reading (defaults to the start of the line)
string_lengthLength of the string to extract. The default value of 4 will try to read four characters, but shorter values will leave blank space at the end of the tuple.

◆ extractString()

std::string stormm::parse::TextFile::extractString ( int line_number,
int start_pos = 0,
int string_length = -1 ) const

Extract a string based on a line number, starting position, and length.

Parameters
line_numberThe line number where the text shall be found
start_posStarting position on the line to begin reading (defaults to the start of the line)
string_lengthLength of the string to extract. A value of -1 indicates that reading shall continue until the end of the line.

◆ getChar()

char stormm::parse::TextFile::getChar ( int index) const

Get one character of a text file after converting it to a character vector in memory.

Parameters
indexThe character index, as ascertained by line limits and some offset

◆ getLineAsString()

std::string stormm::parse::TextFile::getLineAsString ( int line_index) const

Get a line from the file as a string.

Parameters
line_indexThe line of interest, indexing starting from zero

◆ getLineLength()

int stormm::parse::TextFile::getLineLength ( int index) const

Get the length of a line. A bounds check will be applied to the line index number.

Parameters
indexThe line index. The array of limits contains one more indices than the text file has lines, to allow the end of the last line to be determined.

◆ getLineLimits()

int stormm::parse::TextFile::getLineLimits ( int index) const

Get one of the line limits of a text file converted to a character vector in memory.

Parameters
indexThe line index. The array of limits contains one more indices than the text file has lines, to allow the end of the last line to be determined.

◆ getLinePointer()

const char * stormm::parse::TextFile::getLinePointer ( int line_index) const

Get a char pointer to a specific line in the object.

Parameters
line_indexThe line of interest, indexing starting from zero

◆ getTextPointer()

const char * stormm::parse::TextFile::getTextPointer ( int index) const

Get a char pointer to a specific index in the object.

Parameters
indexThe character index, as ascertained by line limits and some offset

◆ toString()

std::string stormm::parse::TextFile::toString ( TextEnds line_endings = TextEnds::NEWLINE) const

Convert all content to a string, with the option of making line endings carriage returns or simple spaces (fused line endings, which could create combined words out of the last word on one line and the first word on another, are not accepted).

Parameters
line_endingsSpecify whether to insert newlines or white space between lines

◆ write()

void stormm::parse::TextFile::write ( std::ofstream * foutp) const

Write the contents of the object to disk.

Overloaded:

  • Supply an open file pointer
  • Supply a new file name, distinct from the original name stored in the object, and a file status expectation
  • Supply just a file status expectation and (overwrite, or perhaps append in some clever implementation) the original file
Parameters
foutpOpen output stream
new_filenameName of a distinct file to write
expectationExpected state of the output file

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