PLSSVM - Parallel Least Squares Support Vector Machine  2.0.0
A Least Squares Support Vector Machine implementation using different backends.
Public Member Functions | Private Member Functions | Private Attributes | List of all members
plssvm::detail::io::file_reader Class Reference

The plssvm::detail::file_reader class is responsible for reading a file and splitting it into its lines. More...

#include <file_reader.hpp>

Public Member Functions

 file_reader ()=default
 Creates a new file_reader without associating it to a file.
 
 file_reader (const char *filename)
 Create a new file_reader and associate it to the filename by opening it (possibly memory mapping it). More...
 
 file_reader (const std::string &filename)
 Create a new file_reader and associate it to the filename by opening it (possibly memory mapping it). More...
 
 file_reader (const std::filesystem::path &filename)
 Create a new file_reader and associate it to the filename by opening it (possibly memory mapping it). More...
 
 ~file_reader ()
 Closes the associated file. More...
 
 file_reader (const file_reader &)=delete
 Delete the copy-constructor since file_reader is move-only.
 
 file_reader (file_reader &&other) noexcept
 Implement the move-constructor since file_reader is move-only. More...
 
file_readeroperator= (const file_reader &)=delete
 Delete the copy-assignment operator since file_reader is move-only. More...
 
file_readeroperator= (file_reader &&other) noexcept
 Implement the move-assignment operator since file_reader is move-only. More...
 
void open (const char *filename)
 Associates the current file_reader with the file denoted by filename, i.e., opens the file filename (possible memory mapping it). More...
 
void open (const std::string &filename)
 Associates the current file_reader with the file denoted by filename, i.e., opens the file filename (possible memory mapping it). More...
 
void open (const std::filesystem::path &filename)
 Associates the current file_reader with the file denoted by filename, i.e., opens the file filename (possible memory mapping it). More...
 
bool is_open () const noexcept
 Checks whether this file_reader is currently associated with a file. More...
 
void close ()
 Closes the associated file. More...
 
void swap (file_reader &other)
 Element-wise swap all contents of *this with other. More...
 
const std::vector< std::string_view > & read_lines (std::string_view comment={ "\n" })
 Read the content of the associated file and split it into lines, ignoring empty lines and lines starting with the comment. More...
 
const std::vector< std::string_view > & read_lines (char comment)
 Read the content of the associated file and split it into lines, ignoring empty lines and lines starting with the comment. More...
 
std::vector< std::string_view >::size_type num_lines () const noexcept
 Return the number of parsed lines (where all empty lines or lines starting with a comment are ignored). More...
 
std::string_view line (typename std::vector< std::string_view >::size_type pos) const
 Return the pos line of the parsed file. More...
 
const std::vector< std::string_view > & lines () const noexcept
 Return all lines present after the preprocessing. More...
 
const char * buffer () const noexcept
 Return the underlying file content as one large string. More...
 

Private Member Functions

void open_memory_mapped_file_unix (const char *filename)
 Try to open the file filename and "read" its content using memory mapped IO on UNIX systems. More...
 
void open_memory_mapped_file_windows (const char *filename)
 Try to open the file filename and "read" its content using memory mapped IO on Windows systems. More...
 
void open_file (const char *filename)
 Read open the file and read its content in one buffer using a normal std::ifstream. More...
 

Private Attributes

char * file_content_ { nullptr }
 The content of the file. Pointer to the memory mapped area or to a separately allocated memory area holding the file's content. If the file is empty, corresponds to a nullptr!
 
std::streamsize num_bytes_ { 0 }
 The number of bytes stored in file_content_.
 
std::vector< std::string_view > lines_ {}
 The parsed content of file_content_: a vector of all lines that are not empty and do not start with the provided comment.
 
bool is_open_ { false }
 true if a file is currently associated wih this file_reader, false otherwise.
 

Detailed Description

The plssvm::detail::file_reader class is responsible for reading a file and splitting it into its lines.

If the necessary headers are present, the class tries to memory map the given file. If this fails or if the headers are not present, the file is read as one blob using std::ifstream::read.

Constructor & Destructor Documentation

◆ file_reader() [1/4]

plssvm::detail::io::file_reader::file_reader ( const char *  filename)
explicit

Create a new file_reader and associate it to the filename by opening it (possibly memory mapping it).

Parameters
[in]filenamethe file to open
Exceptions
plssvm::file_not_found_exceptionif the filename couldn't be found

◆ file_reader() [2/4]

plssvm::detail::io::file_reader::file_reader ( const std::string &  filename)
explicit

Create a new file_reader and associate it to the filename by opening it (possibly memory mapping it).

Parameters
[in]filenamethe file to open
Exceptions
plssvm::file_not_found_exceptionif the filename couldn't be found

◆ file_reader() [3/4]

plssvm::detail::io::file_reader::file_reader ( const std::filesystem::path &  filename)
explicit

Create a new file_reader and associate it to the filename by opening it (possibly memory mapping it).

Parameters
[in]filenamethe file to open
Exceptions
plssvm::file_not_found_exceptionif the filename couldn't be found

◆ ~file_reader()

plssvm::detail::io::file_reader::~file_reader ( )

Closes the associated file.

If memory mapped IO has been used, unmap the file and close the file descriptor, and delete the allocated buffer.

◆ file_reader() [4/4]

plssvm::detail::io::file_reader::file_reader ( file_reader &&  other)
noexcept

Implement the move-constructor since file_reader is move-only.

Parameters
[in,out]otherthe file_reader to move the values from

Member Function Documentation

◆ operator=() [1/2]

file_reader& plssvm::detail::io::file_reader::operator= ( const file_reader )
delete

Delete the copy-assignment operator since file_reader is move-only.

Returns
*this

◆ operator=() [2/2]

file_reader& plssvm::detail::io::file_reader::operator= ( file_reader &&  other)
noexcept

Implement the move-assignment operator since file_reader is move-only.

Parameters
[in,out]otherthe file_reader to move the values from
Returns
*this

◆ open() [1/3]

void plssvm::detail::io::file_reader::open ( const char *  filename)

Associates the current file_reader with the file denoted by filename, i.e., opens the file filename (possible memory mapping it).

This function is called by the constructor of file_reader accepting a std::string and is not usually invoked directly.

Parameters
[in]filenamethe file to open
Exceptions
plssvm::file_reader_exceptionif the file_reader has already opened another file
plssvm::file_not_found_exceptionif the filename couldn't be found

◆ open() [2/3]

void plssvm::detail::io::file_reader::open ( const std::string &  filename)

Associates the current file_reader with the file denoted by filename, i.e., opens the file filename (possible memory mapping it).

This function is called by the constructor of file_reader accepting a std::string and is not usually invoked directly.

Parameters
[in]filenamethe file to open
Exceptions
plssvm::file_reader_exceptionif the file_reader has already opened another file
plssvm::file_not_found_exceptionif the filename couldn't be found

◆ open() [3/3]

void plssvm::detail::io::file_reader::open ( const std::filesystem::path &  filename)

Associates the current file_reader with the file denoted by filename, i.e., opens the file filename (possible memory mapping it).

This function is called by the constructor of file_reader accepting a std::string and is not usually invoked directly.

Parameters
[in]filenamethe file to open
Exceptions
plssvm::file_reader_exceptionif the file_reader has already opened another file
plssvm::file_not_found_exceptionif the filename couldn't be found

◆ is_open()

bool plssvm::detail::io::file_reader::is_open ( ) const
noexcept

Checks whether this file_reader is currently associated with a file.

Returns
true if a file is currently open, false otherwise ([[nodiscard]])

◆ close()

void plssvm::detail::io::file_reader::close ( )

Closes the associated file.

If memory mapped IO has been used, unmap the file and close the file descriptor, and delete the allocated buffer. This function is called by the destructor of file_reader when the object goes out of scope and is not usually invoked directly.

◆ swap()

void plssvm::detail::io::file_reader::swap ( file_reader other)

Element-wise swap all contents of *this with other.

Parameters
[in,out]otherthe other file_reader to swap the contents with

◆ read_lines() [1/2]

const std::vector<std::string_view>& plssvm::detail::io::file_reader::read_lines ( std::string_view  comment = { "\n" })

Read the content of the associated file and split it into lines, ignoring empty lines and lines starting with the comment.

Parameters
[in]commenta character (sequence) at the beginning of a line that causes this line to be ignored (used to filter comments)
Exceptions
plssvm::file_reader_exceptionif no file is currently associated to this file_reader
Returns
the split lines, ignoring empty lines and lines starting with the comment

◆ read_lines() [2/2]

const std::vector<std::string_view>& plssvm::detail::io::file_reader::read_lines ( char  comment)

Read the content of the associated file and split it into lines, ignoring empty lines and lines starting with the comment.

Parameters
[in]commenta character (sequence) at the beginning of a line that causes this line to be ignored (used to filter comments)
Exceptions
plssvm::file_reader_exceptionif no file is currently associated to this file_reader
Returns
the split lines, ignoring empty lines and lines starting with the comment

◆ num_lines()

std::vector<std::string_view>::size_type plssvm::detail::io::file_reader::num_lines ( ) const
noexcept

Return the number of parsed lines (where all empty lines or lines starting with a comment are ignored).

Returns 0 if no file is currently associated with this file_reader or the read_lines() function has not been called yet.

Returns
the number of lines after preprocessing ([[nodiscard]])

◆ line()

std::string_view plssvm::detail::io::file_reader::line ( typename std::vector< std::string_view >::size_type  pos) const

Return the pos line of the parsed file.

Returns 0 if no file is currently associated with this file_reader or the read_lines() function has not been called yet.

Parameters
[in]posthe line to return
Returns
the line without leading whitespaces ([[nodiscard]])

◆ lines()

const std::vector<std::string_view>& plssvm::detail::io::file_reader::lines ( ) const
noexcept

Return all lines present after the preprocessing.

Returns 0 if no file is currently associated with this file_reader or the read_lines() function has not been called yet.

Returns
all lines after preprocessing ([[nodiscard]])

◆ buffer()

const char* plssvm::detail::io::file_reader::buffer ( ) const
noexcept

Return the underlying file content as one large string.

Returns
the file content ([[nodiscard]])

◆ open_memory_mapped_file_unix()

void plssvm::detail::io::file_reader::open_memory_mapped_file_unix ( const char *  filename)
private

Try to open the file filename and "read" its content using memory mapped IO on UNIX systems.

If the file could not be memory mapped, automatically falls back to open_file().

Parameters
[in]filenamethe file to open
Exceptions
plssvm::file_not_found_exceptionif the filename couldn't be found

◆ open_memory_mapped_file_windows()

void plssvm::detail::io::file_reader::open_memory_mapped_file_windows ( const char *  filename)
private

Try to open the file filename and "read" its content using memory mapped IO on Windows systems.

If the file could not be memory mapped, automatically falls back to open_file().

Parameters
[in]filenamethe file to open
Exceptions
plssvm::file_not_found_exceptionif the filename couldn't be found

◆ open_file()

void plssvm::detail::io::file_reader::open_file ( const char *  filename)
private

Read open the file and read its content in one buffer using a normal std::ifstream.

Parameters
[in]filenamethe file to open
Exceptions
plssvm::file_not_found_exceptionif the filename couldn't be found
plssvm::invalid_file_format_exceptionif an error occurs while reading the contents of the filename

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