File.hpp

The File object maintains a simple, in-memory file.

Todo:

We need to modify this code so that File can work with Emscripten.

Note

Status: BETA

class File
#include <File.hpp>

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

Public Functions

inline File()
inline File(std::istream &input)
inline File(const String &filename)
File(const File&) = default
File(File&&) = default
inline ~File()
File &operator=(const File&) = default
File &operator=(File&&) = default
inline auto begin() const

Return const iterator to beginning of file.

inline auto end() const

Return const iterator to end of file.

inline auto begin()

Return iterator to beginning of file.

inline auto end()

Return iterator to end of file.

inline size_t GetNumLines() const

How many lines are in this file?

inline size_t size() const

Compatibility with size()

inline vector<String> GetAllLines()

Return entire text of the file.

inline String &operator[](size_t pos)

Index into a specific line in this file.

inline const String &operator[](size_t pos) const

Const index into a specific line in this file.

inline String &front()

Return the first line in the file.

inline const String &front() const

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

inline String &back()

Return the last line in the file.

inline const String &back() const

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

inline bool HasError() const
inline const String &GetError() const
inline void ClearError()
inline File &Append(const String &line)

Append a new line to the end of the file.

template<typename T>
inline File &Append(const vector<T> &in_lines)

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

inline File &Append(const File &in_file)

Join two files.

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

Append to the end of a file.

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

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

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

Extract first line from file.

inline bool operator==(const File &in) const

Test if two files are identical.

inline bool operator!=(const File &in) const

Test if two files are different.

inline bool LoadLine(std::istream &input)

Load a line from an input stream into a file; return whether load was successful.

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

Load an entire input stream into a file.

inline File &Load(const String &filename)

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

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

Write this file to a provided output stream.

inline File &Write(const String &filename)

Write this file to a file of the provided name.

inline bool Contains(const String &pattern) const

Test if a substring exists on ANY line of a file.

inline std::set<String> AsSet() const

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

template<typename FUN_T>
inline File &Apply(FUN_T fun)

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

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

Purge all lines that don’t the criterion function.

inline File &KeepIfContains(const String &pattern)

Keep only strings that contain a specific substring.

inline File &RemoveIfContains(const String &pattern)

Remove all strings that contain a specific substring.

inline File &KeepIfBegins(const String &prefix)

Keep only strings that contain a specific substring.

inline File &RemoveIfBegins(const String &prefix)

Remove all strings that contain a specific substring.

inline File &RemoveEmpty()

Remove all lines that are empty strings.

inline File &CompressWhitespace()

Any time multiple whitespaces are next to each other, collapse to a single WS char. Prefer ‘

’ if in whitespace collapsed, otherwise use ‘ ‘.

inline File &RemoveWhitespace(bool keep_newlines = true)

Delete all whitespace; by default keep newlines.

inline File &RemoveComments(const String &marker, bool skip_quotes = true)

A technique to remove all comments in a file.

inline File &RemoveComments(char marker, bool skip_quotes = true)

Allow remove comments to also be specified with a single character.

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

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

inline vector<String> Read(size_t start, size_t end) const

Get a series of lines.

inline vector<String> ReadUntil(size_t start, auto test_fun) const

Get a series of lines until a line meets a certain condition.

inline vector<String> ReadWhile(size_t start, auto test_fun) const

Get a series of lines while lines continue to meet a certain condition.

inline vector<String> ExtractCol(char delim = ',')

Remove the first column from the file, returning it as a vector of strings.

template<typename T>
inline vector<T> ExtractColAs(char delim = ',')

Remove the first column from the file, returning it as a vector of a specified type.

inline vector<std::string_view> ViewRowSlices(size_t row_id, String delim = ",")

Convert a row of a file to a vector of string views.

inline vector<String> ExtractRow(String delim = ",")

Remove the first row from the file, returning it as a vector of strings.

template<typename T>
inline vector<T> ExtractRowAs(String delim = ",")

Remove the first row from the file, returning it as a vector of a specified type.

inline vector<vector<String>> ToCSV(String delim = ",") const
template<typename T>
inline vector<vector<T>> ToData(String delim = ",") const
inline Scan StartScan(size_t start = 0) const

Protected Attributes

vector<String> lines
String file_error = ""
class Scan
#include <File.hpp>

Public Functions

inline Scan(const File &in, size_t start = 0)
Scan(const Scan &in) = default
inline const File &GetFile() const
inline size_t GetLine() const
inline bool AtStart() const
inline bool AtEnd() const
inline operator bool() const
inline void Set(size_t in_line)
inline void Reset()
inline void SetEnd()
inline const String &Read()
inline vector<String> ReadTo(size_t end)
inline vector<String> ReadUntil(auto test_fun)
inline vector<String> ReadWhile(auto test_fun)

Private Members

const File &file
size_t line = 0