Pretty Diagnostics
Create your own pretty diagnostics
Loading...
Searching...
No Matches
pretty_diagnostics::FileSource Class Referencefinal

A Source implementation that reads from a file on disk. More...

#include <source.hpp>

Inheritance diagram for pretty_diagnostics::FileSource:
[legend]

Public Member Functions

 FileSource (std::filesystem::path path)
 Creates a file source from a filesystem path. More...
 
void set_working_path (const std::filesystem::path &path)
 Sets a working directory used to relativize the displayed path. More...
 
Location from_coords (size_t row, size_t column) const override
 Maps (row, column) to a Location within the file. More...
 
Location from_index (size_t index) const override
 Maps an absolute index to a Location within the file. More...
 
std::string substr (const Location &start, const Location &end) const override
 Extracts a substring delimited by two locations. More...
 
std::string line (const Location &location) const override
 Returns the contents of the line containing the given location. More...
 
std::string line (size_t line_number) const override
 Returns the contents of the specified line number. More...
 
size_t line_count () const override
 Returns the number of lines in the file. More...
 
std::string contents () const override
 Returns the entire file contents. More...
 
std::string path () const override
 Returns the file system path to the file (possibly working path relative) More...
 
size_t size () const override
 Returns the total size of the file in characters. More...
 
- Public Member Functions inherited from pretty_diagnostics::Source
virtual ~Source ()=default
 
virtual Location from_coords (size_t row, size_t column) const =0
 Returns a location corresponding to the given row and column. More...
 
virtual Location from_index (size_t index) const =0
 Returns a location for the given absolute character index. More...
 
virtual std::string substr (const Location &start, const Location &end) const =0
 Returns the substring between two locations. More...
 
virtual std::string line (const Location &location) const =0
 Returns the full line at the given location. More...
 
virtual std::string line (size_t line_number) const =0
 Returns the contents of the specified line number. More...
 
virtual size_t line_count () const =0
 Returns the total number of lines in the source. More...
 
virtual std::string contents () const =0
 Returns the entire contents of the source. More...
 
virtual std::string path () const =0
 Returns a displayable path or identifier of the source. More...
 
virtual size_t size () const =0
 Returns the total size (in characters) of the source. More...
 

Friends

bool operator== (const FileSource &lhs, const FileSource &rhs)
 Equality compares path. More...
 
bool operator!= (const FileSource &lhs, const FileSource &rhs)
 Inequality based on operator== More...
 

Detailed Description

A Source implementation that reads from a file on disk.

Constructor & Destructor Documentation

◆ FileSource()

pretty_diagnostics::FileSource::FileSource ( std::filesystem::path  path)
explicit

Creates a file source from a filesystem path.

Parameters
pathPath to the file on disk (absolute or relative)

Member Function Documentation

◆ contents()

std::string pretty_diagnostics::FileSource::contents ( ) const
overridevirtual

Returns the entire file contents.

Returns
Full file contents

Implements pretty_diagnostics::Source.

◆ from_coords()

Location pretty_diagnostics::FileSource::from_coords ( size_t  row,
size_t  column 
) const
overridevirtual

Maps (row, column) to a Location within the file.

Parameters
row0-based line number
column0-based column number
Returns
Location corresponding to the given coordinates

Implements pretty_diagnostics::Source.

◆ from_index()

Location pretty_diagnostics::FileSource::from_index ( size_t  index) const
overridevirtual

Maps an absolute index to a Location within the file.

Parameters
index0-based absolute character index
Returns
Location corresponding to the given index

Implements pretty_diagnostics::Source.

◆ line() [1/2]

std::string pretty_diagnostics::FileSource::line ( const Location location) const
overridevirtual

Returns the contents of the line containing the given location.

Parameters
locationA location within the desired line
Returns
The entire line contents without a trailing newline

Implements pretty_diagnostics::Source.

◆ line() [2/2]

std::string pretty_diagnostics::FileSource::line ( size_t  line_number) const
overridevirtual

Returns the contents of the specified line number.

Parameters
line_number1-based line number
Returns
The entire line contents without a trailing newline

Implements pretty_diagnostics::Source.

◆ line_count()

size_t pretty_diagnostics::FileSource::line_count ( ) const
overridevirtual

Returns the number of lines in the file.

Returns
Line count

Implements pretty_diagnostics::Source.

◆ path()

std::string pretty_diagnostics::FileSource::path ( ) const
overridevirtual

Returns the file system path to the file (possibly working path relative)

Returns
Display path string

Implements pretty_diagnostics::Source.

◆ set_working_path()

void pretty_diagnostics::FileSource::set_working_path ( const std::filesystem::path &  path)

Sets a working directory used to relativize the displayed path.

Parameters
pathWorking directory used to make the file path relative for display

◆ size()

size_t pretty_diagnostics::FileSource::size ( ) const
overridevirtual

Returns the total size of the file in characters.

Returns
Size in characters

Implements pretty_diagnostics::Source.

◆ substr()

std::string pretty_diagnostics::FileSource::substr ( const Location start,
const Location end 
) const
overridevirtual

Extracts a substring delimited by two locations.

Parameters
startInclusive start location
endExclusive end location
Returns
Substring between start and end

Implements pretty_diagnostics::Source.

Friends And Related Function Documentation

◆ operator!=

bool operator!= ( const FileSource lhs,
const FileSource rhs 
)
friend

Inequality based on operator==

Parameters
lhsLeft-hand file source
rhsRight-hand file source
Returns
True if the paths differ

◆ operator==

bool operator== ( const FileSource lhs,
const FileSource rhs 
)
friend

Equality compares path.

Parameters
lhsLeft-hand file source
rhsRight-hand file source
Returns
True if paths are equal

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