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()
-
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 bool HasError() const
-
inline void ClearError()
-
template<typename T>
inline File &Append(const vector<T> &in_lines) Append a vector of lines to the end of the file.
-
template<typename T>
inline auto operator<<(T &&in) Insert formatted data into file This is exactly the same as operator+=
-
inline bool LoadLine(std::istream &input)
Load a line from an input stream into a file; return whether load was successful.
-
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 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 &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> 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.
-
class Scan
- #include <File.hpp>
-
inline File()