OpenMS
Loading...
Searching...
No Matches
TextFile Class Reference

#include <OpenMS/FORMAT/TextFile.h>

Inheritance diagram for TextFile:
[legend]
Collaboration diagram for TextFile:
[legend]

Type definitions

typedef std::vector< String >::iterator Iterator
 Mutable iterator.
 
typedef std::vector< String >::const_iterator ConstIterator
 Non-mutable iterator.
 
typedef std::vector< String >::reverse_iterator ReverseIterator
 Mutable reverse iterator.
 
typedef std::vector< String >::const_reverse_iterator ConstReverseIterator
 Non-mutable reverse iterator.
 
std::vector< Stringbuffer_
 Internal buffer storing the lines before writing them to the file.
 
 TextFile ()
 Default constructor.
 
virtual ~TextFile ()
 destructor
 
 TextFile (const String &filename, bool trim_lines=false, Int first_n=-1, bool skip_empty_lines=false, const String &comment_symbol="")
 Constructor with filename.
 
void load (const String &filename, bool trim_lines=false, Int first_n=-1, bool skip_empty_lines=false, const String &comment_symbol="")
 Loads data from a text file into the internal buffer.
 
void store (const String &filename)
 Writes the data to a file.
 
template<typename StringType >
TextFileoperator<< (const StringType &string)
 Operator for appending entries with less code.
 
template<typename StringType >
void addLine (const StringType &line)
 
ConstIterator begin () const
 Gives access to the underlying text buffer.
 
Iterator begin ()
 
ConstIterator end () const
 Gives access to the underlying text buffer.
 
Iterator end ()
 
static std::istream & getLine (std::istream &is, std::string &t)
 Platform-agnostic getline() which can deal with all line endings (\r, \r\n, \n)
 

Detailed Description

@brief This class provides some basic file handling methods for text files.

Member Typedef Documentation

◆ ConstIterator

typedef std::vector<String>::const_iterator ConstIterator

Non-mutable iterator.

◆ ConstReverseIterator

typedef std::vector<String>::const_reverse_iterator ConstReverseIterator

Non-mutable reverse iterator.

◆ Iterator

typedef std::vector<String>::iterator Iterator

Mutable iterator.

◆ ReverseIterator

typedef std::vector<String>::reverse_iterator ReverseIterator

Mutable reverse iterator.

Constructor & Destructor Documentation

◆ TextFile() [1/2]

TextFile ( )

Default constructor.

◆ ~TextFile()

virtual ~TextFile ( )
virtual

destructor

◆ TextFile() [2/2]

TextFile ( const String filename,
bool  trim_lines = false,
Int  first_n = -1,
bool  skip_empty_lines = false,
const String comment_symbol = "" 
)

Constructor with filename.

Parameters
[in]filenameThe input file name
[in]trim_linesWhether or not the lines are trimmed when reading them from file
[in]first_nIf set, only first_n lines the lines from the beginning of the file are read
[in]skip_empty_linesShould empty lines be skipped? If used in conjunction with trim_lines, also lines with only whitespace will be skipped. Skipped lines do not count towards the total number of read lines.
[in]comment_symbolLines prefixed with this string are skipped. Comment lines do not count towards the total number of read lines.
Exceptions
Exception::FileNotFoundis thrown if the file could not be opened.

Member Function Documentation

◆ addLine()

template<typename StringType >
void addLine ( const StringType &  line)
inline

◆ begin() [1/2]

Iterator begin ( )

◆ begin() [2/2]

ConstIterator begin ( ) const

Gives access to the underlying text buffer.

Referenced by MsInspectFile::load(), and SpecArrayFile::load().

◆ end() [1/2]

Iterator end ( )

◆ end() [2/2]

ConstIterator end ( ) const

Gives access to the underlying text buffer.

Referenced by MsInspectFile::load(), and SpecArrayFile::load().

◆ getLine()

static std::istream & getLine ( std::istream &  is,
std::string &  t 
)
static

Platform-agnostic getline() which can deal with all line endings (\r, \r\n, \n)

Line endings will be removed from the resulting string.

◆ load()

void load ( const String filename,
bool  trim_lines = false,
Int  first_n = -1,
bool  skip_empty_lines = false,
const String comment_symbol = "" 
)

Loads data from a text file into the internal buffer.

Retrieve the data using begin() and end().

Parameters
[in]filenameThe input file name
[in]trim_linesWhether or not the lines are trimmed when reading them from file
[in]first_nIf set, only first_n lines the lines from the beginning of the file are read
[in]skip_empty_linesShould empty lines be skipped? If used in conjunction with trim_lines, also lines with only whitespace will be skipped. Skipped lines do not count towards the total number of read lines.
[in]comment_symbolLines prefixed with this string are skipped. Comment lines do not count towards the total number of read lines.
Exceptions
Exception::FileNotFoundis thrown if the file could not be opened.

◆ operator<<()

template<typename StringType >
TextFile & operator<< ( const StringType &  string)
inline

Operator for appending entries with less code.

◆ store()

void store ( const String filename)

Writes the data to a file.

Parameters
[in]filenameThe output file name
Note
This function uses platform-dependent line breaks
Exceptions
Exception::UnableToCreateFileis thrown if the file could not be created

Member Data Documentation

◆ buffer_

std::vector<String> buffer_
protected

Internal buffer storing the lines before writing them to the file.