PLSSVM - Parallel Least Squares Support Vector Machine  2.0.0
A Least Squares Support Vector Machine implementation using different backends.
file_reader.hpp
Go to the documentation of this file.
1 
12 #ifndef PLSSVM_DETAIL_IO_FILE_READER_HPP_
13 #define PLSSVM_DETAIL_IO_FILE_READER_HPP_
14 #pragma once
15 
16 // check if memory mapping can be supported
17 #if __has_include(<fcntl.h>) && __has_include(<sys/mman.h>) && __has_include(<sys/stat.h>) && __has_include(<unistd.h>)
18  // supported (UNIX system)
19  #define PLSSVM_HAS_MEMORY_MAPPING
20  #define PLSSVM_HAS_MEMORY_MAPPING_UNIX
21 #elif __has_include(<windows.h>)
22  // supported (Windows system)
23  #define PLSSVM_HAS_MEMORY_MAPPING
24  #define PLSSVM_HAS_MEMORY_MAPPING_WINDOWS
25 
26  #include <windows.h> // HANDLE
27 #endif
28 
29 #include <filesystem> // std::filesystem::path
30 #include <ios> // std::streamsize
31 #include <string> // std::string
32 #include <string_view> // std::string_view
33 #include <vector> // std::vector
34 
35 namespace plssvm::detail::io {
36 
42 class file_reader {
43  public:
47  file_reader() = default;
53  explicit file_reader(const char *filename);
57  explicit file_reader(const std::string &filename);
61  explicit file_reader(const std::filesystem::path &filename);
67 
71  file_reader(const file_reader &) = delete;
76  file_reader(file_reader &&other) noexcept;
81  file_reader &operator=(const file_reader &) = delete;
87  file_reader &operator=(file_reader &&other) noexcept;
88 
96  void open(const char *filename);
100  void open(const std::string &filename);
104  void open(const std::filesystem::path &filename);
109  [[nodiscard]] bool is_open() const noexcept;
115  void close();
116 
121  void swap(file_reader &other);
122 
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);
134 
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;
159 
160  private:
167  void open_memory_mapped_file_unix(const char *filename);
168 
175  void open_memory_mapped_file_windows(const char *filename);
176 
183  void open_file(const char *filename);
184 
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)
191  HANDLE file_{};
193  HANDLE mapped_file_{};
194  #endif
196  bool must_unmap_file_{ false };
197 #endif
199  char *file_content_{ nullptr };
201  std::streamsize num_bytes_{ 0 };
203  std::vector<std::string_view> lines_{};
205  bool is_open_{ false };
206 };
207 
213 void swap(file_reader &lhs, file_reader &rhs);
214 
215 } // namespace plssvm::detail::io
216 
217 #endif // PLSSVM_DETAIL_IO_FILE_READER_HPP_
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.