Empirical
Public Member Functions | Protected Attributes | List of all members
emp::File Class Reference

A class to maintin files for loading, writing, storing, and easy access to components. More...

#include <File.h>

Public Member Functions

 File ()
 
 File (std::istream &input)
 
 File (const std::string &filename)
 
 File (const File &)=default
 
 File (File &&)=default
 
 ~File ()
 
Fileoperator= (const File &)=default
 
Fileoperator= (File &&)=default
 
auto begin () const
 Return const iterator to beginning of file. More...
 
auto end () const
 Return const iterator to end of file. More...
 
auto begin ()
 Return iterator to beginning of file. More...
 
auto end ()
 Return iterator to end of file. More...
 
size_t GetNumLines () const
 How many lines are in this file? More...
 
size_t size () const
 Compatibility with size() More...
 
std::string & operator[] (size_t pos)
 Index into a specific line in this file. More...
 
const std::string & operator[] (size_t pos) const
 Const index into a specific line in this file. More...
 
std::string & front ()
 Return the first line in the file. More...
 
const std::string & front () const
 Return a const reference to to the first line in the file. More...
 
std::string & back ()
 Return the last line in the file. More...
 
const std::string & back () const
 Return a const reference to the last line in the file. More...
 
FileAppend (const std::string &line)
 Append a new line to the end of the file. More...
 
FileAppend (const emp::vector< std::string > &in_lines)
 Append a vector of lines to the end of the file. More...
 
FileAppend (const File &in_file)
 Join two files. More...
 
template<typename T >
Fileoperator+= (T &&in)
 Append to the end of a file. More...
 
template<typename T >
auto operator<< (T &&in)
 
auto operator>> (std::string &out)
 Extract first line from file. More...
 
bool operator== (const File in)
 Test if two files are identical. More...
 
bool operator!= (const File in)
 Test if two files are different. More...
 
FileLoadLine (std::istream &input)
 Load a line from an input stream into a file. More...
 
FileLoad (std::istream &input)
 Load an entire input stream into a file. More...
 
FileLoad (const std::string &filename)
 
FileWrite (std::ostream &output)
 Write this file to a provided output stream. More...
 
FileWrite (const std::string &filename)
 Write this file to a file of the provided name. More...
 
std::set< std::string > AsSet () const
 Convert this file into an std::set of lines (loses line ordering). More...
 
FileApply (const std::function< void(std::string &)> &fun)
 Apply a string manipulation function to all lines in the file. More...
 
FileKeepIf (const std::function< bool(const std::string &)> &fun)
 Purge functions that don't meet a certain criterion. More...
 
FileRemoveEmpty ()
 Remove all lines that are empty strings. More...
 
FileCompressWhitespace ()
 
FileRemoveWhitespace (bool keep_newlines=true)
 Delete all whitespace; by default keep newlines. More...
 
FileRemoveComments (const std::string &marker)
 A technique to remove all comments in a file. More...
 
template<typename T >
emp::vector< T > Process (const std::function< T(std::string &)> &fun)
 
emp::vector< std::string > ExtractCol (char delim=',')
 
template<typename T >
emp::vector< T > ExtractColAs (char delim=',')
 

Protected Attributes

emp::vector< std::string > lines
 

Detailed Description

A class to maintin files for loading, writing, storing, and easy access to components.

Constructor & Destructor Documentation

emp::File::File ( )
inline
emp::File::File ( std::istream &  input)
inline
emp::File::File ( const std::string &  filename)
inline
emp::File::File ( const File )
default
emp::File::File ( File &&  )
default
emp::File::~File ( )
inline

Member Function Documentation

File& emp::File::Append ( const std::string &  line)
inline

Append a new line to the end of the file.

File& emp::File::Append ( const emp::vector< std::string > &  in_lines)
inline

Append a vector of lines to the end of the file.

File& emp::File::Append ( const File in_file)
inline

Join two files.

File& emp::File::Apply ( const std::function< void(std::string &)> &  fun)
inline

Apply a string manipulation function to all lines in the file.

std::set<std::string> emp::File::AsSet ( ) const
inline

Convert this file into an std::set of lines (loses line ordering).

std::string& emp::File::back ( )
inline

Return the last line in the file.

const std::string& emp::File::back ( ) const
inline

Return a const reference to the last line in the file.

auto emp::File::begin ( ) const
inline

Return const iterator to beginning of file.

auto emp::File::begin ( )
inline

Return iterator to beginning of file.

File& emp::File::CompressWhitespace ( )
inline

Any time multiple whitespaces are next to each other, collapse to a single WS char. Prefer '
' if in whitespace collapsed, otherwise use ' '.

auto emp::File::end ( ) const
inline

Return const iterator to end of file.

auto emp::File::end ( )
inline

Return iterator to end of file.

emp::vector<std::string> emp::File::ExtractCol ( char  delim = ',')
inline
template<typename T >
emp::vector<T> emp::File::ExtractColAs ( char  delim = ',')
inline
std::string& emp::File::front ( )
inline

Return the first line in the file.

const std::string& emp::File::front ( ) const
inline

Return a const reference to to the first line in the file.

size_t emp::File::GetNumLines ( ) const
inline

How many lines are in this file?

File& emp::File::KeepIf ( const std::function< bool(const std::string &)> &  fun)
inline

Purge functions that don't meet a certain criterion.

File& emp::File::Load ( std::istream &  input)
inline

Load an entire input stream into a file.

File& emp::File::Load ( const std::string &  filename)
inline

Load a file from disk using the provided name. If file does not exist, this is a nop

File& emp::File::LoadLine ( std::istream &  input)
inline

Load a line from an input stream into a file.

bool emp::File::operator!= ( const File  in)
inline

Test if two files are different.

template<typename T >
File& emp::File::operator+= ( T &&  in)
inline

Append to the end of a file.

template<typename T >
auto emp::File::operator<< ( T &&  in)
inline

Insert formatted data into file This is exactly the same as operator+=

File& emp::File::operator= ( const File )
default
File& emp::File::operator= ( File &&  )
default
bool emp::File::operator== ( const File  in)
inline

Test if two files are identical.

auto emp::File::operator>> ( std::string &  out)
inline

Extract first line from file.

std::string& emp::File::operator[] ( size_t  pos)
inline

Index into a specific line in this file.

const std::string& emp::File::operator[] ( size_t  pos) const
inline

Const index into a specific line in this file.

template<typename T >
emp::vector<T> emp::File::Process ( const std::function< T(std::string &)> &  fun)
inline

Run a function on each line of a file and return the restults as a vector. Note: Function is allowed to modify string.

File& emp::File::RemoveComments ( const std::string &  marker)
inline

A technique to remove all comments in a file.

File& emp::File::RemoveEmpty ( )
inline

Remove all lines that are empty strings.

File& emp::File::RemoveWhitespace ( bool  keep_newlines = true)
inline

Delete all whitespace; by default keep newlines.

size_t emp::File::size ( ) const
inline

Compatibility with size()

File& emp::File::Write ( std::ostream &  output)
inline

Write this file to a provided output stream.

File& emp::File::Write ( const std::string &  filename)
inline

Write this file to a file of the provided name.

Member Data Documentation

emp::vector<std::string> emp::File::lines
protected

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