12 #ifndef PLSSVM_DETAIL_IO_FILE_READER_HPP_
13 #define PLSSVM_DETAIL_IO_FILE_READER_HPP_
17 #if __has_include(<fcntl.h>) && __has_include(<sys/mman.h>) && __has_include(<sys/stat.h>) && __has_include(<unistd.h>)
19 #define PLSSVM_HAS_MEMORY_MAPPING
20 #define PLSSVM_HAS_MEMORY_MAPPING_UNIX
21 #elif __has_include(<windows.h>)
23 #define PLSSVM_HAS_MEMORY_MAPPING
24 #define PLSSVM_HAS_MEMORY_MAPPING_WINDOWS
32 #include <string_view>
96 void open(
const char *filename);
100 void open(
const std::string &filename);
104 void open(
const std::filesystem::path &filename);
129 const std::vector<std::string_view> &
read_lines(std::string_view comment = {
"\n" });
133 const std::vector<std::string_view> &
read_lines(
char comment);
140 [[nodiscard]]
typename std::vector<std::string_view>::size_type
num_lines() const noexcept;
147 [[nodiscard]] std::string_view
line(typename std::vector<std::string_view>::size_type pos) const;
153 [[nodiscard]] const std::vector<std::string_view> &
lines() const noexcept;
158 [[nodiscard]] const
char *
buffer() const noexcept;
185 #if defined(PLSSVM_HAS_MEMORY_MAPPING)
186 #if defined(PLSSVM_HAS_MEMORY_MAPPING_UNIX)
188 int file_descriptor_{ 0 };
189 #elif defined(PLSSVM_HAS_MEMORY_MAPPING_WINDOWS)
193 HANDLE mapped_file_{};
196 bool must_unmap_file_{
false };
The plssvm::detail::file_reader class is responsible for reading a file and splitting it into its lin...
Definition: file_reader.hpp:42
file_reader()=default
Creates a new file_reader without associating it to a file.
void open(const char *filename)
Associates the current file_reader with the file denoted by filename, i.e., opens the file filename (...
~file_reader()
Closes the associated file.
file_reader & operator=(file_reader &&other) noexcept
Implement the move-assignment operator since file_reader is move-only.
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.
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 start...
file_reader(const file_reader &)=delete
Delete the copy-constructor since file_reader is move-only.
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.
void close()
Closes the associated file.
void open(const std::string &filename)
Associates the current file_reader with the file denoted by filename, i.e., opens the file filename (...
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 start...
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).
file_reader & operator=(const file_reader &)=delete
Delete the copy-assignment operator since file_reader is move-only.
char * file_content_
The content of the file. Pointer to the memory mapped area or to a separately allocated memory area h...
Definition: file_reader.hpp:199
std::string_view line(typename std::vector< std::string_view >::size_type pos) const
Return the pos line of the parsed file.
bool is_open() const noexcept
Checks whether this file_reader is currently associated with a file.
bool is_open_
true if a file is currently associated wih this file_reader, false otherwise.
Definition: file_reader.hpp:205
file_reader(const std::string &filename)
Create a new file_reader and associate it to the filename by opening it (possibly memory mapping it).
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 t...
Definition: file_reader.hpp:203
std::streamsize num_bytes_
The number of bytes stored in file_content_.
Definition: file_reader.hpp:201
const std::vector< std::string_view > & lines() const noexcept
Return all lines present after the preprocessing.
void open(const std::filesystem::path &filename)
Associates the current file_reader with the file denoted by filename, i.e., opens the file filename (...
void swap(file_reader &other)
Element-wise swap all contents of *this with other.
file_reader(file_reader &&other) noexcept
Implement the move-constructor since file_reader is move-only.
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...
void open_file(const char *filename)
Read open the file and read its content in one buffer using a normal std::ifstream.
const char * buffer() const noexcept
Return the underlying file content as one large string.
file_reader(const char *filename)
Create a new file_reader and associate it to the filename by opening it (possibly memory mapping it).
Namespace containing implementation details for the IO related functions. Should not directly be used...
Definition: core.hpp:44
void swap(file_reader &lhs, file_reader &rhs)
Elementwise swap the contents of lhs and rhs.